2014-02-12 61 views
0

我是編程新手,需要專家的幫助。我的AJAX調用被觸發了一個條件,但是當它繼續到新的嵌套php函數時,它顯示輸出是php處理文件...下面是我的代碼..我在這一個上使用bootstrap 3.0框架和純JavaScript,因爲我現在對JQuery一無所知......[已修復] AJAX Doesnt加載第二個嵌套的PHP函數

幫助非常感謝

<!-- Modal Register --> 
<div id="register" class="modal fade"> 
    <div class="modal-dialog"> 
     <div class="container"> 
       <div class="col-xs-12 col-sm-10 col-md-8 col-md-offset-1" style="max-width:400px;"> 

    <form role="form" method="POST" id="reg" action="reg-check.php"> 
        <div class="panel panel-primary" style="border:1px solid black;"> 
         <div class="panel-heading" style="background-color:black;"> 
          <h3 class="panel-title"> 
           Register 
           <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="color:white;">&times;</button> 
          </h3> 

         </div> 
         <div class="panel-body "> 
           <div class="login-box"> 

            <img src="images/newlogo2.png" width="100%;"><br/><br/> 
             <div class="input-group"> 
              <span class="input-group-addon"> 
               <span class="glyphicon glyphicon-user"></span> 
              </span> 
              <input type="text" class="form-control" name="name" placeholder="Name" autofocus /> 
             </div> 
             <div class="input-group"> 
              <span class="input-group-addon"> 
               <span class="glyphicon glyphicon-earphone"></span> 
              </span> 
              <input type="tel" class="form-control" name="mobile" placeholder="Mobile Number" required /> 
             </div> 
             <div class="input-group"> 
              <span class="input-group-addon"> 
               <span class="glyphicon glyphicon-briefcase"></span> 
              </span> 
              <input type="email" class="form-control" name="email" placeholder="email" required/> 
             </div> 
             <div class="input-group"> 
              <span class="input-group-addon"> 
               <span class="glyphicon glyphicon-hand-right"></span> 
              </span> 
              <input type="password" id="pass" class="form-control" name="password" placeholder="Password" required /> 
             </div> 
             <div class="input-group"> 
              <span class="input-group-addon"> 
               <span class="glyphicon glyphicon-hand-right"></span> 
              </span> 
              <input type="password" id = "pass1"class="form-control" name="password1" placeholder="Confirm Password" required /> 
             </div> 
              <label> 
                <input type="checkbox" name="agree" value="agree">&nbsp;I agree to the terms and conditions of the website and cookie policy</label>        
           </div> 
         <div class="panel-footer"> 
          <div class="row"> 
           <div class="col-xs-12 col-sm-12 col-md-12" style="text-align:center;"> 
            <p><span id="errorMessage"></span></p> 
            <button id="submitreg" type="submit" class="btn btn-labeled btn-success" data-dismiss="none" onClick="javascript:ajax_post();"> 
             <span class="btn-label"> 
              <i class="glyphicon glyphicon-ok"></i> 
             </span>Submit</button> 
            <button type="reset" class="btn btn-labeled btn-danger" > 
             <span class="btn-label"> 
              <i class="glyphicon glyphicon-remove"></i> 
             </span>lReset</button> 
           </div> 


          </div> 

         </div> 
        </div> 
       </div> 
      </form> 
      <div><p id="errorMessage"></p></div> 
     </div> 
    </div> 
</div> 
<!-- /.modal-dialog --> 
</div> 

