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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?