我试图从radioButton获得价值,但事情发生了错误

时间:2018-04-06 15:36:52

标签: php

我已经尝试了很多次以不同的方式来解决这个问题。每次它产生相同的输出。我在[http://google.com]和[http://youtube.com]中搜索并按照许多教程,并根据它们实现了我的代码。他们的代码运行正常,但我的代码提供了未知的输出,尽管他们的代码和我的代码是相同的。< / p>

这是PHP代码:

if(!isset($_POST["radio"])){
    $radioMsg="You must Select a Category!";
}
if(isset($_POST["radio"])){
    $radio=$_POST["radio"];
}

有收音机按钮

<form method="post" action="register.php" enctype="multipart/form-data" 
style="width: 860px;margin: 0px;height: 580px;">

    <input class="form-control" type="" name="name" placeholder="Name..."/ 
    style="width: 400px; margin-left: 200px" ><br>

    <input class="form-control" type="email" name="email" 
    placeholder="Email..."/ style="width: 400px; margin-left: 200px" ><br>

    <input class="form-control" type="password" name="password" 
    placeholder="Password"/ style="width: 400px; margin-left: 200px" ><br>

    <input class="form-control" type="password" name="cPassword" 
    placeholder=" 
    confirm Password..."/ style="width: 400px; margin-left: 200px" ><br>

    <input class="form-control" type="text" name="contactNO" 
    placeholder="Your 
    contact No.."/ style="width: 400px; margin-left: 200px" ><br>

    <input type="file" name="image"/ style="width: 400px; margin-left: 
    200px"> 

    <br><br>

    <div style="border: 1px solid #4C6A6D;width: 250px;margin-left: 200px" >
    <?php echo $radioMsg ?>

    <h6 style="margin-left: 20px">Select a Cetagory</h6> 

    <input type="radio" name="radio" value="customer" style="margin-left: 
    25px">Customer<br>

    <input type="radio" name="radio" value="DespensaryOwner" style="margin- 
    left: 
    25px">Despensary Owner<br>

    input type="radio" name="radio" value="dealer" style="margin-left: 
    25px">Dealer<br>

   </div>

   <br>

   <input class="btn-primary" type="submit" name="submit" value="Register" 
   style=" margin-left: 200px"><br><br>

</form>

如果选择radioButton1,则返回一个值&#34; radio&#34;而不是价值&#34;客户&#34;

2 个答案:

答案 0 :(得分:2)

输入类型不能是&#34; cust&#34;:

<input type="radio" name="g" value="customer" style="margin-left: 
25px">Customer<br>

<input type="radio" name="g" value="DespensaryOwner" style="margin-left: 
25px">Despensary Owner<br>

<input type="radio" name="g" value="dealer" style="margin-left: 
25px">Dealer<br>enter code here

尝试上面的代码并查看是否有效,您还应该使用本答案评论中建议的更好的名称值

答案 1 :(得分:0)

我继续在本地运行,将它全部放在PHP dev服务器实例的根目录中名为“register.php”的文件中,PHP代码位于顶部。在那种情况下,它会在输入有效值之前吐出错误,但我想你的处理方式不同。

我必须删除HTML中的一些奇怪的正斜杠和换行符,但是缺少&lt;和&gt;,你的单选按钮都被命名为“radio”,但在纠正这些错误后,下面的代码可以工作:

PHP: (包含完整性的PHP标记)

<?php
    if(!isset($_POST["category"])){
        $radioMsg="You must Select a Category!";
    }
    if(isset($_POST["category"])){
        $radioMsg="";
        $radio=$_POST["category"];
    }
?>

HTML: (为了完整性,包含整个html布局。)

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post" action="register.php" enctype="multipart/form-data" style="width: 860px;margin: 0px;height: 580px;">

        <input class="form-control" type="" name="name" placeholder="Name..." style="width: 400px; margin-left: 200px" ><br>

        <input class="form-control" type="email" name="email" placeholder="Email..." style="width: 400px; margin-left: 200px" ><br>

        <input class="form-control" type="password" name="password" placeholder="Password" style="width: 400px; margin-left: 200px" ><br>

        <input class="form-control" type="password" name="cPassword" placeholder="confirm Password..." style="width: 400px; margin-left: 200px" ><br>

        <input class="form-control" type="text" name="contactNO" placeholder="Your contact No.." style="width: 400px; margin-left: 200px" ><br>

        <input type="file" name="image" style="width: 400px; margin-left: 200px">

        <br><br>

        <div style="border: 1px solid #4C6A6D;width: 250px;margin-left: 200px" >
        <?php echo $radioMsg ?>

        <h6 style="margin-left: 20px">Select a Category</h6>

        <input type="radio" name="category" value="customer" style="margin-left: 25px">Customer<br>

        <input type="radio" name="category" value="DispensaryOwner" style="margin-left: 25px">Dispensary Owner<br>

        <input type="radio" name="category" value="dealer" style="margin-left: 25px">Dealer<br>

       </div>

       <br>

       <input class="btn-primary" type="submit" name="submit" value="Register"
       style=" margin-left: 200px"><br><br>

    </form>
</body>
</html>