HTML表单不保存WERE禁用的检查或无线电字段

时间:2013-04-11 07:26:14

标签: php javascript html mysql forms

我有一个HTML表单,通过PHP发送到MySQL数据库。表单工作并保存很好,直到我输入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
  }
 ?>

1 个答案:

答案 0 :(得分:1)

请参阅this article,您会理解 我认为你必须在JS中禁用输入,因为不会提交禁用的输入元素 如果它还没有工作,添加一个隐藏的输入,你将使用JS填充,具体取决于无线电选择。

[编辑]

在Jquery中,您可以使用此方法执行此操作:

var postData = new Array();
$('#myform').find('input:text,input:radio:checked, input:checkbox:checked').each(function() {
    postData.push({$(this).attr('name') : $(this).val()});
});

[新]

您可以将postData行更改为:请参阅SO topic

var postData = {};
$('#myform').find('input:text,input:radio:checked, input:checkbox:checked').each(function() {
    postData[$(this).attr('name')] = $(this).val();
});

然后,调用Ajax方法并发送postData数组。

[FIDDLE]

在你的小提琴中,你想要使用postData数组。我添加(here)ajax调用以将postData发送到PHP脚本 当然,您必须更改url ans success等一些参数。但这将是一个好方法。

[RESPONSE]

实际上,问题出在数据库结构(没有默认值)和PHP查询中。您必须为每列添加默认值,并且在PHP中,您必须定义要使用的所有变量。