I try to list ALL $_POST array items using var_dump (or echo), but null value items are not displayed. If I use var_dump($_POST) null doesn't appears, but if I use var_dump($_Post["nullitem"]) null appears:
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
var_dump($_POST);
foreach ($_POST as $key => $value) {
echo $key."=>";
echo $value;
echo " - ";
}
echo "<br>";
echo "ck_1 "; var_dump($_POST["ck_1"]);
echo "ck_2 "; var_dump($_POST["ck_2"]);
echo "ck_3 "; var_dump($_POST["ck_3"]);
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
Only ck_2 is checked, so this example will output :
array
'ck_2' => string '1' (length=1)
'submit' => string 'Submit' (length=6)
ck_2=>1 - submit=>Submit -
ck_1 null
ck_2 string '1' (length=1)
ck_3 null
How can I include ALL $_POST values in foreach loop (I don't know how many keys nor names in $_POST array)
Thanks for help
Regards
Sorry.
The unchecked checkbox is not set, so is not member of $_POST array and does not appears
A way to get a value for unchecked checkbox is to set an hidden field with same name and id and unchecked value (like 0), so at post time if unchecked hidden value is returned :
<input type="hidden" name="cx1" value="0" />
<input type="checkbox" name="cx1" value="1" />
Thank's Midzai
I think you are including all the values of $_POST array in your foreach. The thing is, if you don't check in the checkbox the $_POST array won't contain it's key nor it's value.
checkbox i believe has only one value possible and that shows only when you "check-in" the checkbox. othervise the $_POST isn't populated with the key. Why you see NULL when you direcly query the $_POST with the specified key name (name of the checkbox that wasn't set) the key does not exist in the $_POST array and to return something it returns NULL.
If you for some obscure reason need to list all the checkboxes that were available to be chcecked in to the user, you can add
<input type='hidden' name='cbNames[]' value='ck_1'/>
<input type='hidden' name='cbNames[]' value='ck_2'/>
<input type='hidden' name='cbNames[]' value='ck_3'/>
for each of the checkboxes on your site and then list through the $_POST['cbNames'] array and query the $_POST for those:
foreach ($_POST['cbNames'] as $cbName)
print $_POST[$cbName];
Try this
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
echo "<pre>";
print_r(array_filter($_POST["ck_1"]));
echo "</pre>";
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1[]' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2[]' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3[]' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
Related
I'm making a Quiz. And with each question I'm showing the possible answers( "True" or "False") with a While loop in PHP:
echo "<form method='post' action='quizCheck.php'>";
while(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer' value='true' />
</label>
<label>False
<input type='radio' name='answer' value='false' />
</label>
</div>";
}
echo "</form>";
Let's say there are 10 questions and I select "True" on 6 questions.
What code do I have to put in quizCheck.php so it can count the number of "True" answers and store it in a variable?
You will need to do two things, first you need a submit button in the form:
<button type="submit" value="Submit">Submit</button>
Then you will also need the names of the radio inputs to be unique so in the while loop (which you really should just change to a for loop) do:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer{$x}' value='true' />
</label>
<label>False
<input type='radio' name='answer{$x}' value='false' />
</label>
</div>";
}
When the form is submitted, then in quizCheck.php you just check $_POST[answer0] through $_POST[answer9] to see which are true and increment a counter.
If you want the answers in a single array then do this:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answers[$x]' value='true' />
</label>
<label>False
<input type='radio' name='answers[$x]' value='false' />
</label>
</div>";
}
When this form is submitted, then in quizCheck.php you just get something like $answers = $_POST[answers] and then go through answers[0] to answers[9] for example
I would like to pass variables through 3 pages. The 1st page asks the user which music genres they like (there will eventually be 20+ genres). The 2nd page asks the user to rank the genres they have selected and the 3rd page sorts and displays their ranking.
This first page asked the user to pick which genres they like:
<form id="genre" name="genre" method="post" action="musicsell.php">
<input type="checkbox" name="genre[]" value="Rap"/>Rap<br />
<input type="checkbox" name="genre[]" value="HipHop"/>HipHop<br />
<input type="checkbox" name="genre[]" value="RnB"/>RnB<br />
<input type="checkbox" name="genre[]" value="Rock"/>Rock<br />
<input type="checkbox" name="genre[]" value="Jazz"/>Jazz<br />
<p>
<input type="submit" value="Next">
<br />
</p>
</form>
This second asks them to rank (prioritize) the genres they have selected with 1 being the best:
<body>
The genre(s) you selected are: <br>
<form id="form1" name="form1" method="post" action="musicresults.php">
<?php
$name = $_POST['genre'];
if(isset($_POST['genre'])) {
foreach ($name as $genre){
?>
<input
type="number" required="required" id="<?php echo $genre ?>" name="music[<?php echo $genre ?>]" />
<?php echo $genre ?><br />
<?php
}
}
?>
<input type="submit" name="button" id="button" value="Submit" /></form>
</body>
The third and last page sorts and echos the results:
<?php
//Get the form results (which has been converted to an associative array) from the $_POST super global
$musicgenres = $_POST['music'];
//Sort the values by rank and keep the key associations.
asort($musicgenres, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music => $rank)
{
echo "$musicgenres is your $rank choice";
echo "<br>";
}
?>
It all works fine until the last page where I'm getting an "array to string conversion" error. Maybe I need to put in session variables but I'm not sure.
Any help would be appreciated.
It's exactly what the error says. It can't convert and array to a string.
Replace
echo "$musicgenres is your $rank choice";
with
echo "$music is your $rank choice";
I have a situation where i do need unchecked radio button value. Is it possible to send unchecked radio button value through form in php? If yes, then can you please explain?
<input type="hidden" name="myField" value="false" />
<input type="checkbox" name="myField" value="true" />
both fields have the same name, if the checkbox isn't checked, the hidden field will be submitted, if the checkbox is checked, it will virtually override the the hidden field.
On a side note, are your sure radio buttons wouldn't be better suited to your needs, you will be able to see the definite answer for each questions, whereas checkboxes are more for submitted only the selected checkboxes. Failing that you could always assume that all checkboxes that were not submitted as checked, could be assumed to be unchecked.
Create sample.php file and paste below code and run it.
<?php
echo "<pre>";
if(isset($_POST['numbers']) && isset($_POST['unchecked']))
{
$checked_items = $_POST['numbers'];
$unchecked_items = array_diff($_POST['unchecked'],$_POST['numbers']);
echo 'Checked Item<br/>';print_r($checked_items);
echo '<br/><br/>Unchecked Items<br/>';print_r($unchecked_items);
}
?>
<form name='frmname' action='' method='post'>
<input type='radio' name='numbers[]' value='one'/>One
<input type='radio' name='numbers[]' value='two' />Two
<input type='radio' name='numbers[]' value='three' />Three
<input type='hidden' name='unchecked[]' value='one' />
<input type='hidden' name='unchecked[]' value='two' />
<input type='hidden' name='unchecked[]' value='three' />
<input type='submit' name='submit' value='Submit' />
</form>
I have solved it. I have sent both radio button values in hidden field and as well as radio button values. On receiving page, I have compare checked value with hidden field value and so I got unchecked value. Thanks for all who responded to my question.
I am trying to perform some array-ception here: Array within an array. I have a string forms which pass on hidden values to the next form. One of these forms submits an array of checkbox values to another form. That form then submits all previous forms into a database. It's working great except for one problem. When passing the array of checkboxes through another form, it puts the array into another array. I want to select the nested array and store it in a local variable. The array is called interests
Array ( [0] => Array ( [0] => movies [1] => art [2] => cars [3] => business [4] => comedy [5] => technology ) )
Right now my array looks like the array above. When I add [0] to $_POST['interests'] like so:
$int = $_POST['interests'][0];
It successfully stores the second array into the variable int. Problem is that it stores it as a string and not an array. It still looks like an array when I echo it out though
Array ( [0] => movies [1] => art [2] => cars [3] => business [4] => comedy [5] => technology )
How do I store the nested array in a variable. Or how do I turn the above string into an array.
Thank you
If I understood what you mean (and it's a huge if), you might use the following:
Original page:
<form method="POST" ... >
<input type='checkbox' name='passable[interests][]' value='movies'>
<input type='checkbox' name='passable[interests][]' value='art'>
<input type='checkbox' name='passable[interests][]' value='cars'>
<input type='checkbox' name='passable[interests][]' value='business'>
<input type='checkbox' name='passable[interests][]' value='comedy'>
<input type='checkbox' name='passable[interests][]' value='technology'>
</form>
Intermediate page:
<?php
function pass_previous_data ()
{
foreach ($_POST['passable'] as $name => $block)
foreach ($block as $value)
{
echo "<input type='hidden' name='passable[$name][]' value='$value'>";
}
}
?>
<form method="POST" ... >
// by default, form fields will not be forwarded
<input type='text' name='added_data' value='whatever'>
// inject previous data as hidden fields
<?php pass_previous_data (); ?>
// add more forwardable data
<input type='checkbox' name='passable[languages][]' value='English'>
<input type='checkbox' name='passable[languages][]' value='Español'>
<input type='checkbox' name='passable[languages][]' value='Deutsch'>
<input type='checkbox' name='passable[languages][]' value='Русский'>
</form>
The final page should see the whole packet of selected values as an array.
The system allows you to forward data across multiple pages by declaring input names as passable.
It makes retrieving data pretty straightforward with minimal PHP code.
All this being said, it's a pretty inefficient way of passing data around:
you generate a whole hidden HTML input to pass each single value.
You could encode your arrays with Json or PHP serialization mechanism instead, but I suppose you have your reasons to do otherwise.
Here is a one-page working demo of the above example (a little obfuscated by PHP/HTML blending though)
<!DOCTYPE html>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<?php
// recreate hidden fields to forward previous post data
function pass_previous_data ()
{
foreach ($_POST['passable'] as $name => $block)
foreach ($block as $value)
{
echo "<input type='hidden' name='passable[$name][]' value='$value'>";
}
}
if (!isset($_POST['page']))
{ // first page
?>
<body onload='document.getElementById("post").submit();'>
<form method="POST" id="post">
<input type='hidden' name='page' value='2'>
<input type='checkbox' name='passable[interests][]' value='movies' checked>
<input type='checkbox' name='passable[interests][]' value='art' checked>
<input type='checkbox' name='passable[interests][]' value='cars'>
<input type='checkbox' name='passable[interests][]' value='business'>
<input type='checkbox' name='passable[interests][]' value='comedy'>
<input type='checkbox' name='passable[interests][]' value='technology' checked>
</form>
</body>
<?php } else switch ($_POST['page']) {
case '2': // second page
?>
<body onload='document.getElementById("post").submit();'>
<form method="POST" id="post">
<input type='hidden' name='page' value='3'>
<?php pass_previous_data (); ?>
<input type='text' name='added_data' value='whatever'>
<input type='checkbox' name='passable[languages][]' value='English' checked>
<input type='checkbox' name='passable[languages][]' value='Español'>
<input type='checkbox' name='passable[languages][]' value='Deutsch' checked>
<input type='checkbox' name='passable[languages][]' value='Русский' checked>
</form>
</body>
<?php break;
case '3':
// final page
echo"<pre>";print_r ($_POST);echo"</pre>";
break;
}
?>
I have 3 textfields and only one submit button to send all the data of the textfield at once. But how do I send the data of all three textfields at once in php?
<form>
for($x=0;$x<3;$x++){
<input type="text" name="name">
}
<input type="submit" name="submit">
</form>
Now I have a three fields inside a for loop and I have to extract data from all of them using single submit button.So how can I do that?
Using the 'name' attribute on an input allows you to do this for example
<form action='submit.php' method='post'>
<input type='text' name='one'></input>
<input type='text' name='two'></input>
<input type='text' name='three'></input>
<input type='submit' name='submit' value='Submit!' />
</form>
and in your PHP you would do something like this
<?php
if(isset($_POST['submit'])){
$inputOne = $_POST['one'];
$inputTwo = $_POST['two'];
$inputThree = $_POST['three'];
//Do whatever you want with them
}
?>
There are better ways of doing this, but this is probably the simplest to understand
If you want all the inputs to have the same name do this
<input type='text' name='textinput[]'></input>
Use that instead and loop through all of the inputs like so
<?php
foreach($_POST['textinput'] as $input){
//do something with $input
}
?>
You put them all inside the same <form> and make sure they have different values for the name attributes (or that the values end in []).
I believe what you are looking for is this Note the [ ] behind the name field
<form>
for($x=0;$x<3;$x++) {
<input type="text" name="name[]" />
}
<input type="submit" name="submit" />
</form>
Then to retrieve the values
$names = $_POST['name'];
foreach( $names as $name ) {
print $name;
}