0

i need to send a variable with post in php to js and in the js get it with ajax, the value of the variable is a string. In php i get the value like this

$_POST["secret"] = json_decode($oSecret->seeSecret());

And in javascript i am trying this

$.post("usuariosService.php",function(datos){ alert(datos); },"json");

but i dont get anything, can somebody help me?

5
  • Where do you get the $oSecret->seeSecret() from? Commented Apr 30, 2020 at 12:24
  • From a class that makes a query to mysql. Commented Apr 30, 2020 at 12:28
  • I'm not sure you understand the concept of ajax. You want to send value from php to javascript with $_POST["secret"] = json_decode($oSecret->seeSecret());? Commented Apr 30, 2020 at 12:31
  • Echo echo json_decode($oSecret->seeSecret()); Commented Apr 30, 2020 at 12:37
  • All i want to do is send the value of the string from the php to the javascript, the problem is that the value is supposed that cant be seen from the inspector, so i have to send it with post i think Commented Apr 30, 2020 at 12:38

1 Answer 1

1

This is a simple example to demonstrate how to put a PHP variable's value into a JS variable:

<?php
$secret = 'secretHashValue';
?>

<script>
let secret = '<?= $secret ?>';
console.log(secret);
</script>

Please note that this code needs to be in a .php file. You cannot use this within a JS file, because JS files will be delivered as static files and therefore will not be interpreted as PHP code by the webserver.


I've composed a small example using a form to POST something. Just create these two files and then open /form.php in your browser.

form.php

<html>
<head>
</head>
<body>
    <form method="POST" action="/posted.php">
        <input type="text" name="postedValue">
        <button>Post this value</button>
    </form>
</body>
</html>

posted.php

<html>
<head>
</head>
<body>
    <?php
    $postedValue = $_POST['postedValue'];
    ?>

    <script>
    let postedValue = '<?= $postedValue ?>';
    alert('Posted value: ' + postedValue);
    </script>
</body>
</html>

Hope this makes things a little clearer.

Sign up to request clarification or add additional context in comments.

4 Comments

I must send the value from the php using post method, and get it in the javascript file using ajax, I have tried this but my teacher says its not valid for him, thanks anyway
That's weird, there are no ways of posting values from a server langauge (php) to clientside language (javascript) (even if a solution exists that I do not know if, it shouldn't exist because it's simply wrong!) Tell your teacher to rethink the task!
Wolfgangs solution here is the "way to go". Just note that <?= probably must be replaced with <?php echo $secret;?>
@PabloLópez I've edited my answer and added an example that you can try yourself. Please let me know if this helps.

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.