简体   繁体   中英

Cant get radio button value to post in 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.

您不会获得选中或未选中的value='No' ...而是您将收到在HTML中指定的value='No'value='No'参数值...

if($_POST["PayPlatform"] == "Yes") {
    $PayPlatform = true;
}

Keeping it simple :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM