2013-07-12 70 views
-5

我創建一個PHP頁面,包含從PHP頁面的某些變量的JavaScript代碼JavaScript代碼,這是使用Javascript是插入PHP變量到數據庫代碼:如何注入,使用PHP變量到PHP頁面

<?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> 

,並在full.php頁

<?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!!"; 
?> 

那麼,有沒有辦法把Javascript代碼到另一個頁面,並使用AJAX注入呢?

+6

爲了您和任何其他開發人員查看代碼,**使用縮進**。 – h2ooooooo

+0

'使用Javascript'將php變量插入數據庫的代碼# – mishik

+2

也許有這樣的[2K +回答問題](http://stackoverflow.com/search?q=javascript+php+variable+into+database)所以。 – Bart

回答

0

分配PHP變量的值,以Java腳本變量:

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

改變這一行從您的代碼:

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

現在,你有一個像單獨的js文件:

 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(''); 
    }); 
} 
}); 

    } 
    } 

說這個文件是myjs.js現在將它包含在php文件中:

<script type="text/javascript" src="myjs.js" /> 
相關問題