不能使用ajax在单击按钮时插入数据库中的表单数据

时间:2022-09-26 08:47:19

I am using ajax for first time,not able to solve this problem here is the code fr.php

我是第一次使用ajax,无法解决这个问题,这里是fr.php代码

<script type="text/javascript">

    function register(){

        $.ajax({

            type: "POST",
            url: "submit_data.php",
            data:     "username=" + document.getElementById("username").value + 
                    "&email=" + document.getElementById("email").value,
            success: function(html){
                $("#response").html(html);
            }
        });
        }

    </script> 

and my problem is its not going inside the function also on button click

我的问题是它不会进入函数内也会点击按钮

3 个解决方案

#1


2  

You are not sending data in a correct way.

您没有以正确的方式发送数据。

Below is your modified code

下面是修改后的代码

<script type="text/javascript">
function register(){
    $.ajax({
        type: "POST",
        url: "submit_data.php",
        data: { "username": document.getElementById("username").value, "email": document.getElementById("email").value },
        success: function(html){
            $("#response").html(html);
        }
    });
}
</script>

#2


1  

you are sending data in wrong way. Use this code for sending data correctly

你用错误的方式发送数据。使用此代码正确地发送数据。

data: { "username": document.getElementById("username").value, "email": document.getElementById("username").value },

#3


0  

agree with Yogesh Suthar additionally if you are using jQuery your code better look like this:

另外,同意Yogesh Suthar的观点,如果你使用jQuery,你的代码最好是这样的:

<script type="text/javascript">

    function register(){

        $.ajax({
            type: "POST",
            url: "submit_data.php",
            data: { username: $("#username").val(), email: $("#email").val() },
            success: function(html){
                $("#response").html(html);
            }
        });
    }

</script>

#1


2  

You are not sending data in a correct way.

您没有以正确的方式发送数据。

Below is your modified code

下面是修改后的代码

<script type="text/javascript">
function register(){
    $.ajax({
        type: "POST",
        url: "submit_data.php",
        data: { "username": document.getElementById("username").value, "email": document.getElementById("email").value },
        success: function(html){
            $("#response").html(html);
        }
    });
}
</script>

#2


1  

you are sending data in wrong way. Use this code for sending data correctly

你用错误的方式发送数据。使用此代码正确地发送数据。

data: { "username": document.getElementById("username").value, "email": document.getElementById("username").value },

#3


0  

agree with Yogesh Suthar additionally if you are using jQuery your code better look like this:

另外,同意Yogesh Suthar的观点,如果你使用jQuery,你的代码最好是这样的:

<script type="text/javascript">

    function register(){

        $.ajax({
            type: "POST",
            url: "submit_data.php",
            data: { username: $("#username").val(), email: $("#email").val() },
            success: function(html){
                $("#response").html(html);
            }
        });
    }

</script>