2014-10-17 68 views
1

如果有人花時間閱讀我的代碼,並且可以告訴我一種將變量$ kakka放在下面的腳本中的方法,我將不勝感激,以便它能夠正常工作。截至目前變量$ kakka沒有價值,因爲PHP代碼在那裏。抱歉,我對jQuery一無所知,但我需要使用此功能。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>'; 

?> 
+0

(http://stackoverflow.com/questions/60174/ (這是一個例子,當你繼續使用'mysql_ *'函數時會發生什麼。](http://stackoverflow.com/questions/26299564/php -version-upgraded-can-use-few-functions)改爲了解[準備語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php。 net/pdo)或[MySQLi](http://us1.php.net/mysqli)。 [本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定。 – 2014-10-17 15:59:13

+0

Hyvätuo $ kakkapökäle! – marko 2016-10-27 14:19:49

回答

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的最新不在生產建議
0

只是附和它變成一個變量 - [!防止SQL注入]

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

這不起作用,因爲$ kakka是一個字符串。你必須在腳本中這樣定義它:var kakka ='<?php echo $ kakka;?>'; – Barry127 2014-10-17 16:04:40