-5

In the code added here, I pass on value from PHP parameter to JS parameter: Both parameters are called 'stv'.

I wonder how do I do the exact opposite?

Thanks!

<script>
var stv="<?php echo $stv; ?>";
</script>
sillypurple
  • 193
  • 1
  • 1
  • 5

1 Answers1

1

send a request with ajax using jquery if using jquery already in document. If you don't have Jquery added, you can add it ath the end of your html body using a script tag and getting the download adress from google:

<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>


  function ajax(value) {
        var x = {
            'info': value //data to send can be json or something else
        };
        $.ajax({
            url: 'index.php', //page where to send
            type: 'POST', //type of request
            data: x, // data to send
            success: function(data) { //what happends when request is success 
                try {
                    getAjPromise(JSON.parse(data));
                } catch (e) {
                    console.log("error");
                }
            }
        });

    }

in PHP you check if there is a $_POST['info'], get the value and do something

Paulus
  • 11
  • 4
  • You show jQuery code here without telling the OP how to add the library. In all likelihood you're making things more confusing for the poster than helping. You cannot assume jQuery is in the document already. – Jay Blanchard Nov 06 '18 at 13:14
  • edited. but in my opinion, using ajax should be higher on learning level than knowing to import a library like Jquery. – Paulus Nov 06 '18 at 13:43
  • Then why didn't your example use VanillaJS? IMHO if you're teaching someone something, you teach them all they need to know about what you're teaching them. Did you know you had to include the library the first time you used jQuery? – Jay Blanchard Nov 06 '18 at 13:47
  • good point. he did use internal js script tag it makes sense he would not know how to use external sources. – Paulus Nov 06 '18 at 14:21