如何将使用php变量的javascript代码注入php页面
How to inject javascript code that uses php variables into php page
我创建了一个php页面和javascript代码,其中包含php页面中的一些变量,这是一个使用javascript将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>
以及在完整的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注入呢?
将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" />
相关文章:
- PHP Javascript显示/隐藏按钮不工作
- php javascript代码从javascript调用php函数
- 如何在php/javascript中制作定时器和警报
- 带有数据输入的PHP/JavaScript web交互式图谱
- PHP - Javascript integration?
- 从复选框 ajax php javascript 中检索值
- 验证此图像验证码.Jquery PHP & javascript.
- Facebook PHP+JavaScript-在页面加载之前导航时出现异常
- PHP/JavaScript-检测当前打开页面的用户
- Php-Javascript-我如何知道哪些页面是彩色的,哪些是黑白的
- PHP+Javascript解析这两个响应
- 创建php/javascript倒计时计时器时出现问题
- 转换“;2012年7月15日星期日00:00:00 GMT+0700(东南亚标准时间)“;到php/javascript
- 与php/javascript函数调用混淆
- 从下拉框中获取选定的值,并使用来填充文本框php javascript
- 如何使用php+javascript更改HTML表的内容
- Php javascript不显示数据
- PHP/JavaScript MP3播放器,用于采样和销售歌曲
- Integration PHP + Javascript
- Adsense Onclick(php, javascript)