I have this checkbox in my html
<input name="cb" class="cmn-toggle cmn-toggle-round" type="checkbox">
What I understood about how checkbox worked, was that when "checked" if you isset the input then it would "exist", and if wasn't "checked" wouldn't, so I did this:
if(isset($_REQUEST['cb'])){
//do something
}else{
//do something else
}
The problem is that when sending the form, it always exist, doesn't matter if checked or not, I don't know how to really see if really checked, so what am I doing wrong?
isset() determine if a variable is set and is not NULL. So in your case, the $_REQUEST['cb'] always exist, so isset() will be true. So if you have isset() in your condition, you need to add a check, if the value is true or false.
You need edit your condition to:
if($_REQUEST['cb']) { ...
or
if($_REQUEST['cb'] == true){ ...
and the best way is use isset with check above:
if( isset($_REQUEST['cb']) && $_REQUEST['cb'] == true ) { ...
You can check like this
if(isset($_REQUEST['cb']) && $_REQUEST['cb']){
Related
I am trying to better understand the ISSET() function. It is my understanding that when used properly, I can determine if a "submit" button was pressed or not because the function determines true or false. I have written some html code that uses the HTML Tag and HTML tag type submit. What I was trying to test was whether the ISSET function would return False while the button was not pressed and True when it was pressed.
Using Wordpress Plugin called "Woody snippets" it allows the combination of HTML and PHP in a shortcode.
Please see my code below:
I have been trying to follow a validation video on youtube = https://youtu.be/x_-HdHijmkw
I did everything line for line and initially it worked, upon refreshing the page, it did not clear all of the variables and the isset function acted as if the boolean was true. I thought it was my cache, so I cleared it. Still the ISSET funtion acts as if the boolean is true.
<?php
if (isset($_GET['fname']) && isset($_GET['lname'])) {
$fname = $_GET['fname'];
$lname = $_GET['lname'];
echo "Hello ".$fname;
}
?>
<form action='' method='get'>
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
<input type="submit" value="Greet Me"><br>
</form>
I expected upon refresh that the isset() function and the variables used to determine true or false would be false until I entered values and pressed the button again.
Pre info: isset($a) && isset($b) is equivalent to isset($a,$b)
isset() proofs the existence of a var, not if it is empty. Try
if ( isset($_GET['fname'],$_GET['lname'])
&& strlen($_GET['fname'])
&& strlen($_GET['lname']) { ...
isset() checks whether the variable exists and will still return true for empty strings, 0, and NULL values. A quick solution would be to use !empty() instead:
if (!empty($_GET['fname']) && !empty($_GET['lname']))
See: In where shall I use isset() and !empty()
I have a form field in my website which i use for a table in my database. i'm using checkboxes for a true/false value. In my case my checkboxes send out an empty string if left unchecked.
I made an if else statement to make sure the empty string is made boolean. however, my if else statements only works for the first one ($ringe). the last two ($halskaeder and $armbaand) does not seem to work, as my database does not register any inputs.
I'm guessing my if else statements has flaws or syntax errors, i'm just too hopeless at PHP to figure out what's wrong.
if ($_REQUEST['ringe'] == ""){
$ringe = '0';
}
else {
$ringe = '1';
}
if ($_REQUEST['halskaeder'] == ""){
$halskaeder = '0';
}
else {
$halskaeder = '1';
}
if ($_REQUEST['armbaand'] == ""){
$armbaand = '0';
}
else {
$armbaand = '1';
}
Any help is highly appreciated!
UPDATE!!!
So I realized my issue is that an unchecked checkbox in html doesn't send and signal. So i have to change that no matter what
AFAIK the check box is not sent if it's not checked. You can do something like:
$default = array(
'ringe' => 0,
'halskaeder' => 0,
'armbaand' => 0,
);
$myFormParams = array_replace($default, $_REQUEST);
var_dump($myFormParams);
few Suggestions... I may add more if you post your form..
1) Trim whitespace from user input (unless you need it)
$input = trim($_REQUEST['user_input']);
2) In this case, you should use empty(), because this function checks to see if the variable exists, and if it's empty, and if it's falsy. You can kill three birds with one stone.
3) Use ternary. It's shorthand for what you're doing.
$input = trim($_REQUEST['user_input']);
$input = empty($input) ? "0" : "1";
I found an answer. My problem wasnt the php code, but the checkboxed not sending any input when unchecked. The workaround is simply putting a hidden input field to send a false signal no matter what. You give it the same name attribute as your real checkbox, which will override when checked.
looks like this:
<input type="hidden" name="ringe" value="0" /><input type="checkbox" name="ringe" value="1" /><p>Ringe</p>
thanks for the help though!
I tried to make a form with a post method, and i want to check if the fields are filled with isset(). But even if the fields are empty, the isset returns true.
Here is a simplified code: (name of the page: test.php, the post method is directed to itself)
if (isset($_POST['test'])) {
echo 'field set';
}
?>
<form method="post" action="test.php">
<input type="text" name="test"/>
<input type="submit" value="Submit"/>
</form>
When you submit, it always echoes 'field set', even if you didn't enter anything in the "test" input.
Thanks for your answers !
So check whether $_POST['test'] is empty or not and try like this
if (isset($_POST['test']) && trim($_POST['test']) != '') {
echo 'field set';
}
You can also use empty,but Note that "0" is also "empty" and along with it its better to trim the inputs to remove spaces. Another way is to check the length of input using strlen
if (isset($_POST['test']) && trim(strlen($_POST['test'])) > 0) {
echo 'field set';
}
try with empty() to check blank cause after submit form $_POST['test'] always found in isset() even it's blank. so need to check with empty()
if (!empty($_POST['test'])) {
echo 'field set';
}
0 would be empty/blank with checking empty()
You can do this with empty() method.try with this code.
ex : echo !empty($_POST['test']) ? "field set" : "" ;
It will echo that you field is set because all you are checking for if is the array key test exists with isset(). As long as you have a field in your form with the name test, it will create an empty key, thus resulting in it being set.
What you would rather like is to see if the key is set, and if the key also is empty or not.
This you can do by:
if( isset( $key['test'] ) && $key['test'] !== "" ){
//Do something
}
I started with the code from simplemodal, and modified to suit my needs. That went amazingly well. The last issue I have is getting some radio buttons to post. I'll skip the code blocks I don't think are necessary and just show what I think is relevant. I've tried literally dozens of attempts of solutions from php.net, but nothing seems to work.
HTML
<label for='PayPlatform'>Are you willing to pay for a trading platform?</label>
<input type='radio' name='PayPlatform' value='Yes' tabindex='1001' />Yes
<input type='radio' name='PayPlatform' value='No' tabindex='1002' />No
Here's where I can't get the value and my attempt there are two attempts in the codeblock below, they were of course not attempted at the same time.
else if ($action == "send") {
// other elements form values are retrieved fine, just not the below
$PayPlatform = isset($_POST['PayPlatform']) ? ' checked="checked"' : "";
//the ternary above just submits 'checked="checked" no matter which radio is checked
//another attempt
$PayPlatform = isset($_POST["PayPlatform"]);
//this just submits "1" weather yes or no is checked
$token = isset($_POST["token"]) ? $_POST["token"] : "";
// make sure the token matches
if ($token === smcf_token($to)) {
smcf_send($name, $email, $subject, $phone, $message, $PayPlatform);
echo "Your message was successfully sent, you can close this window";
}
else {
echo "Unfortunately, your message could not be verified.";
}
}
$p = (isset($_POST['PayPlatform']) && $_POST['PayPlatform'] == 'Yes');
Boolean true if the yes radio is checked, and false otherwise.
Assume you have the following:
$a = 'Yes';
isset($a); //true
$b = 'No';
isset($b); //true
Strings are considered set, and as PsyCoder said, you'll receive the value values, not Booleans.
Also, on a different (chastise-y) note, this question could have been entirely avoided with a bit of better debugging. The first thing I would have done would have been to var_dump($_POST) and see what the value was. You'd have then seen that $_POST['PayPlatform'] was always Yes or No, and you'd have potentially realized that isset is always true on strings.
You wont get value as checked or unchecked... rather you'll receive the value='No' or value='No' parameter value specified in your html...
if($_POST["PayPlatform"] == "Yes") {
$PayPlatform = true;
}
Keeping it simple :)
I want to validate form fields when submitting the form, I use isset() to check if the field has a value or not. The problem I face that it returns true if the field has a value of does not !!!!!
here's HTML code
<form action="handler.php" method="post">
<label>userName: </label><input type="text" name="fname" /><br/>
</form>
and here's php code
<?php error_reporting(E_ALL);
if(validateForm()){?>
Welcome <?php echo $_POST["fname"]; ?>.<br />
}
function validateForm(){
$empty=false;
$empty=isset($_POST['fname'])?'true':'false';
echo 'empty>>>>'.$empty;
return $empty;
}
?>
What you want to use is empty :
function validateForm(){
return !empty($_POST['fname']);
}
edit : removed isset() as it wasn't necessary. Thanks to #Julian.
When your doing an input form, and the input textfield is empty, it's not NULL. isset checks if it's NULL or not. Returns true if not NULL and returns false if NULL.
I would check if it's empty, using empty(). It checks if the variable passed to it is empty or not. Returns true if empty, false if non empty.
Even if the field has no value, it is set with the value of "" (a string with the length 0). If you want to check if there is a value which is usable, either use empty() or check the length of the string.
use
isset($_POST['fname']) && !empty($_POST['fname'])
if not sure whats inside $_POST ,just print it
var_dump($_POST)