brain pinky brain pinky - 9 months ago 29
Javascript Question

Ajax not sending data to php file

im tring to send data from form using ajax to php file in order to be stored in database but i have this problem . im unable to send data to php file.i dont know what wrong with it



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


}

?>


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>




Answer Source

The issue I see with this is that you are running the script when a button is clicked, not when the form is submitted. Since you are not capturing the form being submitted, you are not capturing any of the values. Try something like this instead:(Take out the hashtag in the form name FIRST so it looks like this 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); 
    }
  });


});