简体   繁体   English

PHP中的单选按钮值

[英]radio button value in php

I'm trying to make a simple survey in php 我正在尝试用php做一个简单的调查

I have a set of radio buttons on a page called sja.php that sends its to sjamail.php page 我在名为sja.php的页面上有一组单选按钮,可将其发送到sjamail.php页面

the problem is that when I go to get 问题是当我去得到

$answer = $_POST['ans'];

I can't seen to do anything like 我看不到做任何事情

echo "$answer";

but if I were to throw some logic at it 但是如果我要对此提出一些逻辑

like

    if ($answer == "ans1") {

        echo 'Correct';
    }

   else {

       echo 'Incorrect';
    }    

It will display correct or incorrect (edit: The if/else works correctly and will display the correct answer ) 它将显示正确或不正确(编辑:if / else可以正确运行,并显示正确答案)

so why is it I can't access the value of the radio button "ans" as a string? 那么为什么我不能以字符串形式访问单选按钮“ ans”的值?

http://www.markonsolutions.com/sja.php http://www.markonsolutions.com/sja.php

print_r($_POST); will return Array ( [ans] => ) 将返回Array ( [ans] => )

Perhaps the value is something other than text. 值可能不是文本。

Try 尝试

var_dump($answer);

or 要么

print_r($answer, TRUE);

You need to make sure that the field in HTML has... 您需要确保HTML中的字段具有...

<input type="radio" name="ans" value="ans1" />
<input type="radio" name="ans" value="ans2" />

Also make sure your form method is POST 还要确保您的表单方法是POST

Your page works correctly if you select any of the first 4 radio buttons (ans1/2/3/4). 如果选择前四个单选按钮(ans1 / 2/3/4)中的任何一个,则页面将正常工作。 But the rest of the radio buttons next to all those images have blank values, which would explain why your posted value is empty if you selected any of those to test with. 但是所有这些图像旁边的其余单选按钮均具有空白值,这可以解释为什么如果选择其中任何一个进行测试,则您发布的值将为空。

Try this: 尝试这个:

$answer = (string)$_POST["ans"];
echo $answer;

You must convert $_POST["ans"] to string. 您必须将$_POST["ans"]转换为字符串。

I had a similar problem with the following: 我在以下方面遇到了类似的问题:

     <input name="03 - Gender" type="radio" value="Masculino"/>Male<br/>
     <input name="03 - Gender" type="radio" value="Femenino" required="required"/>Female <br/>
     <input type="hidden" name="03 - Gender" value=""/>

but when I removed the third input line (the hidden one) the problem desapeared. 但是当我删除第三条输入线(隐藏的那条)时,问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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