I have a check box with a name of:
<input type='checkbox' name ='schedule[".$row['id']."][1]' />
I want to check if the check box was checked with the PHP isset(...) And I tried it as
isset($_POST['schedule[".$row['id']."][1]]);
But this didn't work. Any ideas that how It can works?
Try it like:
isset($_POST['schedule'][$row['id']][1])
Simply treat it as multi-D array in this case and edit particular index of schedule key.
After the submission of form the GLOBAL VARIABLES $_POST passes the data as string or int instead of original variables.
To check if checkbox was checked try this one:
$id = $row['id'];
if(isset($_POST['schedule'][$id][1])) {
echo "hello world";
}
Related
I have a form when I do var dump on post am getting an array like this
array(1) { [1338099133]=> string(9) "hardcover" }
However when I try to set a variable with the name of the radio button it is giving me an error of undefined variable, despite the fact that the name of the radio input matches the post value and that var dump is showing some values in post...
How do I remove those values in post to a variable please help
Here is my code that shows the radio in put
<input type="radio" name='.$arr[$row]['isbn'].' value="hardcover" >Hardcover:
and here is my post variable
var_dump($_POST);
$value = $_POST[$row]['isbn'];
am I maybe refering to it wrong...?
It appears the undefined variable is $row.
I'm assuming that you are looping through $arr, grabbing the row and outputting each line for your input element. When you go to grab the item from the $_POST array, $row isn't set and that's throwing the error.
The generated html would look like:
<input type="radio" name='1338099133' value="hardcover" >Hardcover:
which matches the key & value in your $_POST array on submit.
To fix this, you'll need to come up with a known name instead of a dynamic one. Something like the following should work:
<input type="radio" name="isbn['.$arr[$row]['isbn'].']" value="hardcover" >Hardcover:
Which then allows you to loop through all of your inputs on submission:
foreach ( $_POST['isbn'] as $isbn => $response ) {
// $isbn = 1338099133;
// $response = 'hardcover';
}
the array key is "1338099133" its value is "hardcover"
if $row in your example is "1338099133" then to set $value to "hardcover" its.
$value = $_POST[$row];
I'm trying to update mySQL database with PHP. I got form on index.php. I'm getting value of id from link using GET method and trying to transfer value to another page with input hidden.
When i try to read this value on another page it always returns 1.
Here is my code Index.php
<?php
$usr = $_GET['id'];
echo $usr ;
?>
<form method="post" action="status.php">
<input type="text" name="number"> <br>
<input type="hidden" name="3" value="<?php echo (isset($usr)) ? $usr : '' ?>" />
<input type="submit">
</form>
And Here is status.php
$hello=isset($_POST['3']);
echo $hello ;
echo "<br>";
It's always 1.
isset() return bool so if variable is available the $hello=1 else $hello=NULL
Wrtie POST as
if(isset($_POST['3']))
{
$hello=$_POST['3'];
echo $hello;
}
You'll using isset() function to assign variables which should not be the case:
$hello = isset($_POST['3']);
This will return true if $_POST['3'] is set.
As from the literal meaning isset() means variable is set. It's a BOOL function, which means that it can only return true when set and false when not set/ empty.
Thus, your code should be:
$hello = $_POST['3'];
More information on isset(): http://php.net/manual/en/function.isset.php.
Tip: It is actually not a good idea to put the value in a hidden textbox and then get it using $_GET, as most people can just edit the value in their browser.
It is always returning '1' because of:
$hello=isset($_POST['3']);
The function isset() returns 1 for if the value is set and 0 if it is not set. In binary 1 means "true" and 0 means "false"
Essentially, your code is saying that $_POST['3'] exists.
However, there may be a bigger underling issue: $_POST is an array so using a number as your input name may be considered bad practice. Try using a name using letters instead like:
<input type="hidden" name="hidden_user"/>
I think you are using isset wrong.
This function, isset, is not to grab the value of something, but just to check if this something is defined.
Try this:
$hello=$_POST['3'];
The proper way to get your post parameter in status.php is:
$hello = isset($_POST[3]) ? $_POST[3] : null;
echo $hello;
ok i have dynamically created form elements with unique names and when validated i want to store all the form data into SESSION.
This is the code to do it:
if(isset($_POST["address_submit"])){
insertSessionVars();
header("Location: http://localhost/project%20gallery/notes.php");
exit;
}
function insertSessionVars(){
foreach($_POST as $fieldname => $fieldvalue){
$_SESSION['formAddresses'][$fieldname] = $fieldvalue;
}
}
This works almost fine but stores also submit button value. I output it like this:
foreach($_SESSION['formAddresses'] as $value){
echo $value;
}
Any help will be greatful :)
Don't assign a name attribute to your submit button. If you assign a name then it is passed as part of the $_POST array.
<input type="submit" value="My Button" />
Since you no longer have the submit button in post, instead of checking if the submit button is set check to see if the post array contains data using count().
if(count($_POST) > 0)
The $_POST values are just array so after the for each why don't you use unset to drop the submit key from the array
That way you can check the form has been submitted and the either choose to not set the submit key or drop it rather then not giving it a name at all
Either way you can in your foreach do something like this (see *) and pass "$except" as parameter.
Where $except has to be the name of your submit button in insertSessionVars();
if(!in_array($key, $except)){...}
And ofc above your foreach something like this :
if(!is_array($except)) $except=array($except);
I would like to test my button can get the current value from the date's textbox? I got the value of submit button but I couldn't find any code get name of the button. So what should I call I'm able to view the value?
$form_button_layout = '<button class="btn" name="oprf" value="view-log" type="submit">View</button>';
<form class="well form-inline" method="post"<?= $form_action_override; ?>>
If you'd like to get the value of 'oprf' above, then:
$value = $_POST['obrf'];
Should do it in php.
I don't think this works consitently in all browsers. Better to work with a customized name attribute.
You can try this one
if(isset($_POST['oprf'])) {
echo $_POST['oprf'];
}
Hope this helps
Add type property with submit type="submit" to button tag this will submit the form then receive the form inputs value in php script with super global variable $_POST['input name'] or $_GET[] wdepends on from action property value by default GET.
You can do:
if ($_POST['oprf'] == '' || $_POST['oprf'] == NULL) {
//this checks to see if there has been a value that was sent with the
//form
}
I usually use isset for cookies.
I have some form fields that when a form is submitted creates an array within the $_POST, I needing to check the this array has atleast 4 keys, how can I check that? I have no idea
try:
<?php
if(count($_POST) >= 4):
//Do your stuff
else:
//Do your error stuff
endif;
If you want to check an array within $_POST as apose to $_POST itself use
count($_POST['name_of_key_to_array_you_want_to_count'])
First, to make your work easier, you should change input name into array version. Something like this should work:
<input type='text' name='data[]' value='' />
Then, PHP will do it's magic and all you have to do is:
echo count($_POST['data']);
This is because your data[] form field is changed into array.
Use array_keys and count:
echo count(array_keys($_POST));
Or simply:
echo count($_POST);
because keys are same in number as items.
The count() function returns the length of an array.