简体   繁体   中英

form submitting twice in php

I have two appointment forms for new patients and already registered patients respectively, mobile number is used as primary key.

So in the new patient registration we check if that mobile is already present, if yes then shows an error otherwise enter the new data into the tables.

However at present even when I enter a new mobile number it shows "already present" error and also enters the data into db.

so I thought the form was submitting 2 times, but i can't figure out at where this is happening.

my php file code snippet is:

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>

The javascript part

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";

}

change your js method to this:

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;

}

this is what i used as form:

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

do not use:

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

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

}

use:

if($_POST['isnewpatient']) {

}else {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM