Home » Php » How to embed PHP inside the jquery script?

How to embed PHP inside the jquery script?

Posted by: admin June 30, 2018 Leave a comment

Questions:

I need to copy the value of a jquery variable into a PHP variable.

My code:

<script>
$(document).ready(function() {
  var gtext = "Sample text";
  console.log(gtext); //I get the gtext printed in console     
  <?php   $variablephp = "<script>document.write(gtext)</script>" ?>
  <?php echo $variablephp; ?> // I cannot get printed this php variable       
  <?php echo "hi"; ?> //This echo statement not get printed
});
</script>

Can anyone please help on this?

My question is different in the sense I want to store a jquery value inside a PHP variable. Not a PHP variable into a jquery element.

Answers: