duanlu1959 2013-08-10 16:59
浏览 29
已采纳

使用php将Ajax文本字段转换为div标签1,将其他文本字段转换为div标签

<html>
<head>
<meta charset="utf-8" />
<title>jQuery Ajax - POST</title>
<script type="text/javascript" src="jquery_1.6.1.js"></script>
<script type="text/javascript"><!--
$(document).ready(function() {
  $('form').submit(function() {
    var data = 'name='+$('#nm').val();
    var data = 'name='+$('#sn').val();
    $.post('script.php', data, function(response){
      $('#dv').html(response);
      $('#dv2').html(response);
    });

    return false;      // required to not open the page when form is submited
  });
});
--></script>
</head>
<body>
<div id="dv">Here will be displayed the response.</div><br />
<form action="script.php" method="post">
 Name: <input type="text" name="nm" id="nm" /><br />
<div id="dv2">Here will be displayed the 2nd response.</div><br />
 Second name: <input type="text" name="sn" id="sn" /><br />
 <input type="submit" value="Submit" />
</form>
</body>
</html>

I want Name to post to JavaScript and come out under divtag (dv) and second name to come out into (dv2) - I looked around all I ever seem to find is just how to echo variable out in one div tag.

  • 写回答

1条回答 默认 最新

  • dongxiaoguang9108 2013-08-10 18:04
    关注

    Check this out. You declared the same name for the data passed to script.php . here is the edited code.It is working.

    <html>
    <head>
    <meta charset="utf-8" />
    <title>jQuery Ajax - POST</title>
    <script type="text/javascript" src="jquery_1.6.1.js"></script>
    <script type="text/javascript"><!--
    $(document).ready(function() {
      $('form').submit(function() {
        var nm = $('#nm').val(); // changes made
        var sn= $('#sn').val();
        $.post('script.php',{
        name1:nm,
        name2:sn
      }, function(response){
          var resp=response.split("."); // spiltting into different output
          $('#dv').html(resp[0]);
          $('#dv2').html(resp[1]);
        });
    
        return false;      // required to not open the page when form is submited
      });
    });
    --></script>
    </head>
    <body>
    <div id="dv">Here will be displayed the response.</div><br />
    <form method="post"> // no need to specify script.php here
     Name: <input type="text" name="nm" id="nm" /><br />
    <div id="dv2">Here will be displayed the 2nd response.</div><br />
     Second name: <input type="text" name="sn" id="sn" /><br />
     <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    

    get the parameter in script.php as follows

    <?php 
    
    String name1 =$_POST["name1"];
    String name2= $_POST["name2"];
    // do your calculations
    
    String together= name1 +"."+ name2; // just make different output together as one string
    out.print(together);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重