简体   繁体   中英

PHP inside AJAX post call

A PHP page contains a variable $hash and has a button. The button's click event is handled by jQuery. I want jQuery to do an AJAX call as follows:

$.post("testupdate.php",  {hash:"<?php echo $hash; ?>"} , function(data) {

           $("#body").html(data);
});   

However, the PHP code <?php echo $hash; ?> <?php echo $hash; ?> inserted inside the AJAX call isn't working. How can I make it work please? Thanks.


Update: I made it work by inserting a script element in the html page which is processed before the jQuery call:

<script> var hash = "<?php echo $hash; ?>"; </script>

And then did the following in the jQuery function call:

$.post("testupdate.php",  {hashpost:hash} , function(data) {

           $("#body").html(data);
});   

Try like this..

<script>
var hash = '<?php echo $hash; ?>';
$.ajax({         
        url : "testupdate.php", 
        type: 'POST',  
        data: {'hash': hash},               
        success: function (response) {          
            console.log(response);          
        },
        error: function(request,  error , status) {
            console.log(error); 
        }       
    });
</script>

Hope this will solve your issue.

I made it work by inserting a script element in the html page which is processed before the jQuery call:

<script> var hash = "<?php echo $hash; ?>"; </script>

And then did the following in the jQuery function call:

$.post("testupdate.php",  {hashpost:hash} , function(data) {

           $("#body").html(data);
});   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM