1

I would be very grateful if somebody would take the time to read my code and could tell me a way to put variable $kakka in the script below, so that it works. As of now variable $kakka has no value because the php code is down there. Sorry I understand nothing about jQuery but I need this function to work.

Markup and 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>';

?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) [Here is an example of what happens when you continue to use `mysql_*` functions.](http://stackoverflow.com/questions/26299564/php-version-upgraded-cannot-use-few-functions) Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 17 '14 at 15:59
  • Hyvä tuo $kakka pökäle! – marko Oct 27 '16 at 14:19

2 Answers2

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>

Some things to consider

  • mysql_query is deprecated
  • The use of jquery latest is not recommended in production
Barry127
  • 1,212
  • 1
  • 12
  • 23
0

Just echo it into a variable -

var refreshId = setInterval(function()
{ 
  var kakka = '<?php echo $kakka; ?>';
  $('#puu').fadeOut("slow").text(kakka).fadeIn("slow");
}, 5000);
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • This won't work because $kakka is a string. You have to put define it like this in the script: var kakka = ''; – Barry127 Oct 17 '14 at 16:04