使用Ajax将我的Javascript变量传递给PHP

时间:2021-08-16 21:31:47

I am trying to take my Javascript variable and pass it to a PHP variable using AJAX so I can update my SQL. Currently the function is being called but AJAX is not sending the data to PHP.php.

我正在尝试使用我的Javascript变量并使用AJAX将其传递给PHP变量,以便我可以更新我的SQL。目前正在调用该函数,但AJAX没有将数据发送到PHP.php。

CODE UPDATE:

function placeData(){
        //Variable is caled and input is updated//
        var hour1Data = document.getElementById("hourDataInput").value;
        document.getElementById("hour1").innerHTML = hour1Data;

        //Launch AJAX//
        $.ajax({
            type: "POST",
            url: "PHP.php",
            data: {hour1Data: "hello", loginName: <?php echo $_POST['loginName'] ?>},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result){
                alert(result.d);
                console.log(result);
            }
        });
    }

//php.php

 if(isset($_POST['hour1Data']))
        {   
            echo "something is working"; 
            print_r($_POST); //Check the values here first
            $hour1Data = $_POST['hour1Data'];
            $sql = "UPDATE `$user` SET `$dateName`='$hour1Data' WHERE hour=1";
            if ($conn->query($sql) === TRUE) {
            echo "Record updated successfully";
            }
            else {
                echo "problem adding value";
            }
        }

2 个解决方案

#1


0  

Here is the code:

这是代码:

$.ajax({
    type: "POST",
    url: "PHP.php", //Check the url 
    data: {hour1Data: 1, loginName: "testUser"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result){
        alert(result.d);
        console.log(result);
    }
});

Php code:

 if(isset($_POST['hour1Data']))
            {   
                echo "<pre>"; 
                print_r($_POST); //Check the values here first
                $hour1Data = $_POST['hour1Data'];
                $sql = "UPDATE `$user` SET `$dateName`='$hour1Data' WHERE hour=1";
                if ($conn->query($sql) === TRUE) {
                echo "Record updated successfully";
                }
                else {
                    echo "problem adding value";
                }
            }

#2


0  

Remove contentType as $_POST is for form-encoded content types. You can look at this posted problem here for some reference -

删除contentType为$ _POST,用于表单编码的内容类型。您可以在此处查看此发布的问题以供参考 -

Ajax call with contentType: 'application/json' not working.

使用contentType调用Ajax:'application / json'无效。

It is also important to note what type of data you are expecting in your Ajax request and the data type returned by your PHP file. Put error callback so you will know what's the error response.

同样重要的是要注意您在Ajax请求中期望的数据类型以及PHP文件返回的数据类型。放入错误回调,这样您就会知道错误响应是什么。

Hope that helps.

希望有所帮助。

#1


0  

Here is the code:

这是代码:

$.ajax({
    type: "POST",
    url: "PHP.php", //Check the url 
    data: {hour1Data: 1, loginName: "testUser"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result){
        alert(result.d);
        console.log(result);
    }
});

Php code:

 if(isset($_POST['hour1Data']))
            {   
                echo "<pre>"; 
                print_r($_POST); //Check the values here first
                $hour1Data = $_POST['hour1Data'];
                $sql = "UPDATE `$user` SET `$dateName`='$hour1Data' WHERE hour=1";
                if ($conn->query($sql) === TRUE) {
                echo "Record updated successfully";
                }
                else {
                    echo "problem adding value";
                }
            }

#2


0  

Remove contentType as $_POST is for form-encoded content types. You can look at this posted problem here for some reference -

删除contentType为$ _POST,用于表单编码的内容类型。您可以在此处查看此发布的问题以供参考 -

Ajax call with contentType: 'application/json' not working.

使用contentType调用Ajax:'application / json'无效。

It is also important to note what type of data you are expecting in your Ajax request and the data type returned by your PHP file. Put error callback so you will know what's the error response.

同样重要的是要注意您在Ajax请求中期望的数据类型以及PHP文件返回的数据类型。放入错误回调,这样您就会知道错误响应是什么。

Hope that helps.

希望有所帮助。