html form input array data not populating in $_GET - php

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>

Related

How to add each of checked checkboxes to a signle row in MySQL

I have several checkboxes that contain names (ids as referrence to database) - see code below. How can I select all checked values and add them to database (via MySQL) each row for each checked?
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
After clicking "send" with requiered checked names, the result in database should look like this (I selected John and Mike):
Id
1
3
(only selected ones)
How can I achieve that?
You need to wrap your inputs in a <form> element and give this form a method of post:
<form method="post">
<input type="checkbox" value="1" name="names[]" />John
<input type="checkbox" value="2" name="names[]" />Peter
<input type="checkbox" value="3" name="names[]" />Mike
<input type="checkbox" value="4" name="names[]" />Kattie
<input type="submit" value="Send" name="send" />
</form>
This will allow you to post submitted data from your form inputs to your PHP.
Note: If your HTML is in a different file (ie not in the same file as your form) you can add the action attribute to your form (eg: action="fileWithPHP.php")
Now you can access all checked checkboxes in your PHP using $_POST['names']. This will allow you to get your array of checked values. You can then use a foreach loop to loop through every value in your array:
<?php
if(isset($_POST['names'])) {
$names = $_POST['names'];
foreach($names as $name) {
echo "Add " . $name . " to db here<br />"; // add $name to db
}
}
?>
You can wrap the inputs around a <form> and send it to php and retrieve using $_GET or $_POST and update the database.
I have used POST method here.
HTML:
<form action="test.php" method="post">
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
</form>
PHP:
if(!empty($_POST['names'])) {
foreach($_POST['names'] as $check) {
echo $check; //instead of echo you can insert it to the database
}
}

Dumping $_POST variable in 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

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>

Different checkboxes send the user to different pages when checked

I need a form in which the checkboxes would open different pages based on their selection when the form is submitted.
So, say I have this simple form:
<form action="" method="post">
<input value="1" type="checkbox" name="sign" />
<input value="2" type="checkbox" name="sign" />
<input value="3" type="checkbox" name="sign" />
<input value="4" type="checkbox" name="sign" />
<input value="5" type="checkbox" name="sign" />
<input type="submit" />
</form>
When an user checks value 1 and submits, he will be redirected to page A. If a user checks 1 and 4 he will be redirected to a different page (page F, for instance). If a user checks 2, 3 and 4 he will be redirected to page R, and so on... There would be 25 different combinations, and therefore, 25 different page results for this form when an user submits it.
In other words, when the form is submitted somehow the system would read which checkboxes were checked and associate each possible combination with a different URL.
Can it be done? If so, how? Anyone ever made something similar? I've searched a long time for solutions, but found only slightly similar ones, not exactly what I need, so any help would be appreciated.
HTML:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[1]" />
<input value="2" type="checkbox" name="sign[2]" />
<input value="3" type="checkbox" name="sign[3]" />
<input value="4" type="checkbox" name="sign[4]" />
<input value="5" type="checkbox" name="sign[5]" />
<input type="submit" />
</form>
PHP:
if (isset($_POST['sign'][1]))
header("Location: a.php");
elseif(isset($_POST['sign'][2]) AND isset($_POST['sign'][3]))
header("Location: b.php");
This can be done in a couple of ways. One is that you can use javascript to intercept the form submission, change the value of the form "action", and then execute the submit.
A second approach might be to just send all this data to a single script and based on the selected values perform a redirect to the intended page.
From should look like:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
And the post like:
if(!empty($_POST['sign'])) {
if(in_array(1, $_POST['sign'])) {
// if just 1, go to page
} elseif(in_array(1, $_POST['sign']) && in_array(4, $_POST['sign'])) {
// if 1 and 4, go to page
}
}
You can just consider each of the inputs as a bit. So, you will always have 5 bits to consider.
Then define an associative array of 25 entries corresponding to each of the possible values:
00001
00010
...
var links = {
"00001" : "www.google.com",
];
Then, when you submit the form, just set the target attribute of the form based on the value.
If I were you and I had no choice than following this idea, I would use something like that in jQuery (assuming your post vars are treated in the page you want to reach):
<form action="" method="post" id="myForm">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
$("#myForm").submit(function() {
var checked_array = new Array();
$("#myForm input").each(function() {
if ($(this).is(":checked")
checked_array.push($(this).attr("value"));
});
if ( checked_array.indexOf(2) !== -1 && checked_array.indexOf(5) !== -1)
("#myForm").attr("action", "/url1.php") ;
else if etc...
});

getting multiple checkboxes names/id's with php

How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="orange" id="orange">
<input type="checkbox" name="apple" id="apple">
<input type="checkbox" name="sky" id="sky">
<input type="checkbox" name="sea" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.
There are several ways of handling checkboxes in PHP:
Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
Give each checkbox a different name and a value.
Give each checkbox a different name, but no value.
In each case, you need to check for the existence of the checkbox name in the $_POST array.
For example:
<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">
To get the values for these checkboxes:
if (isset($_POST['color'])) {
$colors = $_POST['color'];
// $colors is an array of selected values
}
However, if each checkbox has a different name and an explicit value like this:
<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">
You still need to use isset():
if (isset($_POST['orange'])) {
// orange has been set and its value is "orange"
}
If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().
You need to give the inputs the same name:
<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">
Then iterate over the $_POST['selection'] array in PHP.
You won't get the ids but the names will be associative indexes in the $_POST array (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.
if ($_POST['oragne'] == 'on')
You can set them up to post to PHP as arrays, if you build them similar to below:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="box_group_1[oragne]" id="oragne">
<input type="checkbox" name="box_group_1[apple]" id="apple">
<input type="checkbox" name="box_group_1[sky]" id="sky">
<input type="checkbox" name="box_group_1[sea]" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>

Categories