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

时间:2013-09-15 15:11:18

标签: php ajax post button get

我正在研究搜索引擎,我对GETPOST方法存在严重问题。

我使用<input type="button" />来避免每次按下按钮时刷新页面。

按下按钮后,我显示了一个结果(google_map,monumet picture,specs)。

现在的问题是,我想通过按此按钮提交并显示表单值+结果(google_map,monumet picture,specs)。

这是一个问题,因为<input type="button" />没有提交表单值而且我真的被卡住了。

2 个答案:

答案 0 :(得分:3)

当然,这是一个有效的例子:

<强>的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 ...";

在回复中,您将返回您的回复以及表单字段。

答案 1 :(得分:1)

onClick="document.getElementById('FormName').submit();"
相关问题