简体   繁体   中英

HTML Form with javascript not working - AJAX POST data is not sending to PHP

Javascript code:

 $("#musteriEkleBtn").on("click", function(){
        var zp = $("#musteriEkleForm").serialize();
        $.ajax({
            url: "tsettings/islem.php?islem=mKayit",
            type: "POST",
            data: zp,
            success: function(cevap){
                $("#musteriEkleAlertSuccess").html(cevap).hide().fadeIn(700);
            }       
        });
    });

Html Form Example:

<form id="musteriEkleForm" method="POST">

    <li>
    <label>Adınız :</label>
    <input placeholder="Adınızı yazın." type="text" name="musteri_isim">
    <i class="zirvepin-icon zirvepin-user"></i>
    </li>

    <li class="zirvepin">
    <input id="musteriEkleBtn" type="submit">Register</input>
    </li>   
</form>

This html input submit button is not working, how can I make this code work?

So the form data needs to access the islem.php file, but the form data does not go to islem.php.

islem.php file :

if(g('islem') == 'mKayit') {

    echo"<script type='text/javascript'> alert('Evet Çalışıyor'); </script>";

} 

Thanks to everyone who helped me with this problem, thanks to friends who helped solve the problem via stackexchange fixed.

If you are experiencing the same problem I leave a code that works for you, and thanks to this code you will be out of trouble :

$("#musteriKayitForm").submit(function(event){
    var istek;

    // Verileri güvenli bir şekilde savun. Varsayılan gönderimi kontrol et.
    event.preventDefault();

    // Bekleyen istekleri iptal et.
    if (istek) {
        istek.abort();
    }

    // Bazı yerel değişkenleri burada kuruyorum.
    var $form = $(this);

    // Tüm alanları seçip önbelleğe alıyorum.
    var $inputs = $form.find("input, select, button, textarea");

    // Verileri formda serileştir
    var serializedData = $form.serialize();

    // Ajax talebinin süresi için girdileri devre dışı bırakalım.
    // Not: form verilerinin serileştirilmesinden sonra öğeleri devre dışı bırakırız.
    // Devre dışı form öğeleri serileştirilmeyecektir.
    $inputs.prop("disabled", true);

    $.ajax({
      type: 'post',
      url: 'tsettings/islem.php?islem=mKayit',
      data: serializedData,
      success: function(cevap){ $("#musteriKayitAlertSuccess").html(cevap).hide().fadeIn(700); }    
    });

    // Başarıya çağrılacak geri arama işleyicisi
    istek.done(function (response, textStatus, jqXHR){
    // Konsola mesaj gönder
        console.log("Hooray, it worked!");
    });

    // Arızada çağrılacak geri arama işleyicisi
    istek.fail(function (jqXHR, textStatus, errorThrown){
    // Hatası konsola kaydet
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // İstenmeden çağrılacak geri arama işleyicisi
    // İstek başarısız oldu veya başarılı olduysa
    istek.always(function () {
    // Girişleri etkinleştir
    $inputs.prop("disabled", false);
    });

});

正如Barmar所说,你应该使用event.preventDefault()这样表单就不会提交给自己, Javascript将能够完成他的工作。

Thanks to everyone who helped me with this problem, thanks to friends who helped solve the problem via stackexchange fixed.

If you are experiencing the same problem I leave a code that works for you, and thanks to this code you will be out of trouble :

$("#musteriKayitForm").submit(function(event){
    var istek;

    // Verileri güvenli bir şekilde savun. Varsayılan gönderimi kontrol et.
    event.preventDefault();

    // Bekleyen istekleri iptal et.
    if (istek) {
        istek.abort();
    }

    // Bazı yerel değişkenleri burada kuruyorum.
    var $form = $(this);

    // Tüm alanları seçip önbelleğe alıyorum.
    var $inputs = $form.find("input, select, button, textarea");

    // Verileri formda serileştir
    var serializedData = $form.serialize();

    // Ajax talebinin süresi için girdileri devre dışı bırakalım.
    // Not: form verilerinin serileştirilmesinden sonra öğeleri devre dışı bırakırız.
    // Devre dışı form öğeleri serileştirilmeyecektir.
    $inputs.prop("disabled", true);

    $.ajax({
      type: 'post',
      url: 'tsettings/islem.php?islem=mKayit',
      data: serializedData,
      success: function(cevap){ $("#musteriKayitAlertSuccess").html(cevap).hide().fadeIn(700); }    
    });

    // Başarıya çağrılacak geri arama işleyicisi
    istek.done(function (response, textStatus, jqXHR){
    // Konsola mesaj gönder
        console.log("Hooray, it worked!");
    });

    // Arızada çağrılacak geri arama işleyicisi
    istek.fail(function (jqXHR, textStatus, errorThrown){
    // Hatası konsola kaydet
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // İstenmeden çağrılacak geri arama işleyicisi
    // İstek başarısız oldu veya başarılı olduysa
    istek.always(function () {
    // Girişleri etkinleştir
    $inputs.prop("disabled", false);
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM