Cant get radio button value to post in php - php

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 :)

Related

If i load the checkboxes from database, and i uncheck the checkbox after submit it is still checked

The unchecking and checking after submit works if i don't use this line of code to load the data everything works. echo ($extraServices == 'Park') ? "checked='checked'" : (($extraServices == 'Park,Electricity') ?
<input type="checkbox" name="services[]" value="Park" <?php
echo ($extraServices == 'Park') ? "checked='checked'" : (($extraServices == 'Park,Electricity') ? "checked='checked'" : "");
if(isset($_POST['services'])) {
if(in_array("Park", $_POST['services'])) {
echo "checked='checked'";
}
}
?>> Park
So now if use echo "checked='checked'"; and echo ($extraServices == 'Park') ? "checked='checked'" : (($extraServices == 'Park,Electricity') ? "checked='checked'" : "");
together. It doesn't work. So with this code it will load the data from the database on which checkbox was checked. That works. But if i uncheck a checkbox it wont be unchecked after submit. How can i solve this? I thought about a way to detect if i load this page for the first time. But then i bumped into the problem that that was for loading the page the first time EVER.
I wanted to come up with something to detect if the page loads and loads the data. After that it doesn't need to load the data from the database. But i don't know how i could do this.
I dont know what to do now, hope that someone got some idea's?
Thanks for your help.
It is easier to code complex if this or that or the other type tests as a simple piece of php rather than trying to bury the tests into some html. That becomes unmaintainable very quickly. It is also not recommended to string ternary tests together.
So create an empty variable and then fill it with the checked=checked if the rules say it should be checked
<?php
$chk = '';
// here are the rules for setting the checked attribute
if ( ($extraServices == 'Park') || ($extraServices == 'Park,Electricity') ) {
$chk = 'checked=checked';
}
?>
<input type="checkbox" name="services[]" value="Park" <?php echo $chk;?> >
So the HTML will add $chk always, but if its not required, $chk will simply be empty :)
If I missed something this at least should set you on the right path. Post a comment below if you need anything explaining in any more detail

chaining if else statements

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!

MySQL storing boolean - 1, 0 , or NULL

So I've generally stored Boolean values in MyIASM MySQL DBs using TinytInt(1). In a site I'm currently working on, I need to be able to store 1, 0 or NULL. This field is being populated from a radio button in a PHP form.
When I choose 'Yes' in the form (with a value of 1), this gets stored accurately. When I choose 'No' however (with a value of 0), it gets stored as NULL.
I want to reserve NULL for if the user chooses neither 'Yes' or 'No'.
Any idea why the 'No' (0) values aren't storing as expected?
EDIT:
Here's the basic HTML:
Yes <input type='radio' name='video_transfer_dvd_question' value='1' />
No <input type='radio' name='video_transfer_dvd_question' value='0' />
In PHP, both '' and 0 are considered FALSE in a boolean context, so if you try to test '' == 0 you'll find the result is TRUE. And empty() only checks whether the argument is boolean FALSE, so it doesn't truly distinguish between an empty $_POST and a $_POST which contains a value that happens to evaluate as FALSE. My suggestion would be to change the values of your radio buttons so than they never evaluate as boolean FALSE, e.g.:
Yes <input type='radio' name='video_transfer_dvd_question' value='yes' />
No <input type='radio' name='video_transfer_dvd_question' value='no' />
This keeps things more explicit when you test the response and choose a value to send to MySQL:
if (empty($_POST['button'])) {
$value = NULL;
} elseif ($_POST['button'] == 'no') {
$value = 0;
} elseif ($_POST['button'] == 'yes') {
$value = 1;
}
You could also use !$_POST['button'] in place of empty($_POST['button']), but that would generate a warning if $_POST['button'] is not set, which you would probably rather not happen.
You could also change that last elseif block to a simple else, if you have no reason to expect that the possible responses to this radio button will ever change and don't care about being explicit.
As the other people have commented its 99% an issue with your code...what you will need is something like...
if ($_POST["radiobutton"]) { $insertValue = 1; } else { $insertValue = 0; }
or
$insertValue = ($_POST["radiobutton"]) ? 1 : 0;
The reason is an empty radio button is an empty $POST var value which gets interpreted as NULL in mysql.

php - check an empty string with === '' does not work

