AJAX JQUERY Php表单不工作

AJAX JQUERY Php表单不工作,php,jquery,ajax,Php,Jquery,Ajax,问题仍然存在…请帮助 <link rel="stylesheet" type="text/css" media="all" href="test_style.css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("#ajax-c

问题仍然存在…请帮助

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
使用AJAX jquery时无法加载外部文件。我想使用jQueryAjax弹出表单,然后验证,在mysql中输入数据。但是从一个简单的ajax函数开始。请告诉我哪里出了问题

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>

$(文档).ready(函数(){
$(“#ajax联系人表单”).submit(函数(){
var str=$(this.serialize();
$.ajax(
{
类型:“POST”,
url:“contact.php”,
数据:str,
成功:功能(结果)
{
$(“#div1”).html(结果);
}
});
});
});
名称
此字段必填。
并且contact.php文件是

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
<?php
echo "Hello";
?>

您需要
返回false
以防止表单提交和刷新页面,并检查是否缺少
$(“#div1”)

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
$(document).ready(function(){
     $("#ajax-contact-form").submit(function(){
         var str = $(this).serialize();
         $.ajax(
         {
             type: "POST",
             url:"contact.php",
             data: str,
             success:function(result)
             {
                  $("#div1").html(result);
             }
          });
      return false;
    });
});

您需要
返回false
以防止表单提交和刷新页面,并检查是否缺少
$(“#div1”)

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
$(document).ready(function(){
     $("#ajax-contact-form").submit(function(){
         var str = $(this).serialize();
         $.ajax(
         {
             type: "POST",
             url:"contact.php",
             data: str,
             success:function(result)
             {
                  $("#div1").html(result);
             }
          });
      return false;
    });
});

确保你错过了第一组

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
请使用

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
<div id="div1"><div>

确保缺少div1

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
请使用

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
<div id="div1"><div>

简单。由于您是通过ajax发布表单的,因此必须通过在submit方法中返回false来防止默认表单提交。以下是正确的版本:

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
<script>
$(document).ready(function(){
  $("#ajax-contact-form").submit(function(){
   var str = $(this).serialize();
  $.ajax({
     type: "POST",
     url:"contact.php",
     data: str,
     success:function(result) {
        $("#div1").html(result);
     }
  });
  return false;
 });
});
</script>

$(文档).ready(函数(){
$(“#ajax联系人表单”).submit(函数(){
var str=$(this.serialize();
$.ajax({
类型:“POST”,
url:“contact.php”,
数据:str,
成功:功能(结果){
$(“#div1”).html(结果);
}
});
返回false;
});
});

简单。由于您是通过ajax发布表单的,因此必须通过在submit方法中返回false来防止默认表单提交。以下是正确的版本:

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
<script>
$(document).ready(function(){
  $("#ajax-contact-form").submit(function(){
   var str = $(this).serialize();
  $.ajax({
     type: "POST",
     url:"contact.php",
     data: str,
     success:function(result) {
        $("#div1").html(result);
     }
  });
  return false;
 });
});
</script>

$(文档).ready(函数(){
$(“#ajax联系人表单”).submit(函数(){
var str=$(this.serialize();
$.ajax({
类型:“POST”,
url:“contact.php”,
数据:str,
成功:功能(结果){
$(“#div1”).html(结果);
}
});
返回false;
});
});

您可以使用更简单的post请求形式,如下所示:

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
$.post("url",{var1: value1, var2: value2},function(data,status){
 if(status=='success')
   alert(data);
});
第二个参数可以使用此post请求传递尽可能多的参数。第一个参数url,当然是相对于加载此js的文档的
,或者您可以给出服务器上的确切url

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
根据您的php文件,data=='Hello'

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>

任何
GET
请求的过程也类似。

您可以使用更简单的post请求形式,如下所示:

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
$.post("url",{var1: value1, var2: value2},function(data,status){
 if(status=='success')
   alert(data);
});
第二个参数可以使用此post请求传递尽可能多的参数。第一个参数url,当然是相对于加载此js的文档的
,或者您可以给出服务器上的确切url

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>
根据您的php文件,data=='Hello'

<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>

任何
GET
请求的过程也类似。

请提及缺失的
div1
@legendinmaking:我认为div应该在OP代码中没有显示的其他地方。不管怎样,他更新了密码以防忘记。谢谢。@user2267055然后请接受正确的答案。如果不这样做,您的问题将仍然没有答案。请提及缺失的
div1
@legendinmaking:我认为div应该在OP代码中没有显示的其他地方。不管怎样,他更新了密码以防忘记。谢谢。@user2267055然后请接受正确的答案。如果不这样做,您的问题将仍然没有答案。很抱歉您使用的是ajax submit$(“#ajax联系人表单”)。submit(函数(){您无法显示结果。您需要使用return false;很抱歉您使用的是ajax submit$(“#ajax联系人表单”)。submit(函数(){您无法显示结果。您需要使用return false;
<link rel="stylesheet" type="text/css" media="all" href="test_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
var str = $(this).serialize();
$.ajax(
{
type: "POST",
url:"contact.php",
data: str,
success:function(result)
{
$("#div1").html(result);
}
});
});
});
</script>


</head>
<body>

<div id="contact_form">
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>

<INPUT class="button" type="submit" name="submit" value="Send Message">
</fieldset>
</form>
</div> 


</body>
</html>