How to inject javascript code that uses php variables into php page

Assign PHP variable’s value to java script variable :

<script type="text/javascript">
    var json{
    "id":<?php echo $id;?>,
    "user" : "<?php echo $user;?>"
    };
</script>

Change this line from your code :

 $.post('full.php', {msgg: msg, from: json.id, to: json.user}

Now you’ll have separate js file like:

      function send(e){
        if(e.keyCode == 13 && !e.shiftKey){

$(document).ready(function(){
//Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...

//Get values of the input fields and store it into the variables.
var msg=$("#reply").val();
clear();
//here you start PHP->>
//here we put the PHP Variables ^^^^^^^^^///\\\

//use the $.post() method to call insert.php file.. this is the ajax request
$.post('full.php', {msgg: msg, from: json.id, to: json.user},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(200); 
});
function clear() {
$("#myre :input").each(  function() {
    $(this) .val('');
    });
}
}); 

   }
    } 

Say this file is myjs.js now include it in php file:

<script type="text/javascript" src="https://stackoverflow.com/questions/17614282/myjs.js" />

Leave a Comment