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:
    

    打赏 评论
  • douzao9845 2015-02-05 21:48

    The default value of input[type=radio] is on.

    I suggest you learn how to use Firebug or Chrome dev tools, in order to actually see what is really submitted and what comes back from the server.

    打赏 评论

相关推荐 更多相似问题