0

有没有一种方法可以在不自动进入下一页的情况下传输这些输入值。

        <form method="GET" action="cos2.php">
<td> Table:</td>
<td>
<select name="table"><option value=""></option>
<option value="1">1</option><option value="2">2</option
</select>
    </form>

这些值将显示在“cos2.php”中。但我想将其存储在“cos2.php”页面上,而不会自动转到“cos2.php”页面。

4

2 回答 2

0

你可以利用jQuery.ajax()有大量的教程来做到这一点。

例子

HTML:

<form id="foo">

    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />

</form>

JavaScript:

// variable to hold request
var request;
$("#foo").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);
    // let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    $inputs.prop("disabled", true);

    // fire off the request to /form.php
    var request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // reenable the inputs
        $inputs.prop("disabled", false);
    });

    // prevent default posting of form
    event.preventDefault();
});

PHP(即form.php):

// you can access the values posted by jQuery.ajax
// through the global variable $_POST, like this:
$bar = $_POST['bar'];

你就完成了。

于 2013-02-26T04:30:54.243 回答
0

您可以为此使用Jquery Ajax,其中有很多示例。

于 2013-02-26T04:32:59.497 回答