PHP jQuery代码组合

时间:2014-10-17 15:58:07

标签: php jquery variables

如果有人花时间阅读我的代码并且可以告诉我一种方法将变量$ kakka放在下面的脚本中,那么我会非常感激。截至目前变量$ kakka没有价值,因为php代码在那里。对不起我对jQuery一无所知,但我需要这个功能才能工作。

标记和jQuery

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var refreshId = setInterval(function()
{
  $('#puu').fadeOut("slow").text($kakka).fadeIn("slow");
}, 5000);
</script>
</head>
<body>

PHP

<?php
$rs = mysql_query("SELECT * FROM users WHERE id='$_SESSION[user_id]'");
  while 
    ($row= mysql_fetch_array($rs))
    {$starter= $row['id'];
     $user_name= $row['user_name'];}


$starterID=$starter;
$companyID=$_GET['id'];


$input = $_POST['viesti'];


date_default_timezone_set('Europe/Helsinki');
$timestamp = date('h:i', time());


$file = $companyID." and ".$starterID.".txt";


if (file_exists($file)) {
$kakka = $companyID." and ".$starterID.".txt";
} else {
$kakka = $starterID." and ".$companyID.".txt";
}


$current = file_get_contents($kakka);

if(isset($_POST['viesti']) && $_POST['viesti'] != null){
$currents= $current. "<b>$user_name</b> <br> $input $timestamp\n<br>";
$shipuli= "<b>$user_name</b> <br> $input $timestamp\n<br>";
file_put_contents($kakka, $currents);
}
echo '<div id="puu">'.$current.$shipuli.'</div>';

?>

2 个答案:

答案 0 :(得分:2)

<?php
$rs = mysql_query("SELECT * FROM users WHERE id='$_SESSION[user_id]'");
  while 
    ($row= mysql_fetch_array($rs))
    {$starter= $row['id'];
     $user_name= $row['user_name'];}


$starterID=$starter;
$companyID=$_GET['id'];


$input = $_POST['viesti'];


date_default_timezone_set('Europe/Helsinki');
$timestamp = date('h:i', time());


$file = $companyID." and ".$starterID.".txt";


if (file_exists($file)) {
$kakka = $companyID." and ".$starterID.".txt";
} else {
$kakka = $starterID." and ".$companyID.".txt";
}


$current = file_get_contents($kakka);

if(isset($_POST['viesti']) && $_POST['viesti'] != null){
$currents= $current. "<b>$user_name</b> <br> $input $timestamp\n<br>";
$shipuli= "<b>$user_name</b> <br> $input $timestamp\n<br>";
file_put_contents($kakka, $currents);
}
echo '<div id="puu">'.$current.$shipuli.'</div>';

?>

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var refreshId = setInterval(function()
{
  $('#puu').fadeOut("slow").text('<?php echo $kakka;?>').fadeIn("slow");
}, 5000);
</script>
</head>
<body>

需要考虑的一些事项

  • 不推荐使用mysql_query
  • 不建议在制作中使用最新的jquery

答案 1 :(得分:0)

将它回显到变量 -

var refreshId = setInterval(function()
{ 
  var kakka = '<?php echo $kakka; ?>';
  $('#puu').fadeOut("slow").text(kakka).fadeIn("slow");
}, 5000);