duanjing9339 2014-01-28 10:43
浏览 56
已采纳

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

Reply 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 content :

Script for show hide div which is in while loop

<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 which ll send data to database

<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

message sent

Now the problem is, i ve while loop for reply .. so if i m posting in the 3rd reply link, the 1st reply link got posted with msg sent mesage.. second validtion is not working drctly the reply.txt msg is showing n 3rd value is not inserting to database

  • 写回答

1条回答 默认 最新

  • du9698 2014-01-29 06:21
    关注

    Your 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>
    

    Your 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应