我有一个通过 PHP 发送到 MySQL 数据库的 HTML 表单。在我输入 JavaScript 以仅根据用户响应显示某些字段之前,该表单可以正常工作并保存得很好。大多数字段在开始时被禁用,然后在选择/输入某些响应后启用。
文本输入字段仍然保存得很好,但是,需要选择选项的字段,例如单选框和复选框(其中混合了值,包括我使用 TinyInt 数据类型的布尔值和我使用 VarChar 数据类型的文本)不保存任何内容.
有谁知道为什么?或者对如何找出导致问题的原因有任何建议?我必须强调,在 HTML 级别禁用和启用输入可以正常工作。问题与 JavaScript 或 PHP 有关。
以下代码是我一直在使用的 JavaScript 结构/格式的示例,发布此代码段的原因是因为它是我拥有的最简单的代码:
<script>
$(function () {
$("#wq_7_host_0, #wq_7_host_1").change(function () {
$("#wq_7_hostprov, #wq_7_hostprice, #wq_7_email_0, #wq_7_email_1, #wq_7_email_2").val("").attr("disabled", true);
if ($("#wq_7_host_0").is(":checked")) {
$("#wq_7_hostprov, #wq_7_hostprice, #wq_7_email_0, #wq_7_email_1, #wq_7_email_2").removeAttr("disabled");
$("#wq_7_hostprov").focus();
} else if ($("#wq_7_host_1").is(":checked")) {
$("#wq_7_email_0, #wq_7_email_1, #wq_7_email_2").removeAttr("disabled");
$("#wq_7_email_1").focus();
}
});
});
</script>
我的PHP:
<?php
// Database connection string
$dbConn = mysql_connect("info", "info", "info")
or die("Could not connect: " + mysql_error());
mysql_select_db("info", $dbConn)
or die("Could not find database: " + mysql_error());
// create variables from informatin passed from the form
$wq__0_formid = $_POST['wq__0_formid'];
$wq_0_date = $_POST['wq_0_date'];
$wq_0_prona = $_POST['wq_0_prona'];
$wq_1_conna = $_POST['wq_1_conna'];
$wq_1_comna = $_POST['wq_1_comna'];
$wq_1_no = $_POST['wq_1_no'];
ETC
//if no email exists, add the user to the database //Left values are database names and right are variables declared above
mysql_query("INSERT INTO webform
(wq__0_formid, wq_0_date, wq_0_prona, wq_1_conna, wq_1_comna, wq_1_no, wq_1_str, wq_1_tow, wq_1_cit, wq_1_coun, wq_1_pos, wq_1_conno, wq_1_eadd, wq_1_trad, wq_1_tradlo_years, wq_1_tradlo_months, wq_1_tradda, wq_2_web, wq_2_webadd, ETC) VALUES('$wq__0_formid', '$wq_0_date', '$wq_0_prona', '$wq_1_conna', '$wq_1_comna', '$wq_1_no', '$wq_1_str', '$wq_1_tow', '$wq_1_cit', '$wq_1_coun', '$wq_1_pos', '$wq_1_conno', '$wq_1_eadd', '$wq_1_trad', '$wq_1_tradlo_years', '$wq_1_tradlo_months', '$wq_1_tradda', '$wq_2_web', '$wq_2_webadd' ETC)")
or die(mysql_error());
//email customer to let them know login was successful and verify details
$to = "$wq_1_eadd";
$subject = "subject";
$body = "Hi $wq_1_conna,
Message";
if (mail($to, $subject, $body)) {
?>
<script language="javascript" type="text/javascript">
alert('Thanks <?PHP echo $wq_1_conna ?>. We will review the information you have provided and contact you shortly.');
window.location = 'index.html';
</script>
<?php }
else
{
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, get in touch with us via email address.');
window.location = 'index.html';
</script>
<?php
}
?>