Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  155] [ 3]  / answers: 1 / hits: 20876  / 13 Years ago, mon, september 19, 2011, 12:00:00

I am attempting to make a gallery that calls the image names from a flat file database using the PHP 'fgets' function. There are different sections in the gallery, each with it's own default image, and a small list of images that the users can select from. Everything is working fine, except for one button.



I have one button on the page that is supposed to reset all the galleries to their default images using Javascript OnClick. It works exactly as I want it to, with one small hitch: It copies the line break at the end of the line allong with the characters on the line, breaking the Javascript.



The offending code:



function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';

//This should output the proper javascript, but does not
<?php
$a = fopen('c.txt','r');
if (!$a) {echo 'ERROR: Unable to open file.'; exit;}
$b = fgets($a);
echo document.getElementById('i1').src='$b';;
fclose($a);
?>

}


How it outputs:



function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';
document.getElementById('i1').src='00.jpg
';}


As you can see, the ending quotation mark and the semi-colon falls on the next line, and this breaks the button.



With the files I'm using now, I can get around this problem by changing, fgets($a) to, fgets($a, 7) but I need to have it grab the entire line so that if the client decides to enter a file with a longer name, it does not break the gallery on them.


More From » php

 Answers
22

Your best bet is to use the php trim() function. See http://php.net/manual/en/function.trim.php



$b = trim(fgets($a));

[#90022] Saturday, September 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raymondorlandok

Total Points: 530
Total Questions: 110
Total Answers: 96

Location: Lebanon
Member since Wed, Dec 21, 2022
1 Year ago
;