Dumping $_POST variable in PHP - php

I have a page that has a huge number of checkboxes, the name of the checkboxes are dynamically generated as they convey certain information about each checkbox.
I am trying to view the contents of the posted array with:
var_dump($_POST);
But I am getting an output of:
array(0) {}
Any ideas on how to display the $_POST array? Or maybe transfer it into another array and display it?
Example generated html:
<div id="addProductToCollection">
<div id="collection_heading">
Testing
Another Test
Another Test Collection
</div>
<div id="checkboxes">
<form action="127.0.0.1/view_products.php?product=dvds&page=1" name="checkboxes" method="post">
<div id="collection_row">
<br><input type="checkbox" name="21,dvds,1">
<br><input type="checkbox" name="21,dvds,2">
<br><input type="checkbox" name="21,dvds,3">
<br><input type="checkbox" name="21,dvds,4">
<br><input type="checkbox" name="21,dvds,5">
<br><input type="checkbox" name="21,dvds,6">
<br><input type="checkbox" name="21,dvds,7">
<br><input type="checkbox" name="21,dvds,8">
<br><input type="checkbox" name="21,dvds,9">
<br><input type="checkbox" name="21,dvds,10">
</div>
<div id="collection_row">
<br><input type="checkbox" name="22,dvds,1">
<br><input type="checkbox" name="22,dvds,2">
<br><input type="checkbox" name="22,dvds,3">
<br><input type="checkbox" name="22,dvds,4">
<br><input type="checkbox" name="22,dvds,5">
<br><input type="checkbox" name="22,dvds,6">
<br><input type="checkbox" name="22,dvds,7">
<br><input type="checkbox" name="22,dvds,8">
<br><input type="checkbox" name="22,dvds,9">
<br><input type="checkbox" name="22,dvds,10">
</div>
<div id="collection_row">
<br><input type="checkbox" name="23,dvds,1">
<br><input type="checkbox" name="23,dvds,2">
<br><input type="checkbox" name="23,dvds,3">
<br><input type="checkbox" name="23,dvds,4">
<br><input type="checkbox" name="23,dvds,5">
<br><input type="checkbox" name="23,dvds,6">
<br><input type="checkbox" name="23,dvds,7">
<br><input type="checkbox" name="23,dvds,8">
<br><input type="checkbox" name="23,dvds,9">
<br><input type="checkbox" name="23,dvds,10">
</div>
<input type="submit" value="Update">
</form>
</div>
Solved:
I didn't realise unticked boxes didn't get posted, turns out I needed to tick the checkboxes before posting. Sorry for the inconvinience.

Only checked checkboxes are returned in php $_POST. Try using hidden input if you want all values to be returned.
<input name="checkboxstatus[]" type="hidden" value="0">
You can manupulate hidden iput values using js.

Your form action is incorrect, that will try and submit to a the current URL /127.0.0.1/view_products.php?product=dvds&page=1.
try this:
<form action="http://127.0.0.1/view_products.php?product=dvds&page=1" name="checkboxes" method="post">

Try using print_r($_POST)
But it looks like it isn't posting any data. Check that your html document that is posting the data is wrapped in <form action="submit.php" method="post"></form>

What you want to dump?
You are dumping nothing so it gives empty array.
ever
var_dump($_POST[username]);
this will dump username. likewise you can update your code to dump what ever you want

Related

PHP form multiple checkbox groups

I have a group of checkboxes in a from and I need to get values with php. Note that div class="group" can also be duplicated onruntime (with jquery) so I may end with multiple div class="group" of checkboxes.
Is there a way to set checkboxes name so form data gets grouped together for each "group", or whatever is easier to handles afterwards? There is also a problem of checkboxes that were not checked, so they wont be present in a post data.
I tried using names like this but I dont like the results.
<form>
<div class="group">
<input type="checkbox" name="ev[][time]">
<input type="checkbox" name="ev[][space]">
<input type="checkbox" name="ev[][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[][time]">
<input type="checkbox" name="ev[][space]">
<input type="checkbox" name="ev[][money]">
</div>
</form>
When defining checkbox groups its much better to be verbose with input names and avoid using the [] notation.
A un-ticked checkbox is not sent in post data, and your selections will become misaligned.
<form>
<div class="group">
<input type="checkbox" name="ev[0][time]">
<input type="checkbox" name="ev[0][space]">
<input type="checkbox" name="ev[0][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[1][time]">
<input type="checkbox" name="ev[1][space]">
<input type="checkbox" name="ev[1][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[2][time]">
<input type="checkbox" name="ev[2][space]">
<input type="checkbox" name="ev[2][money]">
</div>
</form>

html form input array data not populating in $_GET