When I test to see if the textarea in my form is empty to do a redirect so it doesn't submit it in php, it doesn't work.
The textarea is named $_POST['message'], I know the variable exists because if I do this statement;
if (isset($_POST['message'])) {
header('Location:/');
exit();
}
Then it always redirects back to the index page so the variable must exist, although if I do this;
if (empty($_POST['message'])) {
header('Location:/');
exit();
}
It does not work, also tried with all three combos of =/==/===
if ($_POST['message'] === '') {
header('Location:/');
exit();
}
And also...
if (empty(trim($_POST['message']))) {
header('Location:/');
exit();
}
Any ideas why this is happening? And how I can prevent it, I really need to stop it as I do not want empty values in my mysql table.
I did some research and it seems some other people have had this problem, but I have seen no answer as of yet.
You probably have some whitespaces in the string, which isn't stripped by trim().
Do a strlen() on it to see what's up, and then log it byte by byte (http://stackoverflow.com/questions/591446/how-do-i-get-the-byte-values-of-a-string-in-php).
One thing you could think about is to make sure your textarea doesn't have any content in the markup (spaces, linkebreaks, whatever), like this:
<textarea></textarea>
I'm pretty sure your last try would work if you'd do it correctly, this:
if (empty(trim($_POST['message']))) {
// ...
}
...is a syntax error. empty is a language construct and does not accept expressions. Try:
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
if (empty($message)) {
// $_POST['message'] is empty
}
This will ignore all input lengths below 3 chars:
if (isset($_POST['message']) && strlen(trim($_POST['message'])) < 3) {
header('Location:/');
exit();
}
However, if you just want to check, if a form was submitted, ask for the submit-button:
<input type="submit" name="submit" value="send" />
the php code would be
if (!array_key_exists('submit', $_POST)) {
header('Location:/');
exit();
}

checkbox status "checked" by default problem

I have a page with search form on it and table with search results below. In search form i have checkbox "Search in this category". What i'm doing to check it by default :
if(!isset($_SESSION['inthiscat'])){
$_SESSION['inthiscat'] = 'on' ;
$checked = 'checked';
}
$_GET['inthiscat'] = $_SESSION['inthiscat'];
checkbox code : INPUT type="checkbox" name="inthiscat"<?=$checked?>.
Link to next page of results index.php?inthiscat=$_GET['inthiscat'].
So the problem is when i uncheck "Search in this category" its still checked when i going to next page of results. How to fix it and what i'm doing wrong? Session startet of course.
Firstly, do you really need SESSION variables for this? If you want box to be checked when GET parameter is not specified, you do not need SESSIONs at all.
Assuming you want to preserve the behaviour in case someone removes the GET parameter:
<?php
session_start();
//......
//......
$checked='checked';
if(isset($_REQUEST['inthiscat'])) {
// Form input and url GET parameters take precedence
if($_REQUEST['inthiscat'] != 'checked') { $checked = ''; };
} else if(isset($_SESSION['inthiscat'])) {
// Next, use session variable if it exists
if($_SESSION['inthiscat'] != 'checked') { $checked = ''; };
};
$_SESSION['inthiscat']=$checked;
?>
Note:
1) Assigning values to GET array is not a good practice.
2) I assume you are using correct syntax for your FORM submit.
3) IMO, you could remove the SESSION variable as you are explicitly passing as GET parameter in the subsequent urls. Or dont use the GET parameter in urls.
Problem is: when you uncheck the checkbox and go to the next page, $_SESSION['inthiscat'] will still be unset - where did you change it?
Here is the code:
if (isset($_GET['inthiscat'])) {
$_SESSION['inthiscat'] = $_GET['inthiscat'];
}
if (!isset($_SESSION['inthiscat'])) {
$checked = 'checked';
} else {
if ($_SESSION['inthiscat'] == 'on') {
$checked = 'checked';
} else {
$cheked = '';
}
}
Assuming this HTML: <INPUT type="checkbox" name="inthiscat" checked="<?=$checked?>" value="on" />
So what it does is:
Looks for the GET data and, if there is, assigns it (can be 'on' or '') to the SESSION;
If there is no SESSION (that means, no GET as well) it's the first page of that kind the user visits, so checked;
If there is a SESSION for inthiscat, it means it's not the first page and GET data has been assigned to the SESSION. So, if it's on, it displays the mark; else, it does not.

Categories