I created a php page and javascript code that contains some variables from the PHP page, this is a code that inserts php variables into the database using Javascript :

<?php
     $id = "Kevin";
     $user = "Calvin";
?>
<!-- include jquery library file-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- The ajax/jquery stuff -->
<script type="text/javascript">
    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: <?php echo json_encode($id); ?>, to: <?php echo json_encode($user); ?>},
    function(data){
        $("#message").html(data);
        $("#message").hide();
        $("#message").fadeIn(200); 
    });
    function clear() {
        $("#myre :input").each(  function() {
        $(this) .val('');
    });
    }
    }); 

   }
 } 
</script>

and in the full.php page

<?php
     mysql_connect("localhost","root","");
     mysql_select_db("db");
     $to=mysql_real_escape_string($_POST['to']);
     $from=mysql_real_escape_string($_POST['from']);
     $msg=mysql_real_escape_string($_POST['msgg']);
     $to = mysql_real_escape_string($to);
     $from = mysql_real_escape_string($from);
     $msg = mysql_real_escape_string($msg);     
     if(empty($msg)){
        exit();
     }
    $query=mysql_query("INSERT INTO `message`(`user`,`who`,`message`) VALUES ('$to','$from','$msg')");
    if($query){
        echo "Perfect!";
    }else{
        echo "Failed!!";
?>

so is there any way to put the Javascript code into another page and inject it using ajax?

share|improve this question
6  
For your own sake and any other developer looking at the code, use indentation. – h2ooooooo Jul 12 '13 at 11:51
    
code that inserts php variables into the database using Javascript – mishik Jul 12 '13 at 11:54
2  
Maybe there are 2K+ answered questions just like this one on SO. – Bart Jul 12 '13 at 11:58
1  
this question is ridiculous – Gunr Jesra Jul 12 '13 at 11:59
up vote 0 down vote accepted

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="myjs.js" />
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.