PHP how to multiple rows of form via checkboxes - php

My current form includes many rows (100s) each with several values the user can choose and they click submit for each. I would like instead to give them the option to submit multiple rows at a time by using a checkbox with one submit button.
My current code is:
<tr>
<form id='form$y' action=".htmlspecialchars($_SERVER["PHP_SELF"])." method='post''/>
<input type='hidden' form='form$y' name='id' value='$id' />
<td>
<input style='height:18px' form='form$y' type='date' name='date value='$date'/>
</td>
<td>
<select name='status' form='form$y' required/>
<option>$status</option>
<option value='option1'>option1</option>
<option value='option2'>option2</option>
</select>
</td>
<td>
<input form='form$y' type='text' name='comment' value='$comment'/>
</td>
<td>
<input type='submit' value='Save' />
</form>
</td>
</tr>";
As you can see, each row is its own form. I imagine I should be using only one total form if I'm employing check-boxes instead of individual submits? Where do I go from here? Where doe the submit button belong?
I suppose I should replace the submit column with something like this:
<td>
<input type='checkbox' name='check_list[]' value='value$y'>
</td>
I'd appreciate if someone can give me an example (based on my code) of how this should work. I did read what is already there on SO but I couldn't quite get anything to work.

Use one form -
HTML
<form method="post">
<tr>
<input type='hidden' name='id[]' value='$id' />
<td><input type='checkbox' name='check_list[]' value='value$y'></td>
<td>
<input style='height:18px' ftype='date' name='date[]' value='$date'/>
</td>
<td>
<select name='status[]' required/>
<option>$status</option>
<option value='option1'>option1</option>
<option value='option2'>option2</option>
</select>
</td>
<td>
<input type='text' name='comment[]' value='$comment'/>
</td>
</tr>
<tr>.......</tr>
</form>
PHP
if(!empty($_POST)) {
if(count($_POST['check_list']) > 0 && !empty($_POST['check_list'][0])) {
foreach($_POST['check_list'] as $key => $checked) {
$id = $_POST['id'][$key];
// rest of the data and processings
}
}
}

Keep the Submit Button with the form
In PHP
if(!empty($_POST['check_list'])){
// $_POST[check_list] will be an array with selected values only.
// Do whatever you want do with the $_POST[check_list] array.
}

Related

Submit form data within a table via Ajax always sends first row PHP

This table has many rows each with a few columns that are completed essentially as its own mini-form. The variable $y is auto-incremented and identifies which row should be submitted. My old form pre-ajax looked like this:
<tr>
<form id='form$y' action=".htmlspecialchars($_SERVER["PHP_SELF"])." method='post''/>
<input type='hidden' form='form$y' name='id' value='$id' />
<td>
<input style='height:18px' form='form$y' type='date' name='date value='$date'/>
</td>
<td>
<select name='status' form='form$y' required/>
<option>$status</option>
<option value='option1'>option1</option>
<option value='option2'>option2</option>
</select>
</td>
<td>
<input form='form$y' type='text' name='comment' value='$comment'/>
</td>
<td>
<input type='submit' value='Save' />
</form>
</td>
</tr>";
I've now tried to do the same thing via Ajax so that the page does not refresh upon submission and my code looks like this:
<tr>
<form id='form$y' class='col-lg-3'/>
<div class='form-group'>
<input type='hidden' name='id' class='form-control id' value='$id' />
</div>
<td>
<div class='form-group'>
<input style='height:18px' form='form$y' type='date' name='date' class='form-control date' value='$date' required/>
</div>
</td>
<td>
<div class='form-group'>
<select name='status' form='form$y' required/>
<option>$status</option>
<option value='option1'>option1</option>
<option value='option2'>option2</option>
</select>
</div>
</td>
<td>
<div class='form-group'>
<input form='form$y' type='text' name='comment' class='form-control comment' value='$comment'/>
</div>
</td>
<td>
<input type='button' form='form$y' class='btn btn-default send' value='Submit'>
</form>
</td>
</tr>";
In the old form, it always worked correctly and the data from just that row in the table was submitted. In the new form, it always submits the data from the first row regardless of which row I press the 'submit' button on.
The Ajax is:
$(document).ready(function(){
function send_email(id,date,status,comment){
$.post('post.php',{id:id,date:date,status:status,comment:comment}, function(data){
if (data==='sent') {
alert('sent');
}else{
alert('error');
}
});
}
$('.send').click(function(){
var id = $('.id').val();
var date = $('.date').val();
var status = $('.status').val();
var comment = $('.comment').val();
send_email(id,date,status,comment);
})
})
Is there a way to ensure that the value from the correct form is submitted? Does the form ID not work via Ajax? I cobbled together the Ajax from a few sources as I don't use it very often.

