表单在php中提交两次

时间:2014-09-29 08:00:19

标签: javascript php wordpress forms

我有两个新患者的预约表格,已分别注册患者, 手机号码用作主键。

因此,在新的患者登记中,我们检查该移动设备是否已经存在,如果是,则显示错误,否则将新数据输入表中。

然而,目前即使我输入一个新的手机号码,它也会显示已经存在的#34;错误并将数据输入db。

所以我认为表格提交了两次,但我无法弄清楚这种情况发生了什么。

我的php文件代码片段是:

if ($_POST['isnewpatient'] == "true") {

    @$name = mysql_real_escape_string(trim($_POST['aaptntname']));
    @$email = mysql_real_escape_string(trim($_POST['emlid']));
    @$mobile = mysql_real_escape_string(trim($_POST['mobile']));
    $qqcSql = "select * from " . WP_eemail_TABLE_SUB 
      . " where eemail_mobile_sub ='" . trim($_POST['mobile']) 
      . "' OR eemail_patient_id ='" . trim($_POST['mobile']) . "'";

    $qqdata1 = $wpdb->get_results($qqcSql);
    var_dump($qqdata1);

    if (!empty($qqdata1)) {

        $err = 1;
        echo "<div id='message' class='aerror'>Already patient details exists. Use your existing patient ID !</div>\n";

    } 
    else {
        $pt_id = mysql_real_escape_string(trim($_POST['mobile']));
        $sql = "insert query to WP_Appointment";

        $wpdb->get_results($sql);

        $sqls = "insert query to WP_Appointment_Contact";

        $wpdb->get_results($sqls);

        $sqql = " insert query to table WP_eemail_Table_Sub";

        $wpdb->get_results($sqql);

        echo "<div id='message' class='asuccess'>Request has been sent for appointment  </div>";

    }
} 
 else {

   // Already registered patient form

 }

<form name="FormEdit" action="<?php echo the_permalink(); ?>" method="post" onsubmit="return p_appointment()" class="aform">

   /* Form part */  
</form>

javascript部分

function p_appointment()
{ 
    if($('input:radio[name=new_patient]:checked').val() == "new") 
        document.FormEdit.isnewpatient.value = "true";
    if($('input:radio[name=new_patient]:checked').val() == "old") 
        document.FormEdit.isnewpatient.value = "false";  

    document.FormEdit.appsmssend.value = "true";
    document.FormEdit.appemailsend.value = "true";

}

1 个答案:

答案 0 :(得分:0)

将您的js方法更改为:

function p_appointment()
{ 
    if($('input:radio[name=new_patient]:checked').val() == "new") 
        document.FormEdit.isnewpatient.value = true;
    if($('input:radio[name=new_patient]:checked').val() == "old") 
        document.FormEdit.isnewpatient.value = false;  


  // document.FormEdit.appsmssend.value = "true";
   // document.FormEdit.appemailsend.value = "true"; commented because i did not added these field while testing.
    return document.FormEdit.isnewpatient.value;

}

这就是我用作形式的内容:

<form name="FormEdit"  method="post" onsubmit="return p_appointment()" class="aform">
<input type="text" name="name"/>
<input type="submit" value="submit"/>
</form>

不要使用:

if ($_POST['isnewpatient'] == "true") {

}
if ($_POST['isnewpatient'] == "false") {

}

使用:

if($_POST['isnewpatient']) {

}else {

}