通过ajax正确发送表单

时间:2017-02-08 18:57:27

标签: ajax forms

我试图将表单内容发送到当前(或其他)php文件进行处理,但我不知道我的代码是如何工作的!

<a href="#" class="eb" date-uid="<?php echo $value[0]; ?>" data-toggle="modal" data-target=".bs-example-modal-lg">
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">

                <div class="modal-body">

                    <H2>Edit contact</H2>
                    <form name="editform" method="post">
                        <input type="name" name="ename" placeholder="name" required /></br></br>
                        <input type="tel" name="etel" placeholder="phone number" required /></br></br>
                        <input type="text" name="edes" placeholder="description" required /></br></br>
                        <input type="submit" class="esub" name="esub" value="Save" />
                    </form>

                    <script>
                        $(document).ready(
                            function () {
                                $(".esub").click(
                                    function () {
                                        var uid = $(".eb").attr("data-id");
                                        var ename = document.getElementByName("ename");
                                        var etel = document.getElementByName("etel");
                                        var edes = document.getElementByName("edes");
                                        ajax({
                                            url: 'uedit.php',
                                            type: 'POST',
                                            data: {
                                                owner: uid,
                                                name: ename,
                                                tel: etel,
                                                description: edes
                                            },
                                            success: function (result) {
                                                return result;
                                            }
                                        });
                                    }
                                );
                            }
                        );
                    </script>

我的代码是对的吗?我怎么能确定我的代码能够正确运行?

2 个答案:

答案 0 :(得分:0)

从你的jquery代码中删除这一行

var uid=$(".eb").attr("data-id");

因为没有元素具有class =&#34; eb&#34;

答案 1 :(得分:0)

您的代码中有几处错误。试试这个:

             <script>
            $( document ).ready(function() {
                    $(".esub").click(function(){

                            var uid=$(".eb").attr("data-id");
                            var ename=getElementByName("ename");
                            var etel=getElementByName("etel");
                            var edes=getElementByName("edes");

                            ajax({
                                url:'uedit.php',
                                type:'POST',
                                data:({
                                    owner:uid,
                                    name:ename,
                                    tel:etel,
                                    description:edes
                                    }),
                            success:function(result){
                            return result;
                            }  

                        });
                });
            });
             </script>