See you're using AlpineJS and Livewire, have you tried to use @entangle to share the state between the two?
You can find more about it in the Livewire documentation on working with AlpineJS.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to bind backAfterSaveStatus value to hidden input and for some reason then form is submited backAfterSave value is null.
After that I go back and submit form again - backAfterSave value is 1. Where is the problem?
I tried same thing without prevent and submit() but it's not working still. Also I had dumped div with x-text and code makes hidden input 1 before form submit. What I am doing wrong?
<form action="<...>" method="post">
<div x-data="{
backAfterSaveStatus: '',
backAfterSave () {
this.backAfterSaveStatus = '1';
document.querySelector('form.withBackAfterSave').submit();
}
}">
<input name="backAfterSave" :value="backAfterSaveStatus">
<div>
<span >
<button
x-on:click.prevent="backAfterSave()"
type="submit">
Save & back
</button>
</span>
<span>
<button type="submit">
Save
</button>
</span>
</div>
</div>
</form>
I want same result as below:
let buttonBackAfterSave = document.getElementById('button-back-after-save');
if (buttonBackAfterSave) {
buttonBackAfterSave.addEventListener('click', () => document.getElementById('input-back-after-save').value = 1);
}
Using the $nextTick function on submit worked for me, this gives the model time to update.
<form action="{{ route('form_submit') }}" method="post" x-data="{ backAfterSaveStatus: 0 }" x-ref="foo">
@csrf
<input type="hidden" name="backAfterSave" x-model.number="backAfterSaveStatus">
<div>
<button x-on:click.prevent="backAfterSaveStatus = 1; $nextTick(() => { $refs.foo.submit() })" type="submit">
Save & Back
</button>
<button x-on:click.prevent="$refs.foo.submit()" type="submit" >
Save
</button>
</div>
</form>
This also uses $ref instead of querySelector to submit the form, I also set the model to 0 by default, so when Save is submitted the value is set to 0. On Save & Back we set it to one then wait for the next tick and submit.
Created a repository to demo it, it's the same HTML with some simple styling and then I'm just dumping the result on post.
Please or to participate in this conversation.