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 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大