SORU
15 ŞUBAT 2011, Salı


jQuery Ajax örneği ile YAZILAN PHP

Veritabanına formdan veri göndermek için çalışıyorum. Burada kullandığım form

<form name="foo" action="form.php" method="POST" id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />
    <input type="submit" value="Send" />
</form>

Tipik yaklaşımı formu göndermek olabilir, ama bu tarayıcı yeniden yönlendirme neden olur. Kullanarak bir WordPress kullanmak ve Ajax mümkün formun veri yakalama ve PHP bir komut dosyası (örneğin,. kabul edilir ^em>form.php)?

CEVAP
15 ŞUBAT 2011, Salı


.ajax temel kullanımı bu gibi bir şey olacaktır

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;

// Bind to the submit event of our form
$("#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.
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    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 occurred: " 
            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();
});

1.8, .jQuery Beri not: başarı, .hata ve .tam lehine kabul edilmez .bitti .başarısız ve .her zaman.

Not: Yukarıdaki Pasajı $(document).ready() işleyicisi içinde (veya $() steno kullanmak) koymak gerekir bu yüzden DOM hazır sonra yapılması olduğunu Hatırlayın.

İpucu: chain böyle: geri arama işleyicileri $.ajax().done().fail().always();

PHP (form.php):

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

Not: her Zaman iğne ve diğer kötü niyetli kod önlemek için sanitize posted data,.

Ayrıca yukarıdaki JavaScript kodu .ajax yerine 21 ** steno kullanabilirsiniz:

$.post('/form.php', serializedData, function(response) {
    // Log the response to the console
    console.log("Response: " response);
});

Not: Yukarıdaki JavaScript kodu jQuery 1.8 ile çalışmak ve daha sonra yapılır, ama bir önceki sürüm ile bir WordPress kullanmak 1,5-aşağı çalışması gerekir.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Doc Adams

    Doc Adams

    20 HAZİRAN 2007
  • thelonelyisland

    thelonelyisl

    23 Aralık 2005
  • VJ VIMANA

    VJ VIMANA

    3 Mayıs 2007