我的页面加载时,document.ready函数没有响应。 而且我也面临着.submit功能的问题。请帮忙。 请帮我改写这段代码。 我只是想在php中设计一个表单来检查给定的电子邮件ID是否已经存在于数据库中或者是否使用jquery ajax 请帮忙 .... !我的学校项目迟到了。
$(document).ready(function(){
alert("Working..");
$("#userSubmit").submit(function(){
alert("Working ..");
var email=document.getElementById("userEamil").value;
$.ajax({
type:'POST',
data:"SELECT * FROM user WHERE userEmail='"+email+"'",
url:'common1.php',
success:function(data)
{
$result=$.parseJSON($data);
alert($result);
return false;
},
error:function(data)
{
alert(data);
return false;
}
});
});
});
答案 0 :(得分:1)
您的代码中有一些错误,我希望以下代码可以完成这项工作。如有进一步的问题,请查看$.ajax
部分。
$(document).ready(function() {
// alert("Working..");
$("#userSubmit").submit(function(event) {
event.preventDefault();
alert("Working ..");
var email = document.getElementById("#userEamil").value;
$.ajax({
type: 'POST',
data: "SELECT * FROM user WHERE userEmail='" + email + "'",
url: 'common1.php',
success: function(data) {
$result = $.parseJSON($data);
alert($result);
return false;
},
error: function(data) {
alert(data);
return false;
}
});
});
});
<!-- begin snippet: js hide: false console: true babel: false -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="userSubmit" action="">
<input type="email">
<input type="submit" value="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
<input type="email" >
<input id="userSubmit" type="submit" value="submit">
</form>
答案 1 :(得分:0)
您可以使用解决方案https://jsfiddle.net/1uor0gxf/1/
$(document).ready(function(){
$("#userSubmit").click(function(){
var email = $("userEamil").val();
$.ajax({
type:'POST',
data:`SELECT * FROM user WHERE userEmail='${email}'`,
url:'common1.php',
success:function(data)
{
$result=$.parseJSON($data);
alert($result);
return false;
},
error:function(data)
{
alert(data);
return false;
}
});
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
<input type="text" id="userEamil" />
<input type="button" id="userSubmit" value="Submit" />
</form>
&#13;
答案 2 :(得分:-3)
<script>
$(document).ready(function() {
//alert("Working..");
$("#userSubmit").submit(function() {
//alert("Working ..");
var email = $(this).closest("form").find("input[name='emailbox']").val();
console.log(email);
$.ajax({
type: 'POST',
data: "SELECT * FROM user WHERE userEmail='" + email + "'",
url: 'common1.php',
success: function(data) {
$result = $.parseJSON($data);
alert($result);
return false;
},
error: function(data) {
alert(data);
return false;
}
});
});
});
</script>
<html>
<body>
<form id="userSubmit">
<input type="email" id="userEmail" name="emailbox">
<input type="submit" value="Submit">
</form>
</body>
</html>