使用带有其他文本字段的单选按钮将MySQL数据回显为html表单

时间:2015-03-02 16:27:01

标签: php html mysql sql

在我的网络表单中,用户可以将产品数据提交到MySQL数据库中。供应商字段为我们的用户提供了3个单选按钮(我们最常用的供应商是Sigma-Aldrich,VWR和Filter-Service)或带有文本字段的第4个“其他”单选按钮之间的选择,他们可以在其中指定“其他” '供应商。这是使用以下代码完成的:

php方面:

if (isset($_POST['submit'])) {
$q2_productname = $conn->real_escape_string($_POST['q2_productname']);
$q3_supplier = $conn->real_escape_string($_POST['q3_supplier']);
if ($q3_supplier =='other'){
  $q3_supplier = $conn->real_escape_string($_POST['q3_supplier_other']);
}

和html端的表单:

<fieldset>
<div>
<label for="q2"><font color="red">*</font> Full product name:</label><br />
<input id="q2" type="text" style="width:400px" name="q2_productname" required="true" tabindex="2">
</div>
</fieldset>
<fieldset>
<div>
<label for="q3"><font color="red">*</font> Supplier:</label><br />
<input id="q3" type="radio" name="q3_supplier" required="true" tabindex="3" value="Sigma-Aldrich" onchange="disableTxt()" checked="checked" />Sigma-Aldrich &nbsp;
<input id="q3" type="radio" name="q3_supplier" required="true" tabindex="3" value="VWR" onchange="disableTxt()" />VWR &nbsp;
<input id="q3" type="radio" name="q3_supplier" required="true" tabindex="3" value="Filter-Service" onchange="disableTxt()" />Filter-Service &nbsp;
<input id="q3" type="radio" name="q3_supplier" required="true" tabindex="3" value="other" onchange="enableTxt()" />Other:
<input id="other" type="text" name="q3_supplier_other" required="true" tabindex="3" disabled="disabled" />
<script>
function disableTxt() {
    document.getElementById("other").disabled = true;
}
function enableTxt() {
    document.getElementById("other").disabled = false;
}
</script>
</div>
</fieldset>

现在,我还有一个编辑页面,用户可以在其中编辑和更新条目。 对于普通文本字段,我设法将值回显到文本字段中。 对于供应商,我想将价值回显到与webform相同的布局(即检查相应的单选按钮,如果是'其他',则检查'其他'单选按钮并填写'其他'文本(),但这似乎相当困难。

我正在尝试

<input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="other" onchange="enableTxt()" <?php if (isset($q3_supplier) && $q3_supplier!="Sigma-Aldrich" OR "VWR" OR "Filter-Service") echo "checked";?> />Other:

检查另一个单选按钮,以防它与3个常用供应商之一不同,然后我也放在文本字段

<input id="other" type="text" name="ud_q3_supplier_other" required="true" tabindex="3" disabled="disabled" value="<?php if (isset($q3_supplier) && $q3_supplier!="Sigma-Aldrich" OR "VWR" OR "Filter-Service") echo $q3_supplier;?>" />

但结果是代码总是选择其他文本字段并填写文本字段中的值,无论它与Sigma-Aldrich,VWR或Filter-Service有什么不同......

我在想 OR语句无法正常工作,但我的google和stackoverflow搜索都没有找到解决方案。有没有人有解决这个问题的技巧?

编辑表格php的代码:

$sid = (int)$_GET['sid'];
$sql = "SELECT * FROM orders WHERE sid = '$sid'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $sid = $row['sid'];
        $q2_productname = $row['q2_productname'];
        $q3_supplier = $row['q3_supplier'];
    }
}

和html方:

<input type="hidden" name="sid" value="<?=$sid;?>"><br>
<font color="red">*</font> Product name: <input id="q2" type="text" style="width:400px" name="ud_q2_productname" required="true" value="<?=$q2_productname?>" tabindex="2"><br>
<font color="red">*</font> Supplier: 
<input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Sigma-Aldrich" onchange="disableTxt()" <?php if (isset($q3_supplier) && $q3_supplier=="Sigma-Aldrich") echo "checked";?> />Sigma-Aldrich &nbsp;
<input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="VWR" onchange="disableTxt()" <?php if (isset($q3_supplier) && $q3_supplier=="VWR") echo "checked";?> />VWR &nbsp;
<input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Filter-Service" onchange="disableTxt()" <?php if (isset($q3_supplier) && $q3_supplier=="Filter-Service") echo "checked";?> />Filter-Service &nbsp;
<input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="other" onchange="enableTxt()" <?php if (isset($q3_supplier) && $q3_supplier!="Sigma-Aldrich" OR "VWR" OR "Filter-Service") echo "checked";?> />Other:
<input id="other" type="text" name="ud_q3_supplier_other" required="true" tabindex="3" disabled="disabled" value="<?php if (isset($q3_supplier) && $q3_supplier!="Sigma-Aldrich" OR "VWR" OR "Filter-Service") echo $q3_supplier;?>" /><br />
<script>
function disableTxt() {
    document.getElementById("other").disabled = true;
}
function enableTxt() {
    document.getElementById("other").disabled = false;
}
</script>

