gpt4 book ai didi

javascript - Ajax不发送数据到php文件

转载 作者:行者123 更新时间:2023-11-28 04:53:16 25 4
gpt4 key购买 nike

我正在尝试使用 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";}


}

?>

   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>

最佳答案

我看到的问题是,您在单击按钮时运行脚本,而不是在提交表单时运行脚本。由于您没有捕获正在提交的表单,因此您没有捕获任何值。请尝试这样的操作:(首先取出表单名称中的主题标签,使其看起来像这样 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);
}
});


});

关于javascript - Ajax不发送数据到php文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42738624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com