dougou1943 2013-09-15 15:11
浏览 186
已采纳

我可以使用<input type =“button”/>提交表单值吗?

I'm working on a search engine and I have a really serious problem with GET and POST methods.

Im using an <input type="button" /> to avoid page from refreshing every time im pressing the button.

After button is pressed I'm showing a result (google_map, monumet picture, specs).

The problem now is that I want to submit and show form values + result (google_map, monumet picture, specs) by pressing this button.

This is a problem because <input type="button" /> does not submit form values and I'm really stuck.

  • 写回答

2条回答 默认 最新

  • dscs63759 2013-09-15 15:30
    关注

    Sure, here is a working example for you:

    index.php

    <!DOCTYPE html>
    <html>
    <head>
        <title>Working Example</title>
    </head>
    <body>
    
    <form id="search-form">
        <input type="text" name="text1" id="text1" value=""><br>
        <input type="text" name="text2" id="text2" value=""><br>
        <input type="text" name="text3" id="text3" value=""><br>
        <input type="button" id="search-button" name="search-button" value="search">
    </form>
    
    <div id="response"></div>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('#search-button').click(function(){
                $.ajax( {
                    type: "GET",
                    url: 'response.php',
                    data: $('#search-form').serialize(),
                    success: function(response) {
                        $('#response').html(response);
                    }
                } );
            });
        });
    </script>
    
    </body>
    </html>
    

    response.php

    <?php
    
    echo "text1: " . $_GET['text1'] . "<br>";
    echo "text2: " . $_GET['text2'] . "<br>";
    echo "text3: " . $_GET['text3'] . "<br>";
    
    echo "your response ...";
    

    In the response you return whatever your response is, plus the form fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