this is what I get when I do print_r($_GET);
Array ([categories%5B%5D] => test1)
and this is the html form
<form method="GET" >
<input type="checkbox" name="categories[]" value="test1" />
<input type="checkbox" name="categories[]" value="test2" />
<input type="checkbox" name="categories[]" value="test3" />
</form>
so when I change the method to POST, the categories array is populated with the checked checkboxes, but when I choose GET, the first checked checkbox is only there, why this happens? and how do I get the data correctly?
There is either additional code on page that is throwing this off or you've got something set up incorrectly in the server settings. Is there additional code you can paste in that we can see? I pasted that exact code + a submit button onto my server and it works perfectly with $_GET and $_POST
<?php print_r($_POST); ?>
<form action="" method="POST" >
<input type="checkbox" name="categories[]" value="test1" />
<input type="checkbox" name="categories[]" value="test2" />
<input type="checkbox" name="categories[]" value="test3" />
<input type="submit">
</form>

email signup multiple lists

I have the following code, I want the user to be able to sign up to multiple lists at the same time. It is currently only signing up emails to one list randomly, even if I check all the lists. Is it possible to do? Maybe some sort of php echo?
<form action="" method="post">
<input name="accName" type="hidden" value="companyname">
<input name="listName" type="hidden" value="">
<input name="fullEmailValidationInd" type="hidden" value="Y">
<input name="doubleOptin" type="hidden" value="false">
<input name="successUrl" type="hidden" value="">
<input name="errorUrl" type="hidden" value="">
Email Address <input class="border" name="email" size='50' type="text" value="">
First Name <input class="border" name="First_Name" size='50' type="text" value="">
Last Name <input class="border" name="Last_Name" size='50' type="text" value="">
<label><input name="listName" type="checkbox" value="list1"></label>
<label><input name="listName" type="checkbox" value="list2"></label>
<label><input name="listName" type="checkbox" value="list3"></label>
<label><input name="listName" type="checkbox" value="list4"></label>
<label><input name="listName" type="checkbox" value="list5"></label>
<input type="submit" value="OK">
</form>
Use array for the checkboxes:
<input type="checkbox" NAME="listName[]" VALUE="list1" />
Or use different names...
You will need to grab this using, php side, a loop:
foreach ($_POST['listName'] as $selected)
Just notice that if none are selected this will fail, thus check if the array exists before:
if (isset($_POST['listName'])
{
foreach ($_POST['listName'] as selected)
{
DO YOUR STUFF
}
}

Post text box value when checkbox is selected

I want to post the text box value to the database when checkbox is checked and the form is submitted,how can i do this.My code snippet is,
Html:
<form name="form1" method="post" enctype="multipart/form-data">
<td>Accessories with computer<br>
Power Adapter<input type="checkbox" name="accessories[]" value="power adapter"><br>
Power Cord<input type="checkbox" name="accessories[]" value="power cord"> <br>
Monitor<input type="checkbox" name="accessories[]" value="monitor"><br>
Keyboard<input type="checkbox" name="accessories[]" value="keyboard"><br>
Mouse<input type="checkbox" name="accessories[]" value="mouse"><br>
<input type="text" size="10" name="others1" class="input-text" />
<input type="checkbox" name="accessories[]" value="<?= $_POST['others1'];?>" ><br>
<input type="text" size="10" name="others2" class="input-text" /><input type="checkbox" name="accessories[]" value="<?=$_POST['others2'];?>" ><br>
</td>
</tr>
</form>
php:
$accessories = implode(' ', $_POST['accessories']);
$query ="INSERT INTO ticket SET accessories = '$accessories'";
$result = mysql_query($query);
I've also tried passing the checkbox value as a variable but value is not stored in database. Please suggest me.
the section of the form looks like,
You mixed insert query with update query. The query should look like this
INSERT INTO ticket (acessiories) VALUES ('$acessories')
<form name="form1" method="post" enctype="multipart/form-data">
<input type="text" size="10" name="others2" class="input-text" onclick="document.form1.submit();" />
</form>
<?php
if(isset($_POST))
{
//Do insert query to insert into database
}
?>
I think you just forgot to mention the " action='filename.php' " in the form tag.
hey refer following code,
<form method="post"> <input type="checkbox" name="check" /> <input type="text" name="txt" /> <input type="submit" name="submit"/> </form>
<?php $a=$_POST['checkboxname']; if($a) /* true when checkbox is checked */ { echo $_POST['textboxname']; } ?>

How can I use several form inputs with the same name?

I have several checkbox (I don't know number of them) that create from a loop in a form.
<form>
<input type="checkbox" name="id" value="id">
<input type="checkbox" name="id" value="id">
...//create in a loop
<input type="checkbox" name="id" value="id">
</form>
My question is that How can I read them, If I use <?php $_REQUEST['id']; ?>, it only reads the last checkbox.
Use an input array:
<input type="checkbox" name="id[]" value="id_a">
<input type="checkbox" name="id[]" value="id_b">
<input type="checkbox" name="id[]" value="id_c">
<!-- ^^ this makes it an array -->
$_REQUEST['id'] can be accessed:
foreach($_REQUEST['id'] as $id)
{
echo $id;
}
Outputs
id_a
id_b
id_c
Side note: this works with $_POST and $_GET (not just $_REQUEST). Generally speaking though $_REQUEST should be avoided if possible.
Use unique id's for your checkboxes, e.g.,
<form>
<input type="checkbox" name="id1" value="value1">
<input type="checkbox" name="id2" value="value2">
...//create in a loop
<input type="checkbox" name="id3" value="value3">
</form>

Categories