I have a form like this in my view:
<form action="http://localhost/RenderForm/public/handle-form" method="POST">
<input type="hidden" name="_token" value="BOwsdSS3Zc4oI08wDutUQbvtQhGvGZXBgxaOoOFD">
<div class="links">
<div>
First Name:<br>
<input name="firstName" type="text">
</div>
<br>
</div>
<div class="links">
<div>
Last Name:<br>
<input name="lastName" type="text">
</div>
<br>
</div>
<div class="links">
<div>
Location:<br>
<select name="location">
<option value="0">HN</option>
<option value="1">HCM</option>
</select>
</div>
<br>
</div>
<div>
<button type="submit">Reset Form</button>
<button type="submit">Complete Task</button>
</div>
</form>
In my controller, I use $request->all() to get all form values and store into a variable. After that, I use json_encode to convert it become a Json object.
When I debug that variable, it have values:
"{"firstName":"hao","lastName":"nguyen","location":"0"}"
But what I really need is:
[ { "id" : "firstName", "value" : "hao" }, { "id" : "lastName", "value" : "nguyen" }, { "id" : "location", "value" : "0" } ]
Can you tell me how to fix this? Thank you very much!
$arr = [json_decode(json_encode($request->all()), true)];