2017-05-13 28 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; 
}); 

});

这是为了让你开始。处理它并根据需要更改它