2 个答案:

答案 0 :(得分:0)

我认为在打印单选按钮时你的if语句在这里是错误的。

非空字符串在布尔值中变为true。因此,$q3_supplier!="Sigma-Aldrich" OR "VWR" OR "Filter-Service"无论如何都会验证为true

您的条款应该是:$q3_supplier!="Sigma-Aldrich" OR $q3_supplier!="VWR" OR $q3_supplier!="Filter-Service"

请参阅PHP type comparison

答案 1 :(得分:0)

我想通过添加php if, elseif, else标签来做到这一点。我将每个if / elseif / else的checked="checked"放在相应的单选按钮上。 它就像一个魅力: - )。

我将在这里提供代码。 如果其他人想出更优雅的解决方案,请告诉我们!

<form action="update_script.php" method="post">
<input type="hidden" name="sid" value="<?=$sid;?>"><br>
<font color="red">*</font> Product name: <input id="q2" type="text" style="width:400px" name="ud_q2_productname" required="true" value="<?=$q2_productname?>" tabindex="2"><br>
<?php
if($q3_supplier == 'Sigma-Aldrich'){ ?>
<font color="red">*</font> Supplier: 
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Sigma-Aldrich" onchange="disableTxt()" checked="checked" />Sigma-Aldrich &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="VWR" onchange="disableTxt()" />VWR &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Filter-Service" onchange="disableTxt()" />Filter-Service &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="other" onchange="enableTxt()" />Other:
    <input id="other" type="text" name="ud_q3_supplier_other" required="true" tabindex="3" disabled="disabled" /><br />
    <script>
    function disableTxt() {
        document.getElementById("other").disabled = true;
    }
    function enableTxt() {
        document.getElementById("other").disabled = false;
    }
    </script>
<?php
} elseif($q3_supplier == 'VWR'){ ?>
<font color="red">*</font> Supplier: 
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Sigma-Aldrich" onchange="disableTxt()" />Sigma-Aldrich &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="VWR" onchange="disableTxt()" checked="checked" />VWR &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Filter-Service" onchange="disableTxt()" />Filter-Service &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="other" onchange="enableTxt()" />Other:
    <input id="other" type="text" name="ud_q3_supplier_other" required="true" tabindex="3" disabled="disabled" /><br />
    <script>
    function disableTxt() {
        document.getElementById("other").disabled = true;
    }
    function enableTxt() {
        document.getElementById("other").disabled = false;
    }
    </script>
<?php
} elseif($q3_supplier == 'Filter-Service'){ ?>
<font color="red">*</font> Supplier: 
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Sigma-Aldrich" onchange="disableTxt()" />Sigma-Aldrich &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="VWR" onchange="disableTxt()" />VWR &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Filter-Service" onchange="disableTxt()" checked="checked" />Filter-Service &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="other" onchange="enableTxt()" />Other:
    <input id="other" type="text" name="ud_q3_supplier_other" required="true" tabindex="3" disabled="disabled" /><br />
    <script>
    function disableTxt() {
        document.getElementById("other").disabled = true;
    }
    function enableTxt() {
        document.getElementById("other").disabled = false;
    }
    </script>
<?php
} else { ?>
<font color="red">*</font> Supplier: 
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Sigma-Aldrich" onchange="disableTxt()" />Sigma-Aldrich &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="VWR" onchange="disableTxt()" />VWR &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="Filter-Service" onchange="disableTxt()" />Filter-Service &nbsp;
    <input id="q3" type="radio" name="ud_q3_supplier" required="true" tabindex="3" value="other" onchange="enableTxt()" checked="checked"/>Other:
    <input id="other" type="text" name="ud_q3_supplier_other" required="true" tabindex="3" enabled="enabled" value="<?php echo $q3_supplier;?>" /><br />
    <script>
    function disableTxt() {
        document.getElementById("other").disabled = true;
    }
    function enableTxt() {
        document.getElementById("other").disabled = false;
    }
    </script>
<?php
} ?>