-1

I tried all the stackoverflow posts about this, I spend two days and still no success , I am trying to pass a JavaScript variable to a PHP variable, I tried this for example and still not succeed, it does not print noting on the screen, here is a full code:

here cc.html:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>

        <script>
            $(document).ready(function(){
                //treehouse code
                var $my_variable = "something";
                $.ajax({
                    url: 'cc.php',
                    data: {x: $my_variable},
                    type: 'POST'
                });
            });
        </script>
    </body>
</html>

and here cc.php:

<?php
if(isset($_POST['x'])){
    echo $_POST['x'];
}
?>

What should I do?

Error 404
  • 427
  • 5
  • 25
  • 1
    The results of `echo` are printed on the server... as in, you wont see it if it is working. A better test would be to display the result on your client that `cc.php` returns. – Ice76 Nov 03 '17 at 18:42
  • do you even know that it executed? – Basti Nov 03 '17 at 18:46
  • You may make an AJAX call to process some data from client in the server-side script and return the result back to the client. If you want to see the result right from the cc.php, you should redirect the page to cc.php with the parameters you want, not by AJAX – Vahid Nov 03 '17 at 18:48

3 Answers3

3

You don't do anything with the result of the AJAX call. Add a callback function:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>

        <script>
            $(document).ready(function(){
                //treehouse code
                var $my_variable = "something";
                $.ajax({
                    url: 'cc.php',
                    data: {x: $my_variable},
                    type: 'POST'
                }).done(function (result) {
                    alert(result);  // <--- do something with the result
                });
            });
        </script>
    </body>
</html>

And your PHP code remains unchanged:

<?php
if(isset($_POST['x'])){
    echo $_POST['x'];
}
?>

What you do in that callback function is up to you. You can alert() the value, log it to the console, write it somewhere on the page, etc.

David
  • 208,112
  • 36
  • 198
  • 279
  • @Error404: Exactly like any other variable in PHP code. `$someVariable = $_POST['x'];` Define the variable and use it for whatever you need. – David Nov 03 '17 at 19:01
  • @Error404: How are you trying to "run" `cc.php`? What have you changed from the question and this answer? – David Nov 03 '17 at 19:20
  • I'm running it via localhost/cc.php in my browser. [Here](https://codepen.io/error303/pen/ooxbKq) is my code (one is `cc.html` and the second is `cc.php`) – Error 404 Nov 03 '17 at 19:29
  • @Error404: CodePen doesn't execute server-side PHP code the way you're attempting. You can't just write PHP code in the CSS code window and expect it to work. As for running it locally, what specifically are you doing and what specifically is happening? If you're just loading `localhost/cc.php` into the browser, what are you expecting to happen and why? The only thing `cc.php` does is read a POST value and echo it to the output. So if you're not *providing it* a POST value, it will have nothing to echo to the output. What exactly is the problem? – David Nov 03 '17 at 19:32
  • I want to echo the variable in `cc.php` – Error 404 Nov 03 '17 at 19:36
  • @Error404: And the code you've written does exactly that. How specifically is this not working? Is PHP not working at all on your system? Have you tested any other PHP code? Have you checked the PHP error logs? How exactly are you testing `cc.php` that it's not working? – David Nov 03 '17 at 19:37
  • PHP is working well, here is the error `Notice: Undefined index: x in C:\XAMPP\htdocs\Compie\Compie\cc.php on line 3` – Error 404 Nov 03 '17 at 19:39
  • You don't will see the x value. This is visible only for your jquery script (with .done) – Pedro Antônio Nov 03 '17 at 19:40
  • @Error404: That error message is answered in detail here: https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef If `x` isn't in the values being posted, then it's not defined in `$_POST`. How are you calling `cc.php`? *Are you* supplying it that value? Your AJAX code does, but how else are you testing it? – David Nov 03 '17 at 19:42
  • @Error404: I guess if you really need me to copy and paste your working code into this answer for you, sure. Done. Again, you *already have* working code. All you needed to do was add a callback function to your AJAX code to *use* the result of the AJAX call. Everything else remains unchanged. – David Nov 03 '17 at 19:54
  • @Error404: When I make a POST request to `cc.php` with an `x` value, it echoes that value as expected. You can observe this by opening `cc.html` and seeing *the code your wrote* successfully alert *the value you're sending*. What other behavior are you expecting? Why? – David Nov 03 '17 at 20:25
  • @Error404: That will print whatever value is in `$_POST['x']`. When you "open `cc.php`", *how* are you supplying it a value? You seem to be drastically misunderstanding a critical concept here. You have to *provide it a value* in order for it to print the value that you provide it. When you just open `cc.php` in your browser, where are you including the value "something"? – David Nov 03 '17 at 20:33
  • My goal is to take a variable from javascript and to use it in the `cc.php` I mean if I have on script in `cc.html`: `var my_var="something"` then I want to use the variable in `cc.php` In my case to echo it `echo $my_var` – Error 404 Nov 03 '17 at 20:40
  • @Error404: And your code successfully does exactly that. On the link you sent, open cc.html and observe. Your JavaScript code sends a value to the server, the server echoes that value back in the response, and the JavaScript code shows that response in an alert. The code *demonstrably works*. All you have to do is click on cc.html and see that it’s doing exactly what you wrote it to do. So since the code does exactly what I expect, and exactly what you describe, it’s really not clear what the problem is. – David Nov 03 '17 at 20:59
0

If you want to print it on the screen you should use success: function(data)$('#result').html(data)} here is code example:

            $(document).ready(function() {
                $('#sub').click(function() {
                    var $my_variable = "something";
                    $.ajax({
                        url: 'cc.php',
                        type: 'POST',
                        data: { x: $my_variable },
                        success: function(data) {
                            $('#result').html(data)
                        }
                    });
                });
            });
-1

You can try $.post too

$.post( "cc.php", { x: $my_variable} );
Pedro Antônio
  • 395
  • 1
  • 6
  • 19