Ajax没有将数据发送到php文件

时间:2017-03-11 17:55:19

标签: javascript php jquery ajax html5

我正在尝试使用AJAX将表单中的数据发送到PHP文件以便存储在数据库中,但我遇到了这个问题。我无法将数据发送到PHP文件。我不知道它有什么问题



this is php

<?php
  
$servername = "localhost";
$username = "root";
$password = "";
$db_name="menoon";

// Create connection
$conn = new mysqli($servername, $username, $password,$db_name);

// Check connection
if ($conn->connect_error) 
 {
    die("Connection failed: " . $conn->connect_error);
 } 
	$UserName=$_POST['username'];
	$Email=$_POST['inputEmail3'];
	$Password=md5($_POST['inputPassword3']);
	$Address=$_POST['add'];
	$fname=$_POST['name'];
	$membership=$_POST['memberhhiip'];
	
if (filesize($_FILES['pic']['tmp_name']))
 {
    // cover_image is empty (and not an error)
    
	$File=file_get_contents($_FILES['pic']['tmp_name']); //$_POST['file'];
	
	
	$vaildMail="SELECT EMAIL From user_emails WHERE EMAIL='$Email'";
	
	$r =mysqli_query($conn ,$vaildMail);
	if(mysqli_num_rows($r)==0)   //to check if there is an exist email 
	{
  
    
   // echo "ID".$ID."  ".$UserName." ".$Password."<br>";	
  	$sql="INSERT INTO user (FULLNAME,HOMEADDRESS,UserName,PASSWORD,MEMBERSHIPTYPE,PROFILEPHOTO) values('$fname','$Address','$UserName','$Password','$membership',?)"; 
	
	
	if($stmt = $conn->prepare($sql))
	{           $stmt->bind_param("b",$_null);
				$stmt->send_long_data(0,$File);
				$stmt->execute();

				 $sql="SELECT ID FROM user WHERE UserName='$UserName'";
			      $s =mysqli_query($conn ,$sql);
			      $row = mysqli_fetch_row($s);
			      $sql=" INSERT INTO user_emails (USER_ID,EMAIL) values('$row[0]','$Email')"; 
			      $sr =mysqli_query($conn ,$sql);

			 
			    
				{
					
						ob_start();
			              header('Location:../index.php');
			              ob_end_flush();
			              die();
					
					
				}
    }

    else {
    	echo "user name alredy exists";
		}
	
	}
	else {echo "Email alredy exists";
					}	
}
else {$vaildMail="SELECT EMAIL From user_emails WHERE EMAIL='$Email'";

	$r =mysqli_query($conn ,$vaildMail);
	if(mysqli_num_rows($r)==0)   //to check if there is an exist email 
	{
   

   // echo "ID".$ID."  ".$UserName." ".$Password."<br>";	
  	$sql=" INSERT INTO user (FULLNAME,HOMEADDRESS,UserName,PASSWORD,MEMBERSHIPTYPE) values('$fname','$Address','$UserName','$Password','$membership')"; 
  
	
	
	if ($s =mysqli_query($conn ,$sql))
	
 {     $sql="SELECT ID FROM user WHERE UserName='$UserName'";
      $s =mysqli_query($conn ,$sql);
      $row = mysqli_fetch_row($s);
      $sql=" INSERT INTO user_emails (USER_ID,EMAIL) values('$row[0]','$Email')"; 
      $sr =mysqli_query($conn ,$sql);



    
	{
		
			
			ob_start();
              header('Location:../index.php'); 
              ob_end_flush();
              die();
		
		
	}
	}

	

else  
	        { echo "user name alredy exists";
              

              
	}
	
	}
	else {echo "Email alredy exists";}


		}

?>

 
&#13;
   this is the  html file including script

<div id="signup" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        
        <h4 class="modal-title"SIGN UP</h4>
      </div>
    
        
         <div class="modal-body">
                
                <form class="form-horizontal" role="form"  id="#my_form" enctype="multipart/form-data">


                
             
                  <div class="form-group">
                   

                    <div class="col-sm-10">
                        <input type="email" class="form-control" 
                         name="inputEmail3"   id="inputEmail3" placeholder="Email"  required/>
                    </div>
                  </div>
                  <div class="form-group">
                    
                    <div class="col-sm-10">
                        <input type="password" class="form-control"
                          name="inputPassword3"  id="inputPassword3" placeholder="Password"  required/>
                    </div>
                  </div>

            
                           <div class="form-group">
                         <div class="col-sm-10">
                       <input type="file" name="pic" class="" data-multiple-caption="{count} files selected" multiple />
                      </div>
                       </div>
                 
                  <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                      <div class="checkbox">
                        <label>
                            <input type="checkbox"/> Remember me
                        </label>
                      </div>
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                      <button type="submit" class="btn btn-primary "  onclick="sendForm()">Sign Up</button>
                    </div>
                  </div>


                </form>
                
                
                
                
                
                
           
      </div>
      <div class="modal-footer">
       
      </div>
    </div>


  </div>
</div>
 
<script >
   function sendForm(){
    
    $.ajax({
    method: "POST",
    url: "signcontroller.php",
    data: jQuery("#my_form").serialize(),
    cache: false,
    success:  function(data){
        alert(data); 
    }
  });

}

</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我看到的问题是,您在单击按钮时运行脚本,而不是在提交表单时运行脚本。由于您没有捕获正在提交的表单,因此您没有捕获任何值。请尝试这样的事情:(取出表格名称为FIRST的主题标签,使其看起来像id="my_form"

$(document).on("submit", "#my_form", function(e){
// The e is the form submission event.


e.preventDefault(); //This prevents form from submitting normally

var formdata = $('#my_form').serialize(); //This will serialize the form data

 $.ajax({
    method: "POST",
    url: "signcontroller.php",
    data: formdata,
    cache: false,
    success:  function(data){
        alert(data); 
    }
  });


});