简体   繁体   English

将表单提交到PHP页面,而无需使用Submit按钮重新加载

[英]submitting form to PHP page without reloading using the submit button

Hello friends please am new to jquery and javascript so i copied the code I want to send form to a php page without reloading the page, this code works but i want to click the submit button to send the form and not the enter key: 朋友您好,请问您是jquery和javascript的新手,所以我将要发送表单的代码复制到php页面,而无需重新加载页面,此代码有效,但是我想单击“提交”按钮发送表单而不是回车键:

<input type="text" id="name" name="name"  /><br><br>
<input type="text" id="job" name="job" /><br><br>
<input type="submit" id="submit" />

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#name').focus();
        $('#name').keypress(function(event) {
            var key = (event.keyCode ? event.keyCode : event.which);
            if (key == 13) {
                var info = $('#name').val();
                $.ajax({
                    method: "POST",
                    url: "fell.php",
                    data: {
                        name: $('#name').val(),
                        job: $('#job').val()
                    },
                    success: function(status) {
                    }
                });
            };
        });
    });
</script>

To make the click of the submit button work you should wrap your code in a form element then change your JS code to hook to the submit event of that form. 为了使单击“提交”按钮有效,您应该将代码包装在一个form元素中,然后更改您的JS代码以挂钩到该表单的submit事件。 Note that will also give you the 'submit on enter keypress' action by default, so your current keypress handler can be removed. 请注意,默认情况下还将为您提供“提交输入按键时提交”操作,因此可以删除当前的按键处理程序。 Try this: 尝试这个:

<form id="my-form">
    <input type="text" id="name" name="name"  /><br><br>
    <input type="text" id="job" name="job" /><br><br>
    <input type="submit" id="submit" />
</form>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#name').focus();
        $('#my-form').submit(function(e) {
            e.preventDefault();
            $.ajax({
                method: "POST",
                url: "fell.php",
                data: $(this).serialize(),
                success: function(status) {
                    console.log('AJAX call successful');
                    console.log(status);
                }
            });
        });
    });
</script>
You are not name the submit button. Try to give name to submit button and try below:-
<form id="myFrom" onsubmit="submitForm();">
    <input type="text" id="name" name="name"  /><br><br>
    <input type="text" id="job" name="job" /><br><br>
    <input type="submit" id="submit" name="submit" />
</form>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#name').focus();
    });
    function submitForm(){
        $.ajax({
            method: "POST",
            url: "fell.php",
            data: $("#myFrom").serialize(),
            success: function(status) {
                alert("successfull");
            }
        });
        return false;
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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