Page 1 of 1

php not working in javascript

Posted: Thu Feb 07, 2008 7:59 am
by tomdagom
hi i am creating a slideshow, the following is the code i am using

Code: Select all

 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>Garryspillane Gaa </title>
<body style="background-color:#313131;color:#FFFFFF">
<?php
 
$conn = @mysql_connect("localhost", "admin", "admin2345");
 
 
if (!@mysql_select_db("temp", $conn))
  die("<p>Error selecting DB-DEVEL database: <i>" . mysql_error() . "</i></p>");
  $current_image;
  $query = "SELECT current FROM current_image WHERE path='2005_championship'";
  $result = mysql_query($query);
while ($property = mysql_fetch_array($result))
{
$current_image=($property["current"]);
}
 
function getImage($num){
 
    $query = "SELECT * FROM gallery WHERE number='$num'";
    $result = mysql_query($query);
        while ($property = mysql_fetch_array($result))
        {
         $name = ($property["image_name"]);
         $loc= ($property["location"]);
         $description= ($property["description"]);
        }
         $url=$loc.'/'.$name;
        return $url;
    }
    
    function getCurrent(){
    $query = "SELECT current FROM current_image where path='2005_championship'";
    $result = mysql_query($query);
        while ($property = mysql_fetch_array($result))
        {
         $current = ($property["current"]);
        }
        return $current;
    }
    
    function total_images(){
    $max_query = "SELECT MAX(number) FROM gallery";
    $max_result = mysql_query($max_query);
    while ($property = mysql_fetch_array($max_result))
        {
         $number = ($property["MAX(number)"]);
        }
        return $number;
    }
 
    function next_Image(){
        echo("<script LANGUAGE='javascript'>temp();</script>");
        $temp=total_images();
        $num=getCurrent()+1;
        if($num<=$temp){
        $query = "update current_image set current='$num'  where path='2005_championship'";
        mysql_query($query);
        }   
    }
    
    function previous_Image(){
        $temp1=1;
        $num2=getCurrent()-1;
        if($num2>=$temp1){
        $query1 = "update current_image set current='$num2'  where path='2005_championship'";
        mysql_query($query1);
        }
    }
    
    $image=getImage(5);
    
    function test(){
    
    return "hello";
    }
?>
<table width="100%" height="100%">
<tr>
<td>
<div style="text-align:center">
<img src="<?php  echo(getImage($current_image)); ?>" name="main_image" width="" height=""/>
 
</div>
</td>
</tr>
 
<tr>
<td>
<table  width="100%" style="text-align:center">
<tr>
<td>
<script>
function temp(){
var value='<?php echo($image)?>';
alert('<?php echo($image)?>');
document.main_image.src=value;
}
</script>
<button onClick="temp()" name="next_button"> hello</button>
<button onClick="<?php next_Image();?>" style="background-color:#313131;border:0;margin:0;padding:0;"><img src="images/previous.jpg"/> 
</button>
 
<button style="background-color:#313131;border:0;margin:0;padding:0;"><img src="images/home.jpg"/> 
</button>
 
<button onClick="<?php next_Image();?>" style="background-color:#313131;border:0;margin:0;padding:0;"><img src="images/next.jpg"/> 
</button>
</td>
</tr>
</table>
</td>
</tr>
 
<tr>
<td>
<table width="100%">
<tr>
<td width="15%">
</td>
<td width="70%">
<div style="white-space:nowrap; width: 700px;height:125px; overflow:auto; scrollbar-arrow-color:  green; scrollbar-face-color: #e7e7e7; scrollbar-3dlight-color: #a0a0a0; scrollbar-darkshadow-color:
#888888;scrollbar-base-color:#313131;">
 
<table style="height:100px">
<tr>
<?php
$width="100px";
$height="80px";
for ($num=1; $num <= 13; $num++ ) {
$image_path=getImage("$num");
if ($num==3){
$width="120px";
$height="100px";
}
 
echo("<td><img src='$image_path' width='$width' height='$height' /></td>");
$width="100px";
$height="80px";
}
?>
 
</tr>
</table>
 
</div>
 </td>
 <td width="15%">
</td>
</tr>
</table>
</td>
</tr>
 
</table>
<?php
 mysql_close ($conn);
?>
</body>
</html>
when i click on the button hello (this is just a test button), the javascript function temp() is being executed. I am getting an unterminated string literal error, because when i call var value='<?php echo($image)?>'; the result i am getting on the client side is
var value='gallery/2005_images
/2005_5.jpg';
As you can see the text is going the the next line. Does echo automically do a newline and if so what should i use instead of this.

Re: php not working in javascript

Posted: Thu Feb 07, 2008 8:20 am
by jrlooney
Hello, did you check your database value to see if the line return exists in the value itself? You can strip the value either when it's input, or on your output code above. Here is a function:

Code: Select all

 
function remove_line_return($source)
{
    $search = array ('@([\r\n])[\s]+@');                    // evaluate as php
    $replace = array (' ');
    $text = preg_replace($search, $replace, $source);
    return $text;
}
 
So then in your code, at line 73, you can take $image and stip out line returns like so:

Code: Select all

 
$image = remove_line_return($image);
 

Re: php not working in javascript

Posted: Thu Feb 07, 2008 8:33 am
by tomdagom
nope that didnt work, i have tried removing the newline characters using the following code

Code: Select all

 
$string = str_replace("\n", "", $url);
$string = str_replace("\r", "", $url);
 

Re: php not working in javascript

Posted: Thu Feb 07, 2008 10:57 am
by Jonah Bron
Maybe this?

Code: Select all

$string = str_replace('
', '', $string);

Re: php not working in javascript

Posted: Thu Feb 07, 2008 11:12 am
by tomdagom
ok i fixed the problem, i was returning $url instead of $string, thanks for all yer help