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 に答える