我阿賈克斯Scripts`

function ajax_post(){ 
    // Create our XMLHttpRequest object 
    var hr = new XMLHttpRequest(); 
    // Create some variables we need to send to our PHP file 
    var url = "reg-check.php?"; 
    hr.open("POST", url, true); 
    // Access the onreadystatechange event for the XMLHttpRequest object 
    hr.onreadystatechange = function() { 
    if(hr.readyState == 4 && hr.status == 200) { 
    var return_data = hr.responseText; 
    console.log(return_data); 
    document.getElementById("errorMessage").innerHTML = return_data; 
    } 
    } 
    // Send the data to PHP now... and wait for response to update the status div 
    hr.send(); // Actually execute the request 
    document.getElementById("errorMessage").innerHTML = "processing..."; 
} 

我的PHP腳本

<?php 
require ("/Pages/dbc.php"); 

if(!empty($_POST['name']) && !empty($_POST['mobile']) && !empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['password1'])) 
{ 
    $name =mysql_real_escape_string($_POST['name']); 
    $mobile =mysql_real_escape_string($_POST['mobile']); 
    $email =mysql_real_escape_string($_POST['email']); 
    $password =mysql_real_escape_string($_POST['password']); 
    $password1 =mysql_real_escape_string($_POST['password1']); 

$query =mysql_query("SELECT * FROM `register` WHERE `mobile`= $mobile"); 
$numrows =mysql_num_rows($query); 

    if($numrows == 0){ 
     $query1 =mysql_query("SELECT * FROM `register` WHERE `email`= '$email'"); 
     $numrows1 =mysql_num_rows($query1); 
     if($numrows1==0){ 
      if($password == $password1){ 
       mysql_query("INSERT INTO `cabs4rent`.`register` (`name`, `mobile`, `email`, `password`) VALUES ('$name', '$mobile', '$email', '$password');"); 
        echo "Registration Successful"; 
      }else{ 
       echo "Passwords do not match"; 
      } 

     }else{ 

      echo "Emails is already registered"; 
     } 

    }else{ 

     echo "Mobile Number Already Registered"; 
    } 

} 
else 
{ 

    echo 'Please fill all the details'; 
} 
exit; 

?>

米歇爾感謝您的幫助。

我會盡快更換爲mysqli/pdo。

問題出在我的表單元素的動作attr。所以我刪除了表單元素,並在阿賈克斯的更改和使用URLEncoder的發送數據...一切順利工作..

下面是代碼

function ajax_post(){ 
    // Create our XMLHttpRequest object 
    //preventDefault(); 
    var hr = new XMLHttpRequest(); 
    var name =document.getElementById("name").value; 
    var mobile =document.getElementById("mobile").value; 
    var email =document.getElementById("email").value; 
    var password =document.getElementById("pass").value; 
    var password1 =document.getElementById("pass1").value; 
    var vars = "name="+name+"&mobile="+mobile+"&email="+email+"&password="+password+"&password1="+password1; 
    // Create some variables we need to send to our PHP file 
    var url = "reg-check.php?"; 
    hr.open("POST", url, true); 
    hr.setRequestHeader("Content-type","application/x-www-form-urlencoded") 
    // Access the onreadystatechange event for the XMLHttpRequest object 
    hr.onreadystatechange = function() { 
    if(hr.readyState == 4 && hr.status == 200) { 
    var return_data = hr.responseText; 
    console.log(return_data); 
    document.getElementById("errorMessage").innerHTML = return_data; 
    } 
    } 
    // Send the data to PHP now... and wait for response to update the status div 
    hr.send(vars); // Actually execute the request 

    document.getElementById("errorMessage").innerHTML = "processing..."; 

} 

形式而不formelement

<!-- Modal Register --> 
    <div id="register" class="modal fade"> 
     <div class="modal-dialog"> 
      <div class="container"> 
        <div class="col-xs-12 col-sm-10 col-md-8 col-md-offset-1" style="max-width:400px;"> 


         <div class="panel panel-primary" style="border:1px solid black;"> 
          <div class="panel-heading" style="background-color:black;"> 
           <h3 class="panel-title"> 
            Register 
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="color:white;">&times;</button> 
           </h3> 

          </div> 
          <div class="panel-body "> 
            <div class="login-box"> 

             <img src="images/newlogo2.png" width="100%;"><br/><br/> 
              <div class="input-group"> 
               <span class="input-group-addon"> 
                <span class="glyphicon glyphicon-user"></span> 
               </span> 
               <input type="text" class="form-control"id="name" name="name" placeholder="Name" autofocus /> 
              </div> 
              <div class="input-group"> 
               <span class="input-group-addon"> 
                <span class="glyphicon glyphicon-earphone"></span> 
               </span> 
               <input type="tel" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" required /> 
              </div> 
              <div class="input-group"> 
               <span class="input-group-addon"> 
                <span class="glyphicon glyphicon-briefcase"></span> 
               </span> 
               <input type="email" class="form-control" id="email" name="email" placeholder="email" required/> 
              </div> 
              <div class="input-group"> 
               <span class="input-group-addon"> 
                <span class="glyphicon glyphicon-hand-right"></span> 
               </span> 
               <input type="password" id="pass" class="form-control" name="password" placeholder="Password" required /> 
              </div> 
              <div class="input-group"> 
               <span class="input-group-addon"> 
                <span class="glyphicon glyphicon-hand-right"></span> 
               </span> 
               <input type="password" id = "pass1"class="form-control" name="password1" placeholder="Confirm Password" required /> 
              </div> 

            </div> 
          <div class="panel-footer"> 
           <div class="row"> 
            <div class="col-xs-12 col-sm-12 col-md-12" style="text-align:center;"> 
             <p><span id="errorMessage"></span></p> 
             <button id="submitreg" type="submit" class="btn btn-labeled btn-success" data-dismiss="none" onClick="javascript:ajax_post();"> 
              <span class="btn-label"> 
               <i class="glyphicon glyphicon-ok"></i> 
              </span>Submit</button> 
             <button type="button" id="regReset" onclick="rested();"  class="btn btn-labeled btn-danger" > 
              <span class="btn-label"> 
               <i class="glyphicon glyphicon-remove"></i> 
              </span>Reset</button> 
            </div> 
            <p><span id="errorMessage"></span></p> 

           </div> 

          </div> 
         </div> 
        </div> 
      </div> 
     </div> 
    </div> 
    <!-- /.modal-dialog --> 
    </div> 

對PHP腳本沒有任何更改。

謝謝你的幫助Michel和Stackoverflow ...你guyz岩石。

+0

首先擺脫mysql'的'和使用[mysqli的(http://nl1.php.net/manual/en/book.mysqli.php)或[pdo](http://nl1.php.net/manual/en/book.pdo.php)。 mysql已被棄用 – Michel

回答

0

看起來你沒有發佈任何數據。

// Create some variables we need to send to our PHP file 
var url = "reg-check.php?"; 

不會將任何值添加到URL。

你schould這樣做

var name_value=document.getElementById('name').value; 
var mobile_value=document.getElementById('mobile').value; 
var url = "reg-check.php?" + "name=" + name_value + "&mobile=" + mobile_val;