How to use multiple checkboxes in php and display all selected values [duplicate]

I've looked around for a few examples here but a lot of them are either too advanced for my grasp of PHP or their examples are too specific to their own projects. I am currently struggling with a very basic part of a PHP form.
I am trying to create a form with a few checkboxes, each assigned a different value, I want these to be sent to a variable (array?) that I can echo/use later, in my case I will be sending the checked values in an email.
So far, I have tried a few variations, but the closest I have come to it is this...
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar' value='Option Three'>3
</td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>
<?php
$checkboxvar[] = $_REQUEST['checkboxvar'];
?>
Where I'd echo $checkboxvar[] into my email.
Am I going about this completely wrong? The other idea I had was to use a lot of if statements.
<form method='post' id='userform' action='thisform.php'> <tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</td> </tr> </table> <input type='submit' class='buttons'> </form>
<?php
if (isset($_POST['checkboxvar']))
{
print_r($_POST['checkboxvar']);
}
?>
You pass the form name as an array and then you can access all checked boxes using the var itself which would then be an array.
To echo checked options into your email you would then do this:
echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want
Please keep in mind you should always sanitize your input as needed.
For the record, official docs on this exist: http://php.net/manual/en/faq.html.php#faq.html.arrays
add [] in the name of the attributes in the input tag
<form action="" name="frm" method="post">
<input type="checkbox" name="hobby[]" value="coding"> coding &nbsp
<input type="checkbox" name="hobby[]" value="database"> database &nbsp
<input type="checkbox" name="hobby[]" value="software engineer"> soft Engineering <br>
<input type="submit" name="submit" value="submit">
</form>
for PHP Code :
<?php
if(isset($_POST['submit']){
$hobby = $_POST['hobby'];
foreach ($hobby as $hobys=>$value) {
echo "Hobby : ".$value."<br />";
}
}
?>
Try this, by for Loop
<form method="post">
<?php
for ($i=1; $i <5 ; $i++)
{
echo'<input type="checkbox" value="'.$i.'" name="checkbox[]"/>';
}
?>
<input type="submit" name="submit" class="form-control" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$check=implode(", ", $_POST['checkbox']);
print_r($check);
}
?>
You need to use the square brackets notation to have values sent as an array:
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>
Please note though, that only the values of only checked checkboxes will be sent.
Also remember you can include custom indices to the array sent to the server like this
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[4]' value='Option One'>4<br>
<input type='checkbox' name='checkboxvar[6]' value='Option Two'>6<br>
<input type='checkbox' name='checkboxvar[9]' value='Option Three'>9
</td>
</tr>
<input type='submit' class='buttons'>
</form>
This is particularly useful when you want to use the id of individual objects in a server array accounts (for instance) to send data back to the server and recognize same at server
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<?php foreach($accounts as $account) { ?>
<input type='checkbox' name='accounts[<?php echo $account->id ?>]' value='<?php echo $account->name ?>'>
<?php echo $account->name ?>
<br>
<?php } ?>
</td>
</tr>
<input type='submit' class='buttons'>
</form>
<?php
if (isset($_POST['accounts']))
{
print_r($_POST['accounts']);
}
?>
if (isset($_POST['submit'])) {
for($i = 0; $i<= 3; $i++){
if(isset($_POST['books'][$i]))
$book .= ' '.$_POST['books'][$i];
}

PHP Multiple Checkbox Array

I've looked around for a few examples here but a lot of them are either too advanced for my grasp of PHP or their examples are too specific to their own projects. I am currently struggling with a very basic part of a PHP form.
I am trying to create a form with a few checkboxes, each assigned a different value, I want these to be sent to a variable (array?) that I can echo/use later, in my case I will be sending the checked values in an email.
So far, I have tried a few variations, but the closest I have come to it is this...
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar' value='Option Three'>3
</td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>
<?php
$checkboxvar[] = $_REQUEST['checkboxvar'];
?>
Where I'd echo $checkboxvar[] into my email.
Am I going about this completely wrong? The other idea I had was to use a lot of if statements.
<form method='post' id='userform' action='thisform.php'> <tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</td> </tr> </table> <input type='submit' class='buttons'> </form>
<?php
if (isset($_POST['checkboxvar']))
{
print_r($_POST['checkboxvar']);
}
?>
You pass the form name as an array and then you can access all checked boxes using the var itself which would then be an array.
To echo checked options into your email you would then do this:
echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want
Please keep in mind you should always sanitize your input as needed.
For the record, official docs on this exist: http://php.net/manual/en/faq.html.php#faq.html.arrays
add [] in the name of the attributes in the input tag
<form action="" name="frm" method="post">
<input type="checkbox" name="hobby[]" value="coding"> coding &nbsp
<input type="checkbox" name="hobby[]" value="database"> database &nbsp
<input type="checkbox" name="hobby[]" value="software engineer"> soft Engineering <br>
<input type="submit" name="submit" value="submit">
</form>
for PHP Code :
<?php
if(isset($_POST['submit']){
$hobby = $_POST['hobby'];
foreach ($hobby as $hobys=>$value) {
echo "Hobby : ".$value."<br />";
}
}
?>
Try this, by for Loop
<form method="post">
<?php
for ($i=1; $i <5 ; $i++)
{
echo'<input type="checkbox" value="'.$i.'" name="checkbox[]"/>';
}
?>
<input type="submit" name="submit" class="form-control" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$check=implode(", ", $_POST['checkbox']);
print_r($check);
}
?>
You need to use the square brackets notation to have values sent as an array:
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>3
</td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>
Please note though, that only the values of only checked checkboxes will be sent.
Also remember you can include custom indices to the array sent to the server like this
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<input type='checkbox' name='checkboxvar[4]' value='Option One'>4<br>
<input type='checkbox' name='checkboxvar[6]' value='Option Two'>6<br>
<input type='checkbox' name='checkboxvar[9]' value='Option Three'>9
</td>
</tr>
<input type='submit' class='buttons'>
</form>
This is particularly useful when you want to use the id of individual objects in a server array accounts (for instance) to send data back to the server and recognize same at server
<form method='post' id='userform' action='thisform.php'>
<tr>
<td>Trouble Type</td>
<td>
<?php foreach($accounts as $account) { ?>
<input type='checkbox' name='accounts[<?php echo $account->id ?>]' value='<?php echo $account->name ?>'>
<?php echo $account->name ?>
<br>
<?php } ?>
</td>
</tr>
<input type='submit' class='buttons'>
</form>
<?php
if (isset($_POST['accounts']))
{
print_r($_POST['accounts']);
}
?>
if (isset($_POST['submit'])) {
for($i = 0; $i<= 3; $i++){
if(isset($_POST['books'][$i]))
$book .= ' '.$_POST['books'][$i];
}

how to display mysql data using drop down?

here is what i came up,
i could insert a data into my "gender" field, but when I'm in edit/update page, i want the "gender" field only displays "Select One" value, not "male/Female"
thanks in advance
<form action='#' method='post' border='0'>
<table>
<tr>
<td>
<select name="gender" id="gender">
<option>Select One</option>
<option>Male</option>
<option>Female</option>
</select>
</td>
</tr>
<td>
<input type='hidden' name='id_number' value='<? php echo $id_number ?>' />
<input type='hidden' name='action' value='edit' />
<input type='submit' value='Edit' />
</td>
</table>
</form>
You have to add an attribute named selected="selected" to the <option> tag that you want selected by default.
Simply import the $gender value from the db into the edit page and then add this :
<select name="gender" id="gender">
<option>Select One</option>
<option <?if $gender=="Male" echo 'selected="selected"'?>>Male</option>
<option <?if $gender=="Female" echo 'selected="selected"'?>>Female</option>
</select>
Thats it friend.I hope it will useful for you much

How can I dynamically add input fields to a form?

I'm not much of a web programmer, but I'm creating a simple web app that has a form where the user can enter a series of points (x,y,z) but I don't know how many the user is going to enter. I don't want to guess the probable maximum (100 maybe?) and put 100 fields on the form because it would look ugly. What's the easiest way to add more fields (or unhide fields) as the user enters data without contacting the server.
Currently I'm just using html & php, but I assume to do this I'll need javascript?
Currently my code looks like this, as the user enters data, I want another row to appear.
<form action="edit.php" method="post">
<table border=1>
<tr>
<td>
<td>X
<td>Y
<td>Z
</tr>
<tr>
<td>1</td>
<td><input type=text name=x1 size=10 value="0.4318"></td>
<td><input type=text name=y1 size=10 value="0.0000"></td>
<td><input type=text name=z1 size=10 value="0.1842"></td>
</tr>
<tr>
<td>2</td>
<td><input type=text name=x2 size=10 value="0.4318"></td>
<td><input type=text name=y2 size=10 value="0.0000"></td>
<td><input type=text name=z2 size=10 value="-0.1842"></td>
</tr>
<tr>
<td>3</td>
<td><input type=text name=x3 size=10 value="-0.3556"></td>
<td><input type=text name=y3 size=10 value="0.0000"></td>
<td><input type=text name=z3 size=10 value="0.1636"></td>
</tr>
... I want more to appear here...
</table>
<button name="submit" value="submit" type="submit">Save</button>
</form>
Any idea the easiest way? Thanks...
I would use jQuery and append the new inputs.
You will most likely have to use javascript, yes. You can use this or write your own using it as a reference:
http://www.mredkj.com/tutorials/tableaddrow.html
1: HTML :
<div class="form-group app-edu">
<label for="Example Details" class="col-xs-3 col-sm-2 control- label">Example Details</label>
<div class="form-group add-field">
<div class="user">
<select name="xx[]" id="xx" required>
<option value="">Education</option>
<option value="Class 10">Class 10</option>
<option value="Class 12">Class 12</option>
<option value="Diploma">Diploma</option>
<option value="PG Diploma">PG Diploma</option>
<option value="Bachelors Degree">Bachelors Degree</option>
<option value="Masters Degree">Masters Degree</option>
<option value="Other Certifications">Other Certifications</option>
</select>
<input type="text" placeholder="Name of Board" name="xx[]" id="xx" required>
<input type="text" placeholder="Name of Institute" name="xx[]" required id="xx">
<input type="text" placeholder="xx" name="xx[]" required id="xx">
<select name="xx[]" id="xx" required>
<option value="">xx</option>
<option value="xx">xx</option>
<option value="xx">xx</option>
<option value="xx">xx</option>
</select>
<input type="text" placeholder="Year and Month of Exam" name="date[]" required id="date" autocomplete="off">
</div>
<div class="add-more"><span>+</span>Add More</div>
</div>
</div>
2: PHP
if(isset($_POST['submit']))
{
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$xx= json_encode($_POST["xx"]);
$query=mysql_query("INSERT INTO `xxx` (`xx`, `xxx`, `xxx`) VALUES ('NULL', '$xx','$xx','$xx')");
}
3: JS
var data_fo = $('.add-field').html();
var sd = '<div class="remove-add-more">Remove</div>';
var data_combine = data_fo.concat(sd);
var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".user"); //Fields wrapper
var add_button = $(".add-more"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append(data_combine); //add input box
//$(wrapper).append('<div class="remove-add-more">Remove</div>')
}
// console.log(data_fo);
});
$(wrapper).on("click",".remove-add-more", function(e){ //user click on remove text
e.preventDefault();
$(this).prev('.user').remove();
$(this).remove();
x--;
})
What you're saying is that you're hand writing the input tags? Or are you saying that you want a dynamic action where a user clicks a button and it adds more table rows?
In anycase, for your code, you just need a loop, like so. I assume $data is whatever data you want to set based on an array that is probably from the database or something:
<?php
for($i=0, $iMaxSize=count($data); $i<$iMaxSize; $i++)
{
?>
<tr>
<td><?= $i+1 ?></td>
<td><input type=text name=x1 size=10 value="<?=$data[$i]['something']"></td>
<td><input type=text name=y1 size=10 value="<?=$data[$i]['somethingelse']"></td>
<td><input type=text name=z1 size=10 value="<?=$data[$i]['somethingelseagain']"></td>
</tr>
<?php
} // end for
?>
Of course you can't copy and past the above, but that's a good starting point.
For dynamically doing it, you can't use php. What it sounds like you want to use is javascript ajax, and php combination.
Appends some HTML to all paragraphs.
<!DOCTYPE html>
<html>
<head>
<style>
p { background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<p>I would like to say: </p>
<script>
$("p").append("<strong>Hello</`enter code here`strong>");
</script>
</body>
</html>

Categories