如何通过URL发送所有参数?

时间:2015-11-02 03:15:59

标签: javascript jquery ajax

$(document).ready(function () {
    $("#submitButton").click(function () {
            var name = $("#name").val();
            var age = $("#age").val();
            var gender = $('input:radio[name=gender]:checked').val();
            var contact_no = $("#contact_no").val();
            var city = $("#city").val();
            var concern = $("#concern").val();
            var email = $("#email").val();

            function ajaxcall(name, age, gender, contact_no, city, concern, email);
        }
    });

    function ajaxcall(name, age, gender, contact_no, city, concern, email) {
        jQuery.ajax({
            type: "POST",
            url: "http://adjetter.com/lp/lead-integration.html",
            data: {
                name: name,
                age: age,
                gender: gender,
                contact_no: contact_no,
                city: city,
                concern: concern,
                email: email
            },
            dataType: "html",
            success: function (data) {
                alert();
            }
        });
    }

2 个答案:

答案 0 :(得分:1)

您应该使用此经过测试和运行的代码:

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

        var $form = $(this);

        ajaxcall($form.serialize());
    });

    function ajaxcall(data) {
        jQuery.ajax({
            type: "POST",
            url: "form.php?" + data,
            dataType: "html",
            success: function (data) {
                console.log(data);
            }
        });
    }
});

如果您发送值的文件名为form.php,那么您可以通过以下方式访问form.php中的数据:

<?php
    echo $_GET['name'] . ' ' . 
         $_GET['age'] . ' ' . 
         $_GET['gender'] . ' ' . 
         $_GET['contact_no'] . ' ' . 
         $_GET['city'] . ' ' . 
         $_GET['concern'] . ' ' . 
         $_GET['email'];
?>

答案 1 :(得分:0)

我修改了你的代码。当你使用function ajaxcall(name, age, gender, contact_no, city, concern, email);时,实际上是定义了一个新函数而不是调用现有函数。您还可以查看将参数传递给js函数的更好方法 Pass a JavaScript function as parameter

$(document).ready(function (){
        $("#submitButton").click(function() {
                var name =$("#name").val();
                var age = $("#age").val();
                var gender = $('input:radio[name=gender]:checked').val();
                var contact_no = $("#contact_no").val();
                var city = $("#city").val();
                var concern = $("#concern").val();
                var email = $("#email").val();

               ajaxcall(name, age, gender, contact_no, city, concern, email)
            })

        });

    function ajaxcall(name, age, gender, contact_no, city, concern, email) {
         jQuery.ajax({
                type: "POST",
                url: "https://adjetter.com/lp/lead-integration.html",
                data: {
                    name: name,
                    age: age,
                    gender: gender,
                    contact_no: contact_no,
                    city: city,
                    concern: concern,
                    email: email
                },
                dataType: "html",
                success: function (data) {
                    alert();
                }
            });
        }

为了方便而创造了一个小提琴。如果您使用的是chrome,则可以查看开发人员工具的ajax请求。按F12并查找网络选项卡,如果ajax执行,您将看到ajax url被调用。单击所请求的URL,您将看到一个标题选项。单击它以检查输入参数。     jsfiddle Chrome Developer's tool & inspecting request