'button form for header sticky by javascript html laravel

There are two buttons on that blades (Laravel). The button (.btn-main) works in the main, but the button (.btn-header) in the header does not work.

I mean when i click the button (.btn-header) in the header, it does nothing and does not save in the database.

I know it is about the form.

inc/header.blade.php

<div class="sticky fixed">
<button type="submit" class="btn-header">Save</button>
</div>

index.blade.php

@extends('layouts.app')
@section('content')
<main>
<form action="{{ route('post.update', $post->id) }}" method="POST">
    @method('PUT')
    @csrf
    <input type="text" name="title" value="Title...">
    <input type="text" name="content" value="This is a test.">
    <button type="submit" class="btn-main">Save</button>
</form>
</div>
@endsection

app.blade.php

<!DOCTYPE html>
<html>
<head>...</head>
<body>
<header>
@include('inc.header')
</header>
@yield('content')
</body>
</html>

I want the buttons to work on both sections (header and main) and store the information in the database.



Solution 1:[1]

Your header button doesn't do anything because it is not part of/inside the form. You can however use a form attribute for your header button to work. Add an id attribute to your <form> element and point the form attribute in your button to that same id.

Your form:

@extends('layouts.app')
@section('content')
<main>
<form action="{{ route('post.update', $post->id) }}" method="POST" id="myForm">
    @method('PUT')
    @csrf
    <input type="text" name="title" value="Title...">
    <input type="text" name="content" value="This is a test.">
    <button type="submit" class="btn-main">Save</button>
</form>
</div>
@endsection

(Added id="myForm" to the <form> element.)

Your header button:

<div class="sticky fixed">
<button type="submit" class="btn-header" form="myForm">Save</button>
</div>

(Added form="myForm" to the button.)

Edit: make sure there is no other id="myForm" on your page, choose a different unique name if so

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brombeer