提交数据和使用ajax加载消息

时间:2014-01-28 10:43:24

标签: javascript php jquery ajax

  

回复div:

<a href="#" class="show_hide" style="text-align:right;">Reply</a>
<div class="slidingDiv">

<div class="container">
<div id="myDiv">
<a href="#" class="show_hide1" style="float:right; text-decoration:none; font-size:14px; font-weight:bold; color:#000;">
X</a>

<form method="post" name="form" action="">
<table width="97%" border="0">
 <tr height="100px">
<td>Message</td>
<td>:</td>
<td colspan="4"><textarea name="msg" cols="50" rows="6" required></textarea></td>
</tr>
<tr height="30px">
<td>Email</td>
<td>:</td>
<td><input name="email" type="email" required/></td>    
 <td>Mobile</td>
   <td>:</td>
     <td><input name="mob" type="text" required placeholder="+91" maxlength="10"/></td>
</tr>
 <tr height="30px">
<td colspan="6" align="right">
<input type="submit" value="Post" name="submit" class="submit_button" onclick="loadXMLDoc()"/></td>

 </tr>
 <div class="clear"></div>
</table>

<div class="clear"></div>
</form>
</div>
</div>
  

Ajax内容:

show hide div的脚本,它位于while循环中

<script type="text/javascript">
$(document).ready(function () {
var $slides = $(".slidingDiv").hide();
$(".show_hide").show().click(function () {
    //if it is the reply link then find the next element
    var $slider = $(this).next(".slidingDiv");
    if (!$slider.length) {
        //if the link inside the slider div is cliced then find the ancestor element
        $slider = $(this).closest(".slidingDiv");
    }
    $slides.not($slider).stop(true, true).slideUp();
    $slider.stop(true, true).slideToggle();
});
});
</script>
  

将数据发送到数据库的Ajax

<script type="text/javascript">
$(function() {
$(".submit_button").click(function() {
var textcontent = $("#content").val();
var dataString = 'content='+ textcontent;
if(textcontent=='')
{
alert("Enter some text..");
$("#content").focus();
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
$.ajax({
type: "POST",
url: "action.php",
data: dataString,
cache: true,
success: function(html){
$("#show").after(html);
document.getElementById('content').value='';
$("#flash").hide();
$("#content").focus();
}  
});
}
return false;
});
});
</script>

 <script>
 function loadXMLDoc()
 {
 var xmlhttp;
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
 }
 }
xmlhttp.open("GET","ajax.txt",true);
xmlhttp.send();
}
</script>
  

reply.txt

发送消息

现在问题是,我在回复循环时..所以如果我在第3个回复链接发布,第一个回复链接发布了msg发送消息..第二个验证不能正常工作answer.txt消息是显示第3个值未插入数据库

1 个答案:

答案 0 :(得分:1)

您的HTML

<a href="#" class="show_hide" style="text-align:right;">Reply</a>
<div class="slidingDiv">

<div class="container">
<div id="myDiv">
<a href="#" class="show_hide1" style="float:right; text-decoration:none; font-size:14px; font-weight:bold; color:#000;">
X</a>

<form method="post" name="form" action="">
<table width="97%" border="0" id="myID"> // Added ID
 <tr height="100px">
<td>Message</td>
<td>:</td>
<td colspan="4"><textarea name="msg" cols="50" rows="6" required></textarea></td>
</tr>
<tr height="30px">
<td>Email</td>
<td>:</td>
<td><input name="email" type="email" required/></td>    
 <td>Mobile</td>
   <td>:</td>
     <td><input name="mob" type="text" required placeholder="+91" maxlength="10"/></td>
</tr>
 <tr height="30px">
<td colspan="6" align="right">
<input type="submit" value="Post" name="submit" class="submit_button" onclick="loadXMLDoc()"/></td>

 </tr>
 <div class="clear"></div>
</table>

<div class="clear"></div>
</form>
</div>
</div>

您的AJAX

function loadXMLDoc()
     {
     var xmlhttp;
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp=new XMLHttpRequest();
     }
     else
     {// code for IE6, IE5
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange=function()
     {
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     $("#myId").css("display", "none"); // Add this
     document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
     }
     }
    xmlhttp.open("GET","ajax.txt",true);
    xmlhttp.send();
    }

REMOVED $

<script type="text/javascript">
$(document).ready(function () {
var slides = $(".slidingDiv").hide();
$(".show_hide").show().click(function () {
    //if it is the reply link then find the next element
    var slider = $(this).next(".slidingDiv");
    if (!slider.length) {
        //if the link inside the slider div is cliced then find the ancestor element
        slider = $(this).closest(".slidingDiv");
    }
    slides.not($slider).stop(true, true).slideUp();
    slider.stop(true, true).slideToggle();
});
});
</script>