0

I try to pass this value to my php code, but I do not know how to do it. post method does not work. (I do not know why).

<script>
    var val = localStorage.getItem('sumalist');
    $.ajax({
        type: "POST",
        url: "index.php",
        data: {value: val},
        success: function () {
            console.log(val);
        }
    });
</script>

and in my php code, value is not set.

if (isset($_POST["value"])) {
    echo "Yes, value is set";
    $value = $_POST["value"];
}else{
    echo "N0, value is not set";

}

PS: My php code is in the same file in js code.

5
  • How do you run your javascript code? Did you get any errors? Commented Jul 4, 2018 at 18:23
  • My code js in the same file in php code - index.php || no, my console only shows the value I want to pass to php Commented Jul 4, 2018 at 18:26
  • Given the code and the fact that index.php is returning a HTML document with that script tag included, you won't notice whether the value is set or not. The return value of your ajax request is never used... Please specify what you wan't to achieve after sending the ajax request. Commented Jul 4, 2018 at 18:32
  • My var val = localStorage.getItem('sumalist'); returns a float. I would like this value send to sql query.: Commented Jul 4, 2018 at 18:35
  • $value = $_POST["value"]; if ($polaczenie->query("INSERT INTO zamowienia VALUES ('$value')")) ... Commented Jul 4, 2018 at 18:35

4 Answers 4

2

Check if this works

<?php
if(!empty($_POST)) {
    $value = (isset($_POST["value"])) ?  $_POST["value"] : NULL;
    $return = ($value != NULL) ? "Yes, value is: ".$value : "N0, value is not set";
    echo $return;
    exit;
}
?>
<script src="//code.jquery.com/jquery-3.3.1.js"></script>
<script>
    var val = 'value sent';
    $.ajax({
        type: "POST",
        url: "index.php",
        data: {value: val},
        success: function (ret) {
            console.log(ret);
        }
    });
</script>
Open console for result
Sign up to request clarification or add additional context in comments.

4 Comments

why not just print_r($_POST); exit;?
it would be even better but he wants to have this message with set/not set
Yes true that, but if he/she is knowing what he/she is doing there wouldn't be such a question :)
N0, value is not set
0

Please use console if you're using chrome then open console and try debugging, And first you run that ajax function in jquery ready function like this

$(document).ready(function (){ $.ajax( replaced for ajax function ) }

Comments

0

If you want to use the response in callback success function, use this:

success: function (ret) {
    console.log(ret); //Prints 'Yes, value is set' in browser console
}

3 Comments

My console show my all html code in my file, and this solution does not work either.
It returns the entire HTML code because your PHP code and the HTML data are in the same file (HTML tags are output by server after PHP code is processed by PHP core). Create a different file called 'script.php', put the PHP code in there, modify the target from Ajax request to 'script.php' and it should work.
data: {'value': val} and data: {value: val} are absolutely equal, even you have defined var value, JavaScript do not allow variables as property in this way of Object defining.
0

In your browser you have Developer Tools - press F12 to open, go to Network tab (FireFox, Chrome, IE - all the same), then reload your page and you will see the line for your AJAX call (if it is performed on load, or trigger your call if this is not the case), select it and right hand you'll see a extra frame where you can see all the details of your request, including request params, headers, response headers, the actual response and many other.

That's the best solution to check your AJAX request without asking uncompleted questions and seeking for the answers in case someone can assemble your full case in his mind.

Believe me - this is the best solution for you and not only for this case!

Of course your JS should be performed when DOM is ready so you have to wrap it in

${function() {
  // your code here 
}); 

in case you want to be executed on load.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.