I am retrieving some rows from a $_GET array and some of them are check box values. Since they will only display an "on" status in the $_GET array, I've devised the following solution to add an "off" to my strings that I will be sending to a client. Here is what it looks like:
if(!($_GET['active'][$row])){ //row is incremented in a loop
$oactive = "off";
}else{
$oactive = "FBINSERTa".":". "\"" . $_GET['active'][$row] ."\"";
}
Relatively simple and this works, putting off in $oactive if there is nothing at ['active'][$row]. However, if the value at $_GET['active'][$row] is undefined, PHP echoes a notice:
Notice: Undefined offset: 6 in ...\page.php
on line x
I could suppress it with error_reporting() but I'd like to find a better way. Any ideas?
Check that it's set before trying to use it, with isset
if(isset($_GET['active'][$row]) && $_GET['active'][$row]==false){
You should use isset:
if(isset($_GET['active'][$row])){ //row is incremented in a loop
$oactive = "off";
}else{
$oactive = "FBINSERTa".":". "\"" . $_GET['active'][$row] ."\"";
}
The other option is to include an input with type hidden and a default value ("off" I guess) in your markup immediately before each checkbox (and with the same name as), this way the value of the checkbox will take precidence if checked when the form is submitted, otherwise the default value from the hidden input will get submitted.
Example:
<input type="hidden" name="myCheckbox" value="off" />
<label>
<input type="checkbox" name="myCheckbox" value="on" />
Check me!
</label>
Related
When I do something like:
foreach($_POST as $post_key => $post_value){
/* Any code here*/
}
So, something like:
$varSomething = $_POST['anything'];
$varSomethingElse = $_POST['somethingElse'];
Is it possible? When I catch a $_POST[' '], isn't that variable already consumed?
The main reason why I would do this is because after a form submission, I want to check wether some items of some type got certain value or not.
Is there aything else more appropiate?
Firstly the html code don't use variable types, for example, if you have
<input id="check" type="checkbox" />
without a established value, after that you have echo $_POST['chek'], you could think that the result would be a boolean value (false or true), but the correct result will be "on" or "off", you can coding this case. Also, if you want to know the type of your data, you can use regular expression on server side, for example:
<input type="text" id="number" value="1350" />
.....
PHP code
$data = $_POST['number'];
$regularExpression = "/^\d{1,10}$/";
if (preg_match($regularExpression, $data)) {
echo "Is numeric";
}
Good lucky.
if you don't know what is the name of element which is sending the data. the first method is ohk . but if know the name like password or username you can use second one
in html
<input type="password" name ="password" />
in php
$pass_recvd=$_POST['password'];
there is no way to check the type i.e. text/password/checkbox/select etc. you have to do it on client side BEST WAY IS USING Jquery
if you wanna check whether a variable is set or not simple check by using isset method
if( isset($_POST['someVariableName'])) {}else{}
This is my checkbox
<input name="interests2" type="checkbox" value="double-deep-racks" />
This is how I am trying to get that value in to a variable
$int = $_POST['interests2'];
Can you please tell me what i am doing wrong. I cant get the values I just get blank.
Try
$int = $_POST['interests2'];
If you are trying to set multiple checkboxes you can do something like,
// Your html
<input type="checkbox" name="interests[]" value="This is i">
<input type="checkbox" name="interests[]" value="Another i value">
// php
$email = "Further Information In: \n";
foreach($_POST['interests'] as $i)
$email .= $i . "\n";
The name of your checkbox is interests2. You must get the value by that name like this:
$int = $_POST['interests2'];
The name element must match what you are looking for. In your input field the name is interests2 but you are looking for interests (missing "2").
Also, you may possibly need to look in $_GET instead of $_POST, depending on the form or the AJAX method you are using (you didn't post that portion of your code).
I've probably not explained what I"m trying to do in the title very well, so here goes:
I've got a HTML entry form, in a .php file. That entry form when submitted files a POST operation. That POST operation has code to check field input, similar to this:
<?php
...
if ($_POST["submitted"] == 1) {
//"submitted" is a hidden field with value '1' in the form
$isvalid = 1; // Inits to 1, changes to 0 if something fails checks.
$field1 = $_POST["field1"];
$field2 = $_POST["field2"];
...
/*
Checks for validation on each field go here,
but are not relevant to this question here.
*/
}
if ($isvalid == 1) {
// Send email
} else { ?>
<!-- Print form, and error msg. -->
...
<input name="field1" type="text" id="field1" size="32" class="stylingclass">
...
So the above is example code, but here's the real question: How can I get any of the text input fields in my form, when submitted and an error occurred, to automatically contain the previous values that were entered, so that the user can correct the entries accordingly, based on whatever constraints I've set?
You can set the value parameter using a basic ternary operator:
<input name="field1" type="text" id="field1" value="<?php echo isset($_POST["field1"]) ? $_POST["field1"] : ''; ?>" size="32" class="stylingclass">
Note that the ternary operator is used here to prevent an error if there is no index of the specified field in the $_POST array
Just add value="<?=$_POST['field1']?>" (assuming PHP 5.4, use <?php echo $_POST['field1'] ?> otherwise)
I have a form on the page. It has too many things like checkboxes and text fields and dropdowns.
But when I don't select one of the checkboxes, the PHP page where i catch the form shows me an error.
Example:
The HTML Code:
Checkbox 1<input type="checkbox" name="check1" value="on" />
Checkbox 2<input type="checkbox" name="check2" value="on" />
The PHP Code:
$check1 = $_GET['check1'];
$check2 = $_GET['check2'];
It works fine if both the items are selected and sent in the URL:
localhost/project/checkbox.php?check1=on&check2=on
But when i deselect 1 of them, suppose check2, then the URL is like this:
localhost/project/checkbox.php?check1=on
and it shows me an error - that $check2 is an undefined index.
But I don't want it to show the error if the checkbox is not being selected. I also tried an if statement to check if i'm getting it in the URL but it didn't work.
Is there a way to first check weather the data is being passed in the URL or not? As I don't get the error.
Actually the error is not the main thing, as I'm getting right results and I know I can switch off error reporting in php.ini, but thats not what I want to do. I want it to first check if data is coming in?
Thanks...
if (isset($_GET['check2']) && !empty($_GET['check2']) will do the check if check2 GET parameter is present and is not empty.
The checkbox value is only registered, when it's checked. So you can simply do a check like this:
$check1 = isset($_GET['check1']);
$check2 = isset($_GET['check2']);
$check1 and $check2 will now have boolean values:
echo "Checkbox 1 one is " . ($check1 ? "checked" : "not checked") . PHP_EOL;
echo "Checkbox 2 one is " . ($check2 ? "checked" : "not checked") . PHP_EOL;
I have two forms
a) post.php
b) edit.php
I am using the checkbox in both the form to set the binary value (0 and 1) to set the status for approve.
to process the value from the checkbox in the post.php I am using the code which checks and assume that if the checkbox is checked then the value will be 1 other wise it is 0 by default
if (isset($_POST['ad_approve'])) $ad_approve = htmlspecialchars(strip_tags(mysql_real_escape_string($_POST['ad_approve'])));
else { $ad_approve = 0; }
Now in the edit.php form I retrieve the data from the database and set the checkbox accordingly . if the approve has the value 1 then it is checked by default . look onto the following code
<input name="ad_approve" type="checkbox" value="1" <?php if($approve==1) echo 'checked="checked"';?> />
in the above code I cannot apply the same logic to catch the value from ad_approve i.e(by checking isset()) because if the value is 1 by default and if I try unchecking the checkbox then automatically the program assume that the value is set because it has been changed from checked to unchecked. now in the edit.php how do I again process the value from it such that if the checkbox is checked hold the value as 1 otherwise 0, ??
to be honest your question does not make a whole lot of sense but I can understand that you wish to check if check boxes are actually set.
firstly you don't need a value attribute on a check box because its the browser that decide what value if any to send via the GP headers.
http://www.w3.org/TR/html401/interact/forms.html
Checkboxes (and radio buttons) are
on/off switches that may be toggled by
the user. A switch is "on" when the
control element's checked attribute is
set. When a form is submitted, only
"on" checkbox controls can become
successful.
So basically if a checkbox is not selected then the entity will not be sent via the headers, there for isset() would be a perfectly usable check.
The way your using real escape is totally wrong, you should never user real escape for html, DATABASE ONLY.
Take this code for example
if(isset($_POST['some_check']))
{
echo $_POST['some_check']; //on
}else
{
echo 'some_check has not been checked';
}
Your code:
if (isset($_POST['ad_approve'])) $ad_approve = htmlspecialchars(strip_tags(mysql_real_escape_string($_POST['ad_approve'])));
else { $ad_approve = 0; }
Can you give me a good excuse why your using htmlspecialchars,strip_tags and mysql_real_escape_string on an entity that can be done like so:
$ad_approve = isset($_POST['ad_approve']) ? true : false;
for issues like this its not that hard to refer to the documentation to get a better understand of what exactly is being sent from the browser.
if you wish to add values to to the checkboxes that you would do this with hidden fields, example follows.
<input type="checkbox" name="check_box" />
<input type="hidden" name="check_box_value" value="01100001" />
And within the php server side.
if(isset($_POST['check_box']))
{
echo $_POST['check_box_value']; //01100001
}else
{
echo 'some_check has not been checked';
}
by adding another html input thats hidden you can hide elements from the user and then use as data holders, the reason why you should use the exact same name but append it with _value is just for best practises.
EDIT
if(isset($_POST['some_check_box']))
{
mysql_query('UPDATE profile SET receive_mail = 1');
}else
{
mysql_query('UPDATE profile SET receive_mail = 0');
}