简体   繁体   English

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

[英]Passing my Javascript Variable to PHP using Ajax

I am trying to take my Javascript variable and pass it to a PHP variable using AJAX so I can update my SQL. 我试图使用Java变量并将其传递给使用AJAX的PHP变量,以便可以更新SQL。 Currently the function is being called but AJAX is not sending the data to PHP.php. 当前正在调用该函数,但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";
            }
        }

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";
                }
            }

Remove contentType as $_POST is for form-encoded content types. 删除contentType因为$ _POST是用于表单编码的内容类型。 You can look at this posted problem here for some reference - 您可以在此处查看此发布的问题以供参考-

Ajax call with contentType: 'application/json' not working . contentType:'application / json'的Ajax调用不起作用

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. 注意在Ajax请求中期望什么类型的数据以及PHP文件返回的数据类型也很重要。 Put error callback so you will know what's the error response. 放置error回调,这样您将知道错误响应是什么。

Hope that helps. 希望能有所帮助。

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

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