dongnai2804 2015-02-05 21:42
浏览 42
已采纳

在带有单选按钮和“其他”文本字段的HTML表单中,无法理解单选按钮的值

I'm trying to create a HTML form which inserts all entered values into a MySQL database through php. Specifically for this case, the user enters a product number and a supplier.

The supplier should be defined by radio buttons and an additional 'other' text field where the user can specify another option... When the user chooses the 'other' option, the value typed there is inserted in the MySQL DB correctly, however when choosing one of the radio buttons, not the supplier is inserted but the text 'on'... I don't understand why the script does this, since I don't have the value 'on' anywhere in the script.

I've searched google and stackoverflow, and tried many things, but I'm still failing to get it to work. I'm a php and html newb so please go easy on me.

Below is a trimmed down version of the code. (In real, the script also submits more fields from the form, the session username, and a timestamp.)

Does anyone see where the 'on' may come from and how to solve this? Thank you.

The php part of the code:

<?php
    if (isset($_POST['submit'])) {
    // These set your initial variables. q1_* is for the queried entries in the form
    $q2_productname = $_POST['q2_productname'];
    $q3_supplier = $_POST['q3_supplier'];
    if ($q3_supplier =='other'){
    $q3_supplier = $_POST['q3_supplier_other'];
    }               


    if (isset($q2_productname)) {
    $q2_productname = trim($q2_productname);
    $q2_productname = strip_tags($q2_productname);
    $q2_productname = stripslashes($q2_productname);
    $q2_productname = htmlspecialchars($q2_productname);
    }
    if (isset($q3_supplier)) {
    $q3_supplier = trim($q3_supplier);
    $q3_supplier = strip_tags($q3_supplier);
    $q3_supplier = stripslashes($q3_supplier);
    $q3_supplier = htmlspecialchars($q3_supplier);
    }


    $dbc = @mysql_connect (DBHOST, DBUSER, DBPASS) or die('Failure: ' . mysql_error() );
    mysql_select_db(DBNAME) or die ('Could not select database: ' . mysql_error() );

    $query = "INSERT INTO orders VALUES ('$q2_productname','$q3_supplier')";
    $q = mysql_query($query);

    if (!$q) {
    exit("<p>MySQL Insertion failure.</p>");
    } else {
    mysql_close();
}

Then the html/echo/script part of the code:

<?php 
    if (isset($_POST['submit'])) {
    echo "<p style='padding: .5em; border: 2px solid red;'>Thank you. Your product has been added to the order list!</p>";
    }
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<fieldset>
<div>
<label for="q2">Full product name:</label><br />
<textarea id="q2" name="q2_productname" rows="1" cols="50"></textarea>
</div>
</fieldset><br /><br />

<fieldset>
<div>
<label for="q3">Supplier (e.g. Sigma-Aldrich, VWR, ...):</label><br />

<input type="radio" name="q3_supplier" id="q3" onchange="disableTxt()" checked="checked" />Sigma-Aldrich 
    <input type="radio" name="q3_supplier" id="q3" onchange="disableTxt()" />VWR 
    <input type="radio" name="q3_supplier" id="q3" onchange="enableTxt()" />Other:
<input type="text" name="q3_supplier_other" id="other" disabled="disabled" />

<script>
    function disableTxt() {
        document.getElementById("other").disabled = true;
    }
    function enableTxt() {
        document.getElementById("other").disabled = false;
    }
</script>

</div>
</fieldset><br /><br />
</form>
  • 写回答

2条回答 默认 最新

  • doufan3958 2015-02-05 21:46
    关注

    Set the value attribute of your radio inputs. This value will be posted to your server. The value will be 'on' unless you specify it.

    <input type="radio" name="q3_supplier" id="q3" onchange="disableTxt()" checked="checked" value='Sigma-Aldrich'/>Sigma-Aldrich 
    <input type="radio" name="q3_supplier" id="q3" onchange="disableTxt()" value='VWR' />VWR 
    <input type="radio" name="q3_supplier" id="q3" onchange="enableTxt()" value='Other' />Other:
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler