带有“其他"标记的html单选按钮具有文本框的选择 [英] html radio button with an "other" selection that has a text box
问题描述
当我的插入程序运行时,它将在文本框中写入文本,但是如果我选择单选按钮而不是其他",则不会在表中插入任何内容. 如果我删除其他"代码,则选中的单选按钮将被写入表格.
When my insert runs it will write the text in the text box but if I choose a radio button selection other than "other" nothing is inserted into the table. If I remove the "other"code the radio button selected is written to the table.
这是代码
<input type="radio" name="job" value="PHP Programmer">PHP Programmer
<input type="radio" name="job" value="SQL Programmer">SQL Programmer<br>
<input type="radio" name="job">Other <input type="text" name="job" >
<h3>* If yes, check which ISO standard(s) you are accredited?</h3>
<input type="radio" name="iso_standard" value="17020">17020
<input type="radio" name="iso_standard" value="17025">17025
<br />
<input action="submit" type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['job']) && isset($_POST['iso_standard']))
{
$job = mysqli_real_escape_string($db, $_POST['job']);
$iso_standard =mysqli_real_escape_string($db, $_POST['iso_standard']);
$sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$job', '$iso_standard')";
if(!mysqli_query($db, $sql))
{
die('Error: ' .mysqli_error($db));
}
echo "1 record added";
}
else
{
echo "You didn't choose all the options!
}
?>
推荐答案
您正在用文本输入字段覆盖job的值.不管有没有值,它都不会计算.
You are overwriting the value of job with the text input field. It does not mather if it has any value or not.
请将您的<input type="text" name="job" >
重命名为例如otherJob
,您应该没事
Please rename your <input type="text" name="job" >
to for example otherJob
and you should be fine
更改<input type="radio" name="job">Other <input type="text" name="job" >
到<input type="radio" name="job" value="other">Other <input type="text" name="otherJob">
更改$job = mysqli_real_escape_string($db, $_POST['job']);
到
$job = mysqli_real_escape_string($db, $_POST['job']);
$otherJob = mysqli_real_escape_string($db, $_POST['otherJob']);
if ($job == 'other') {
$jobField = $otherJob;
} else {
$jobField = $job;
}
并更改$sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$job', '$iso_standard')";
and change $sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$job', '$iso_standard')";
到$sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$jobField', '$iso_standard')";
to $sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$jobField', '$iso_standard')";
这篇关于带有“其他"标记的html单选按钮具有文本框的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!