douyujun0152 2015-08-28 12:52
浏览 205

有没有办法提交没有提交按钮的表单?

I am writing a form, and I may be overcomplicating this, but I want the form to change based on the response to a question. It is a contact form. I want to ask a question, then use a php if else statement to generate the response do if a show this haml, elseif b show this. In the end the form will be emailed to me. Here is my question. If i set the variables in the head section like this:

<?php
    $letter = $_POST['letter'];
    ?>
    </html>
    <body>
    <form method="post" action="<?php echo     htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <select name="letter'>
    <option value="a">a</option>
    <option value="b">b</option>
    </form>
    <?php
    if ($letter == a) {html}
    elseif ($letter == b) {html}
    ?>

Would that work, or does the form need to be submited before the variable can be assigned? If it needs to be submited, can that be done as soon as the selection is made in the select tag and how would I do that without the user clicking a submit button?

  • 写回答

1条回答 默认 最新

  • dsyq40772 2015-08-28 13:07
    关注

    yes using javascript(jQuery) you can submit form without refreshing page.!

    follow these steps and create files

    Example.php

    <html>
    <head>
       <!--include jquery and js code -->
       <script type="javascript/text" src="jquery.js"></script>
       <script type="javascript/text" src="myjs.js"></script>
    </head>
      <body>
       <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
          <select name="letter' id="letter">
              <option value="a">a</option>
              <option value="b">b</option>
          </select>
         </form>
         <div id="response"></div>
       </body>
    </html>
    

    myjs.js

    $(document).ready(function(){
         $("#letter").change(function(){
               $.post("process.php", { letter: $(this).val() },function(r){
                     $("#response").html(r);
               });
         });
    });
    

    Now final process.php

    now in process.php you can do process like that

    <?php
    $letter = $_POST['letter'];
    if ($letter == a) {html}
    elseif ($letter == b) {html}
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写