0

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.

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";
            }
        }
Ben L
  • 127
  • 1
  • 1
  • 13
  • Sanitize you input, you are wide open to SQL injections. Are you hitting the url correctly? Can you check with your browsers developer tools what the request returns? – BobbyTables May 03 '16 at 06:12
  • 1
    "application/x-www-form- urlencoded" should be "application/x-www-form-urlencoded" (no spaces in between form and encoded) – lisa May 03 '16 at 06:12
  • Hi Benjamin, The issue is with you js code not with the PHP. You can use this link for ref: http://www.w3schools.com/jquery/ajax_ajax.asp – khushboo29 May 03 '16 at 06:13
  • This is for a small app that I am making simply for practice. It will never be used by general public. I am simply trying to use what I have learned so I am not worrying about security right now. – Ben L May 03 '16 at 06:13
  • its work fine if you remove space "application/x-www-form-urlencoded" – RJParikh May 03 '16 at 06:16

2 Answers2

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";
                }
            }
khushboo29
  • 906
  • 6
  • 16
  • From what he posted, the OP doesn't use JQuery. He's trying to learn and a pure js solution would have been a better answer. Could you edit your answer to add it (for the sake of completeness)? – lisa May 03 '16 at 06:29
  • Actually your answer is fine. I was not aware that there was an easier way. However for some reason, it still doesn't work. I will update my code. – Ben L May 03 '16 at 06:34
0

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

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

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.

Hope that helps.

Community
  • 1
  • 1
graceth
  • 178
  • 8