簡體   English   中英

使用 jquery AJAX 提交表單

[英]Submitting Form using jquery AJAX

我正在嘗試使用 jQuery ajax 提交我的表單,但我的數據沒有發布到 PHP,它在$_POST數組中返回空數組。

這是我的代碼 - 這是我的表格:

<form action = "/webdevelopmentpakistan/send_mail.php" method = "post" class = "myForm"   >
                <div class="row">
                    <div class="col-md-3 col-sm-12">
                        <div class="form-group">
                            <input class="form-control" id="fname" type="text" required name= "full_name" placeholder="Full Name" 
                             />
                        </div>
                    </div>
                    <div class="col-md-3 col-sm-12">
                        <div class="form-group"> 
                            <input class="form-control" type="tel" required name = "phone" placeholder="+92" id="phone" onkeypress="return isNumberKey(event)" />
                        </div>
                    </div>
                    <div class="col-md-3 col-sm-12">
                        <div class="form-group">
                            <input class="form-control" type="email" required name = "email" id="email" placeholder="Email"/>
                        </div>
                    </div>
                    <div class="col-md-3 col-sm-12">
                        <div class="form-group">
                            <input class="btn popup" type="submit"    name = "submit" value="CONTACT OUR CONSULTANT"/>
                        </div>
                    </div>
                </div>
</form>

它是一個 ajax 部分:

  $('form').on('submit', function (e) {
    e.preventDefault();

    var url = $(this).attr("action");
    var form_data = $(this).serialize();


    $.ajax({
            type: 'POST',
            url: url,
            data: $('.myForm').serialize() ,
            dataType : 'JSON',
            //contentType: "application/x-www-form-urlencoded",
            success: function (data) { // here I'm adding data as a parameter which stores the response

                    console.log(data); // instead of alert I'm changing this to console.log which logs all the response in console.
            },
             error:function(xhr, textStatus, thrownError, data)
             {
                 console.log("Error: " + thrownError);
                 console.log("Error: " + textStatus);


             }
                   });



    // var popup = document.getElementById("myPopup");
    // popup.classList.toggle("show");
    console.log(form_data);

});

在其他頁面使用的 PHP 代碼:

if(isset($_POST)) {
        echo json_encode($_POST);
    }

這是我在提交表單時得到的序列化數組,但它沒有傳遞給 php

full_name=talha&phone=012345678&email=admin%40gmail.com

表單操作需要是絕對網址,即https://somewebsite.com或您網站上的相對網址,因此理想情況下它應該是/some-url.php 在此處閱讀有關表單操作的信息

所以你的表單開始標簽應該是,

<form action = "/web-development-in-pakistan.php" method = "post" class = "myForm"  target="_self">

所以在你的 javascript 代碼中

var url = $(this).attr("action");

我也相信在你的 ajax 調用中, type需要是method ,所以,

$.ajax({
  method: "POST",
  .....
 })

歡迎使用 stackoverflow,這里是更改,希望它會起作用

$.ajax({
    type: 'POST',
    url: url,
    data: $('.myForm').serialize() ,
    dataType : 'json', // changing data type to json
    success: function (data) { // here I'm adding data as a parameter which stores the response
        console.log(data); // instead of alert I'm changing this to console.log which logs all the response in console.
    }
});

在 php 中

if(isset($_POST)) {
    echo json_encode($_POST);
}

這應該在您的控制台中打印 post 參數數組,但是您將在 php 中獲得一個數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM