2017-05-13 59 views
0

我是這個PHP編程的初學者,所以希望任何人在更新我的代碼時提交代碼,當按下提交頁面刷新我不想刷新。使用AJAX提交PHP代碼

請有人把ajax提交到這個代碼。

 <div class="tab-pane" id="email"> 
<?php 
require_once ('includes/emailchange.php'); 
?> 
<? echo $Saved2;?> 
<form action="" method="POST" class="form-horizontal"> 
    <div class="form-group"> 
     <label for="inputExperience" class="col-sm-3 control-label">Security Code:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="SecurityCode" id="inputName" placeholder="Security Code"> 
     </div> 
    </div> 
<? echo $SecurityCode_error;?> 
    <div class="form-group"> 
     <label for="inputName" class="col-sm-3 control-label">Security Question</label> 
     <div class="col-sm-9"> 
      <select name="SecurityQuestion" class="form-control"> 
       <option selected="selected" value="empty">Please Select</option> 
       <option value="What city were you born in?">What city were you born in?</option> 
       <option value="Who is your favorite person?">Who is your favorite person?</option> 
       <option value="What was your nickname?">What was your nickname?</option> 
       <option value="What's your favorite food?">What's your favorite food?</option> 
       <option value="What is your pet's name?">What is your pet's name?</option> 
       <option value="Who is your super hero?">Who is your super hero?</option> 
       <option value="What is your favorite sport?">What is your favorite sport?</option> 
       <option value="Who was your first teacher?">Who was your first teacher?</option> 
       <option value="What is your hobby?">What is your hobby?</option> 
       <option value="What was your first job?">What was your first job?</option> 
      </select> 
     </div> 
    </div> 
<? echo $SecurityQuestion_error; ?> 
    <div class="form-group"> 
     <label for="inputExperience" class="col-sm-3 control-label">Security Answer:</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="SecurityAnswer" id="inputName" placeholder="Security Answer"> 
     </div> 
    </div> 
<? echo $SecurityAnswer_error;?> 
    <div class="form-group"> 
     <label for="inputName" class="col-sm-3 control-label">New E-mail Address:</label> 
     <div class="col-sm-9"> 
      <input type="email" class="form-control" name="NewEmail1" id="inputName" placeholder="[email protected]"> 
     </div> 
    </div> 
<? echo $NewEmail1_error;?> 
    <div class="form-group"> 
     <label for="inputName" class="col-sm-3 control-label">Verify E-mail Address:</label> 
     <div class="col-sm-9"> 
      <input type="email" class="form-control" name="NewEmail2" id="inputName" placeholder="[email protected]"> 
     </div> 
    </div> 
<? echo $NewEmail2_error;?> 
    <div class="form-group"> 
     <label for="inputName" class="col-sm-3 control-label">Captcha:</label> 
     <div class="col-sm-9"> 
      <style> 
       .label-new { 
        position: relative; 
        top: 0; 
        padding: 10px 15px; 
        font-size: 17px; 
        color: #fbfbfb; 
        background-color: #212528 !important; 
        display: inline-block; 
        border-radius: 3px; 
        width: auto; 
        z-index: 2; 
        line-height: .5; 
       } 
      </style> 
      <h5 class="red-text label-new"><?php echo $new_string; ?></h5> 
      <input type="hidden" name="Captcha" value="<?php echo $new_string; ?>"> 
      <input type="text" class="form-control" name="Captcha" placeholder="Captcha"> 
     </div> 
    </div> 
<? echo $Captcha_error; ?> 
    <div class="form-group"> 
     <div class="col-sm-offset-3 col-sm-9"> 
      <input type="submit" name="changeemail" class="btn btn-danger" Value="Submit"> 
     </div> 
    </div> 
</form> 
        </div><!-- /.tab-pane --> 

,這是emailchange.php

<?php 
if (isset($_POST['changeemail'])) { 
$SecurityCode=$_POST["SecurityCode"]; 
$SecurityQuestion=$_POST["SecurityQuestion"]; 
$SecurityAnswer=$_POST["SecurityAnswer"]; 
$NewEmail1=$_POST["NewEmail1"]; 
$NewEmail2=$_POST["NewEmail2"]; 
$Captcha = $_POST['Captcha']; 
function check_email_address($NewEmail1) 
{ 
    // First, we check that there's one @ symbol, 
    // and that the lengths are right. 

if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $NewEmail1)) 
{ 
    // NewEmail1 invalid because wrong number of characters 
    // in one section or wrong number of @ symbols. 

return false; 

} 
    // Split it into sections to make life easier 

$email_array = explode("@", $NewEmail1); 

$local_array = explode(".", $email_array[0]); 

for 
($i = 0; $i < sizeof($local_array); $i++) 
{ 

if 
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%& 
?'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", 
$local_array[$i])) 
{ 

return false; 

} 

} 
} 
if(check_email_address($NewEmail1)) 
{ 
    $NewEmail1_error = '<font color="#FF0000">The New E-mail Address field is not a valid e-mail address.</font>'; 
    $error .= '1'; 
} 
if(check_email_address($NewEmail2)) 
{ 
    $NewEmail2_error = '<font color="#FF0000">The Verify E-mail Address field is not a valid e-mail address.</font>'; 
    $error .= '1'; 
} 
if ($NewEmail1 != $NewEmail2) 
{ 
    $NewEmail1_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        The New E-mail Address is Not Matched With The Verify E-mail Address. 
        </div>'; 
    $error .= '1'; 
      $NewEmail2_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        The Verify E-mail Address is Not Matched With The New E-mail Address. 
        </div>'; 
    $error .= '1'; 
} 
if(empty($NewEmail1)) 
{ 
    $NewEmail1_error = '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        New E-mail Address is Empty. 
        </div>'; 
    $error .= '1'; 
} 
if(empty($NewEmail2)) 
{ 
    $NewEmail2_error = '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Verify E-mail Address is Empty. 
        </div>'; 
    $error .= '1'; 
} 
if ($SecurityCode != $data['SecurityCode']) 
{ 
    $SecurityCode_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Security Code is Wrong. 
        </div>'; 
    $error .= '1'; 
} 
if(empty($SecurityCode)) 
{ 
    $SecurityCode_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Security Code is Empty. 
        </div>'; 
    $error .= '1'; 
} 
if ($SecurityQuestion != $data['SecurityQuestion']) 
{ 
    $SecurityQuestion_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Security Question is Wrong. 
        </div>'; 
    $error .= '1'; 
} 
if(empty($SecurityQuestion)) 
{ 
    $SecurityQuestion_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Security Question is Empty. 
        </div>'; 
    $error .= '1'; 
} 
    if ($SecurityAnswer != $data['SecurityAnswer']) 
{ 
    $SecurityAnswer_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Security Answer is Wrong. 
        </div>'; 
    $error .= '1'; 
} 
if(empty($SecurityAnswer)) 
{ 
    $SecurityAnswer_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Security Answer is Empty. 
        </div>'; 
    $error .= '1'; 
} 
if ($Captcha != $_POST['Captcha']) 
{ 
     $Captcha_error= '<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Captcha SecurityCode is wrong.. 
        </div>'; 
    $error .= '1'; 
} 
if (empty($Captcha)) 
{ 
    $Captcha_error ='<div class="alert alert-danger alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4><i class="icon fa fa-ban"></i> Alert!</h4> 
        Security SecurityCode is Empty. 
        </div>'; 
    $error .= '1'; 
} 
if (empty($error)) 
{ 
mysqli_select_db($conn1,$dbname1); 
$change=mysqli_query($conn1,"UPDATE accounts SET Email='".$NewEmail1."' WHERE Username='".$acc."' ")or die(mysqli_error()); 
mysqli_select_db($conn2,$dbname2); 
$change=mysqli_query($conn2,"UPDATE accounts SET Email='".$NewEmail1."' WHERE Username='".$acc."' ")or die(mysqli_error()); 

} 
} 
if (isset($change)) 
    { 
     $Saved2 = '<div class="alert alert-success alert-dismissable"> 
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
        <h4> <i class="icon fa fa-check"></i> Alert!</h4> 
        Your E-mail Address is Ready To Use. 
        </div>'; 
    } 



?> 

請人幫助我嗎? 請幫助使ajax在此代碼中提交。 我試了更多的時間,然後沒有找到任何解決方案。

+2

請在此處添加您嘗試的Ajax代碼 – manian

+0

:D haha​​h我真的試圖讓它花費很多時間 –

+1

我們不會爲您寫信,但我們非常樂意幫助你解決你試圖爲自己做的事 – RiggsFolly

回答

1

給您的形式例如像id =「形式」的ID

把你的包括/ emailchange.php爲您的表單操作(不需要它)。你需要在你的表單操作和下面的javascript中正確地解決它。

在你的頁面的底部,你需要把這個腳本標籤之間

$(function() { 
"use strict"; 
$("#form").submit(function() { 
    var data = $('#form').serialize(); 
    $.ajax({ 
     url: "includes/emailchange.php", 
     data: data, 
     type: "POST", 
     success: function (msg) { 
      $('#display').html(msg).delay(6000).fadeOut(1000); 
     } 
    }); 
    return false; 
}); 

});

這是爲了讓你開始。處理它並根據需要更改它