dongqian6554 2010-11-22 03:11
浏览 34
已采纳

使用PHP的HTML表单

I'd like to have a form that executes some php code without having to open a completely new php page. Right now, I'm familiar with "POST" so that I can execute a php file and call the variables from the HTML form using $_POST[variable] but, it takes time to open a new page, and I want to have a form that does the action right then and there.

For example, can someone write html code that creates a text box and a button, and when the user presses go, it displays the text that the user entered right next to the button.

Thanks!

  • 写回答

3条回答 默认 最新

  • duancao1951 2010-11-22 04:39
    关注

    Here's an HTML and PHP snippet to get you started. It uses jQuery and just writes the value of textarea beneath the submit button using AJAX.

    HTML Snippet [file=so.html]

    <!DOCTYPE html>
    <html><head><title>SO Example</title>
    <script 
      type="text/javascript" 
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js">
    </script>
    </head>
    <body>
    
    <form id="frm" name="frm">
      <textarea id="txt" name="txt" rows="4" cols="40">
      </textarea><br />
      <input type="submit"><br />
      <span id="result"></span>
    </form>
    
    <script type="text/javascript">
    $('#frm').submit(function(e){
      e.preventDefault();
      $.ajax({
        url:"/so.php",type:"post",dataType:"html",
        data:$('#frm').serialize(),
        success:function(obj){
          $('#result').text(obj);
        }
      });
    });
    </script>
    
    </body>
    </html>
    

    PHP Snippet [file=so.php]

    <?php
    echo $_POST['txt'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等