I have got a question regarding printing an array of checkboxes in the $_POST
When I do:
echo '<pre>';
print_r($_POST);
echo '</pre>';
I can see the Checkboxes which I send through the $_POST
How am I able to read only the checkboxes?
This is what I currently have:
print_r($_POST['checkboxes[]']);
But this doesn't seem to work
If your checkboxes actually have their name='checkboxes[]', then you can access their POST value by doing print_r($_POST['checkboxes']);. You do not have to include the square brackets when referencing the name of the POST value, the brackets are only there make all of the same named elements post as an array.
Related
I have a general question. I wrote the below code to add an array of arrays to the $_POST variable. I know the post variable is an array too. So, after adding my multidimensional array to the post variable one by one, I tried to print_r the data to view it but, only one array would print out.
why is it that only one array would print out?
$x = 0;
for($x; $x < count($return_auth); $x++){
$_POST = $return_auth[$x];
}
print_r($_POST);
Although nerdlyist answer is absolutely right, but I think it is not the right way, in short, you are modifying $_POST variable and since $_POST variable has a meaning attached to it that it contains all the parameters sent in that POST request. If you overwrite it that change will be affected throughout your application and other modules running in your application will see an additional post parameter which is not actually sent in that request in $_POST array which is not right IMO.
What you are doing is overwriting post. Based on what you have here you could do something like this:
$_POST[return_auth] = $return_auth;
Unless you have a reason to loop an array to create an array...
use this in your for loop
$_POST[] = $return_auth[$x];
Edit
this will work better
$_POST['something' . $x] = $return_auth[$x];
now you can access to (foo) for example
$_POST['somethingfoo'];
I am doing a POST to pass checkbox group values using an array. Is it possible to pass another variable that I'm pulling from database and append it into this array? Or are there other ways to pass this particular value along with the array?
echo "<td><input type='checkbox' name='".$checkedIncident."' value='".$incidentID."'/></td></tr>";
This is the array i'm passing to another php on submit. I wish to pass another variable $id too.
Use a hidden input. Assuming $checkedIncident is something like incident[] that is creating an array:
<input type="hidden" name="$checkedIncident" value="$id">
I have an input named like this:
name="ListPassengers[0].Lastname"
How can I display the value, because when I use $_POST['ListPassengers[0].Lastname']
I get the following error:
Notice: Undefined index: ListPassengers[0].Name in C:\wamp\www\3\res.php
Use print_r($_POST). You can also use print_r() for printing $_GET values as well as arrays etc.
all the values passed from one page is stored in $_post variable and these are indexed with names which u have sent them so u just have to use those i.e input field name etc to refer its data
one way to post all data is same as given above of print_r().
u can also use var_dump($_POST)
or u can access one by one using a for loop also
` foreach ($_POST as $key => $value)
echo $key.'='.$value.'<br />'; `
Hello guys i have seen a code which uses $_POST as a variable .The code is
private
$something
function example()
if($_POST) $_POST = $this->$something-1;
if($_SESSION) $_SESSION = $this+1;
if($_COOKIES) $_ COOKIES = $this->something
In this code i have seen $_POST as a variable .Can we us $_POST as a variable instead of
$code = $_POST['code']
I am sorry if i asked this question wrongly .Please help me ..:) .Thanks in advance
A legitimate use of $_POST without a key as in
$_POST['email']
would be ...
is_array($_POST)
This is not using $_POST as a variable, but it is an example of using the $_POST identifier without trying to access one of its elements. Based on your example, this might be something everyone should do before using $_POST?
This has been asked a few times and the general consensus seems to be that there is no issue referencing the $_POST variable directly. In fact by using the line
$code = $_POST['code']
You are in fact adding more code and more work for yourself.
But it is entirely up to you as to how you use $_POST, either way is fine.
Update
You do not necessarily need data posted to the page in order to use $_POST see below
echo '<pre>';
print_r($_POST); //return blank array as nothing has been posted to the page
echo '</pre>';
$_POST[] = 1; //even though no data has been posted we can use $_POST as a variable if we wish
echo '<pre>';
print_r($_POST); // This will print an array where $_POST[0] is equal to 1
echo '</pre>';
$_POST[0] = $_POST[0] + 1;
echo '<pre>';
print_r($_POST); // This will print an array where $_POST[0] is equal to 2 as we added one earlier
echo '</pre>';
So here we can see the use of $_POST. In the third print we have altered the $_POST[0] value. You can also as shown in your code, make $_POST represent a single value rather than an array which is default.
Therefore if we do say
$_POST = 1+1;
echo '<pre>';
print_r($_POST); // This will print 2
echo '</pre>';
This will print 2.
Much like any variable you can use $_POST as a variable name, though I would not recommend this unless dealing with posted data.
How can I print a $_POST?
Example:
echo $_POST['data'];
This returns nothing...
You can also wrap your code with <pre> tags to make your array prints out nicer instead of just 1 continuous line. A trick that was shown by a member on this site.
<pre>
<?php var_dump($_POST); ?>
</pre>
Your code is correct.
You can use either:
var_dump($_POST);
or
print_r($_POST);
to print out the entire POST array for debugging.
You can only show the values of keys that exist. array_keys() returns an array containing the keys that exist in the array. If there is no output for a key despite the fact that the key exists then the array may contain an empty value for that key.