I am struggling to use the form below to post and process to my php script. Currently it only brings up those which are selected. I need it to pass the value of 0 where a checkbox isn't selected. I then need my php to iterate through all rows and update by sql.
<form action="update.php" method="post">
<table class="table table-striped">
<tr class="name_5">
<input type="hidden" name="id[]" value="5" />
<td>
iD: 5 Name: Sam
</td>
<td>
<input type="checkbox" name="block1[]" value="1" />
</td>
<td>
<input type="checkbox" name="block2[]" value="1" />
</td>
<td>
<input type="checkbox" name="block3[]" value="1" />
</td>
<td>
<textarea name="notes[]" cols="20" rows="1"></textarea>
</td>
</tr>
<tr class="name_4">
<input type="hidden" name="id[]" value="4" />
<td>
ID: 4 Name: Joanne<br/>
</td>
<td>
<input type="checkbox" name="block1[]" value="1" />
</td>
<td>
<input type="checkbox" name="block2[]" value="1" />
</td>
<td>
<input type="checkbox" name="block3[]" value="1" />
</td>
<td>
<textarea name="notes[]" cols="20" rows="1"></textarea>
</td>
</tr>
<tr class="name_2">
<input type="hidden" name="id[]" value="2" />
<td>
ID: 2 Name: Fiona<br/>
</td>
<td>
<input type="checkbox" name="block1[]" checked value="1" />
</td>
<td>
<input type="checkbox" name="block2[]" checked value="1" />
</td>
<td>
<input type="checkbox" name="block3[]" value="1" />
</td>
<td>
<textarea name="notes[]" cols="20" rows="1"></textarea>
</td>
</tr>
</table>
</form>
I've tried using hidden fields using value of 0 for each row but this didn't work, plus I would rather handle it more elegantly.
When ever you are dealing with checkbox keep one thing in mind that only those checkbox which are selected are submitted with the form submit. So to send all the checkbox irrespective of checked/unchecked use Jquery to iterate over each checkbox and put a check if the checkbox is checked then push 1 in an array otherwise 0 and send that array to server and process it on server.
Unchecked checkboxes must have a default value '0' or 'false' or null etc.. in the related database column.
Checkboxes are 0/1 values, so you can use ENUM type of column, with default value '0'. This is for checkbox related column on your database.
So when you don't update them, because they are not posted, they will still have their default value.
But if you still want them to be posted on form submit, you can use answer on this link Post the checkboxes that are unchecked
Related
I have the following code in my view :
#foreach($totalUsers as $user)
<tr>
<td class="v-a-m ">
<input type="checkbox" class="user" name="user" value="{{$user->id}}">
<input type="hidden" name="city" value="{{$user->city}}">
</td>
<td class="v-a-m text-white">
<span>{{$user->name}}</span>
</td>
<td class="v-a-m">
<span>{{$user->cnic}}</span>
</td>
</tr>
#endforeach
Some Jquery :
$('input.user').on('change', function() {
$('input.user').not(this).prop('checked', false);
});
The hidden field is not returning its iteration result, Instead it returns the result of the last row in the database table.
dd($request->all()); return the requests, but city have the value of last row of the table, Which is well tested.
What i want is that the hidden field should return the current iteration city.
What am I doing wrong here ?
Simply you can't have multiple input fields with the same name.
If you are passing an user id to the controller, Or anything where you can query the users table with, There you can get the city of the exact same user, Whom you checked the checkbox.
Like :
public function findUser(Request $request){
//You can pass anything here with `where()`, E.g name,username
$user = \App\User::find($request->get('id'));
//Do the rest...
}
You should use first() with eloquent where()
If you need your user to select one of the options, you need to replace your checkbox with radio button.
With that, only one option will be allowed to be selected.
CSS and JS can help you hiding the city radio and making it synced with the user radio:
#foreach($totalUsers as $user)
<tr>
<td class="v-a-m">
<label class="user-label">
<input type="radio" class="user-radio" name="user" value="{{ $user->id }}"/>
{{ $user->id }}
</label>
<input type="radio" class="city-radio" name="city" value="{{ $user->city }}"/>
</td>
<td class="v-a-m text-white">
<span>{{ $user->name }}</span>
</td>
<td class="v-a-m">
<span>{{ $user->cnic }}</span>
</td>
</tr>
#endforeach
<style>
.city-radio {
display: none;
}
</style>
<script>
$(document).ready(function() {
$('.user-label').change(function () {
$(this).parent().find('.city-radio').prop("checked", true);
});
});
</script>
Your form can't have multiple input fields with the same name. It works with the checkbox because it's stores the value in an array with the same name. But input type hidden will return the value of the last input of the form with the same name, which is the last row of your table. In other word, the value is overwritten by the last input field.
when you use city[], it will return an array of all cities, which is not what you want.
I'dont understand your purpose of including hidden field with the user's city. Couldn't you just retrieve this data from your controller?
foreach($request->user as $user) {
$user->city // and do your login here.
}
You could "disable/enable" the city you want to send with the user input to "allow/prevent" the hidden field from submitting to the server, in your change event like :
$('input.user').on('change', function() {
$('input.user').not(this).attr('selected', false);
$('input.city').attr('disabled','disabled');
$(this).siblings('input.city').removeAttr('disabled');
});
You could see that just the related city with selected user will be send in the submit action.
Hope this helps.
$('input.user').on('change', function() {
$('input.user').not(this).attr('selected', false);
$('input.city').attr('disabled','disabled');
$(this).siblings('input.city').removeAttr('disabled');
});
$(document).on('submit','form.remember',function(e){
console.log($(this).serialize());
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="remember">
<table>
<tr>
<td class="v-a-m ">
<input type="radio" name="user" class="user" value="1" />
<input type="hidden" name="city" class="city" value="city_1" disabled />
</td>
<td class="v-a-m text-white"><span>name 1</span></td>
<td class="v-a-m"><span>cnic 1</span></td>
</tr>
<tr>
<td class="v-a-m ">
<input type="radio" name="user" class="user" value="2" />
<input type="hidden" name="city" class="city" value="city_2" disabled />
</td>
<td class="v-a-m text-white"><span>name 2</span></td>
<td class="v-a-m"><span>cnic 2</span></td>
</tr>
<tr>
<td class="v-a-m ">
<input type="radio" name="user" class="user" value="3" />
<input type="hidden" name="city" class="city" value="city_3" disabled />
</td>
<td class="v-a-m text-white"><span>name 3</span></td>
<td class="v-a-m"><span>cnic 3</span></td>
</tr>
<tr>
<td class="v-a-m ">
<input type="radio" name="user" class="user" value="4"/>
<input type="hidden" name="city" class="city" value="city_4" disabled />
</td>
<td class="v-a-m text-white"><span>name 4</span></td>
<td class="v-a-m"><span>cnic 4</span></td>
</tr>
</table>
<input type="submit" value="Submit values"/>
</form>
I have a form like this
<form action="" method="post">
<table>
<thead>
<tr>
<th>Shift Name</th>
<th>Start Time</th>
<th>End Time</th>
<th>Apply To</th>
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="shift_name">
</td>
<td>
<input type="time" name="start_time">
</td>
<td>
<input type="time" name="end_time">
</td>
<td>
<select name="departments"><option>All Departments</option></select>
</td>
<td>
<input type="checkbox" name="day_sun">
</td>
<td>
<input type="checkbox" name="day_mon">
</td>
<td>
<input type="checkbox" name="day_tue">
</td>
<td>
<input type="checkbox" name="day_wed">
</td>
<td>
<input type="checkbox" name="day_thu">
</td>
<td>
<input type="checkbox" name="day_fri">
</td>
<td>
<input type="checkbox" name="day_sat">
</td>
</tr>
<tr>
<td>
<input type="text" name="shift_name">
</td>
<td>
<input type="time" name="start_time">
</td>
<td>
<input type="time" name="end_time">
</td>
<td>
<select name="departments"><option>All Departments</option></select>
</td>
<td>
<input type="checkbox" name="day_sun">
</td>
<td>
<input type="checkbox" name="day_mon">
</td>
<td>
<input type="checkbox" name="day_tue">
</td>
<td>
<input type="checkbox" name="day_wed">
</td>
<td>
<input type="checkbox" name="day_thu">
</td>
<td>
<input type="checkbox" name="day_fri">
</td>
<td>
<input type="checkbox" name="day_sat">
</td>
</tr>
<tr>
<td>
<input type="text" name="shift_name">
</td>
<td>
<input type="time" name="start_time">
</td>
<td>
<input type="time" name="end_time">
</td>
<td>
<select name="departments"><option>All Departments</option></select>
</td>
<td>
<input type="checkbox" name="day_sun">
</td>
<td>
<input type="checkbox" name="day_mon">
</td>
<td>
<input type="checkbox" name="day_tue">
</td>
<td>
<input type="checkbox" name="day_wed">
</td>
<td>
<input type="checkbox" name="day_thu">
</td>
<td>
<input type="checkbox" name="day_fri">
</td>
<td>
<input type="checkbox" name="day_sat">
</td>
</tr>
</tbody>
</table>
</form>
you can also check here https://jsfiddle.net/nasir9bd/1twhcc79/
As you can see every rows have checkboxes to select days of the week. And more row can be added with javascript. How efficiently can I grab these checkbox values with PHP as only checked ones returns value. I just need the idea.
Your form values will only ever submit the last ones.
A form like this:
<form>
<input name="banana" type="checkbox" value="1">
<input name="banana" type="checkbox" value="1">
<input name="banana" type="checkbox" value="1">
</form>
Will only submit the last one.
To use the same name, try making them array values:
<form>
<input name="banana[]" type="checkbox" value="1">
<input name="banana[]" type="checkbox" value="1">
<input name="banana[]" type="checkbox" value="1">
</form>
Then, in PHP, you can access these by using $_POST['banana'], which will be an array of the values. You should also post a row_id or some other identifier inside your array values (i.e. banana[1], banana[2], banana[3], etc) so that your other fields match up (i.e. shift_name[1] corresponds to day_sun[1] when processing).
When you dynamically add more rows, you will also need to increment the array keys accordingly.
Please note that you're also not closing your input tags for shift_name, start_time and end_time.
Let's say that I have an HTML structure that looks like this. Each left-context-field and right-context-field are related and in the backend it should be possible to link left to its right colleague.
<table>
<tbody>
<tr>
<td>
<input type="text" class="left-context-field" value="First left value">
</td>
<td>
<input type="text" class="right-context-field" value="First right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Second left value">
</td>
<td>
<input type="text" class="right-context-field" value="Second right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Third left value">
</td>
<td>
<input type="text" class="right-context-field" value="Third right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Fourth left value">
</td>
<td>
<input type="text" class="right-context-field" value="Fourth right value">
</td>
</tr>
</tbody>
</table>
and a user can add as many additional rows as they please. How would I go about posting the values to a PHP file?
Should I wrap all rows in one single huge form, and then call .serializeArray()? Or one form per row?
From a data-oriented approach, what's the best way to deal with this? I'll be sending the data to the back-end, but I want to make the data ass easily accessible for the the backend developer as possible.
I've Edited meda's Answer, but he is not approving that Edit, So I'm posting it here again.
In HTML forms the way you send arrays is using brackets.
For example
name="values[]"
like this.
<form name = 'vals_form' action = 'php_page.php' method = 'post'>
<table>
... <!-- Your all code -->
<tr>
<td>
<input type="text" class="left-context-field" value="First left value" name="values[]">
</td>
<td>
<input type="text" class="right-context-field" value="First right value" name="values[]">
</td>
</tr>
...
<input type = 'submit' value = 'Post Values' />
</table>
</form> <!--and close the form tag-->
In PHP you get the array back
$values = $_POST['values'];
In HTML forms the way you send arrays is using brackets.
For example
name="values[]"
like this.
<form name = 'vals_form' action = 'php_page.php' method = 'post'>
<table>
... <!-- Your all code -->
<tr>
<td>
<input type="text" class="left-context-field" value="First left value" name="values[]">
</td>
<td>
<input type="text" class="right-context-field" value="First right value" name="values[]">
</td>
</tr>
...
<input type = 'submit' value = 'Post Values' />
</table>
</form> <!--and close the form tag-->
In PHP you get the array back
$values = $_POST['values'];
A strange error came up today on our e-commerce site, despite being used 100's of times by iphones in the past one customer tried multiple times but kept getting errors. Checking the database showed the value from their payment method was not getting stored.
I can't see anything wrong with the form at all and it works for everyone else including some other iphones. The part of the form in question looks like.
<tbody >
<tr >
<td ><input name="payment_type" type="radio" value="nc" checked id="master_card" /> <label for="master_card" >Master Card</label></td>
</tr>
<tr >
<td ><input name="payment_type" type="radio" value="nc" id="visa"/> <label for="visa" >Visa</label></td>
</tr>
<tr >
<td ><input name="payment_type" type="radio" value="sc" id="maestro" /> <label for="maestro" >Maestro</label></td>
</tr>
<tr >
<td ><input name="payment_type" type="radio" value="sc" id="visa_delta"/> <label for="visa_delta" >Visa Delta</label></td>
</tr>
<tr >
<td><input name="payment_type" type="radio" value="sc" id="switch"/> <label for="switch" >Switch/Solo</label></td>
</tr>
<tr>
<td ><input name="payment_type" type="radio" value="paypal" id="paypal" /> <label for="paypal" >(PayPal)</label></td>
</tr>
</tbody>
Looks fine to me, but you may want to try checked="checked" - usually problems like this happen when your DOM is messed up and the browser is having trouble displaying the document. I'd advise putting the whole page through W3C validator and fix all the problems, then see if that fixes the problem.
<input name="payment_type" type="radio" value="nc" checked="checked" id="master_card" />
<label for="master_card" >Master Card</label>
<input name="payment_type" type="radio" value="nc" id="visa"/>
<label for="visa" >Visa</label>
<input name="payment_type" type="radio" value="sc" id="maestro" />
<label for="maestro" >Maestro</label>
<input name="payment_type" type="radio" value="sc" id="visa_delta"/>
<label for="visa_delta" >Visa Delta</label>
<input name="payment_type" type="radio" value="sc" id="switch"/>
<label for="switch" >Switch/Solo</label>
<input name="payment_type" type="radio" value="paypal" id="paypal" />
<label for="paypal" >(PayPal)</label>
can someone give me help, please.
here's my basic html
<form action="addSomething.php" method="POST">
<table>
<tr>
<th>Add Data</th>
<th>Description</th>
<th>Quantity</th>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1" /> </td>
<td class="desc">Newbie</td>
<td>2</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1" /> </td>
<td class="desc">Pro</td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1"/> </td>
<td class="desc" > Master </td>
<td>1</td>
</tr>
<br/>
<input type="submit" name="add" value="SUBMIT"/>
.....
how can I get the one with the class "desc" and the column for quantity that is checked after submitting the form
because the only I can add when querying in mysql is the value of checkbox but I want also the value of the data in "Description" column and "Quantity" column
In my addSomething.php the code I have is
if(isset($_POST['add']))
{
foreach($_POST['data'] as $value)
{
$sql = "INSERT INTO tablename (column1) VALUES('$value');"
//query stuff
}
}
what I will do , Any hints guys?
you can give your checkboxes different values
<input type="checkbox" name="data[]" value="newbie" />
<input type="checkbox" name="data[]" value="pro" />
<input type="checkbox" name="data[]" value="master" />
and then in addSomething.php define an array
$names = array('newbie'=>'Newbie', 'pro'=>'Pro', 'master'=>'Master');
and use it in your sql
if(isset($_POST['add']))
{
foreach($_POST['data'] as $value)
{
$sql = "INSERT INTO tablename (column1) VALUES ('".$names[$value]."');";
}
}
this applies only if you don't want users to edit the decription and quantity on the frontend. if you do, you need to put inputs in there and give them unique names.
I'd advise something along the lines of:
<form action="addSomething.php" method="POST">
<table>
<tr>
<th>Add Data</th>
<th>Description</th>
<th>Quantity</th>
</tr>
<tr>
<td><input type="checkbox" name="data[0]" value="sample1" /> </td>
<td class="desc">Newbie<input type="checkbox" name="desc[0]" value="Newbie" /></td>
<td>2<input type="checkbox" name="quan[0]" value="2" /></td>
</tr>
<tr>
<td><input type="checkbox" name="data[1]" value="sample1" /> </td>
<td class="desc">Pro<input type="checkbox" name="desc[1]" value="Pro" /></td>
<td>1<input type="checkbox" name="quan[1]" value="1" /></td>
</tr>
<tr>
<td><input type="checkbox" name="data[2]" value="sample1"/> </td>
<td class="desc" > Master <input type="checkbox" name="desc[2]" value="Master" /></td>
<td>1<input type="checkbox" name="quan[2]" value="1" /></td>
</tr>
<br/>
<input type="submit" name="add" value="SUBMIT"/>
...
with php
if(isset($_POST['add']))
{
foreach($_POST['data'] as $i => $value) {
$desc = mysql_real_escape_string($_POST['desc'][$i]);
$quan = mysql_real_escape_string($_POST['quan'][$i]);
$sql = "INSERT INTO tablename (desc,quan) VALUES('$desc','$quan');"
//query stuff
}
}
You have to explicitly number your fields in html to preserve associacion of data with checkboxes.
What you could do is write Javascript that triggers an AJAX request to your server.
Otherwise, you won't be able to get values in prior submit.
That, is by far, very inefficient. Executing a query for each field in your table.
You will either have to put the description in a hidden field or read the description from the database.
I suggest the later option.
You'll need to include these values in a hidden input
<input type="hidden" name="whatever" value="whatever>"
as an example
I think the best way for your code is you create a javascript function and call it on submit of form :
<form action="addSomething.php" method="POST" onsubmit="return postVars()">
when you submit the form, this function will be called. create a 2 hidden forms like this :
<input type="hidden" name="description" id="description">
<input type="hidden" name="quantity" id="quantity">
give a unique id to your td s and pass the id to your js function. fill these inputs in your js function :
document.getElementById('description').value = document.getElementById('td_' + id + '_description').innerHTML;
document.getElementById('quantity').value = document.getElementById('td_' + id + '_quantity').innerHTML;
and in your server side just get description and quantity from $_POST.
hope it's been helpful ;)