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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)