drewdan Posted December 19, 2012 Share Posted December 19, 2012 Hi Guys, I am currently writing some pretty messy code and I am having problems with it. I bascially have a script which will change the source of an image using jquery, but all of this is generated through a PHP file. So basically I have this being called using ajax: $sql="select * from vehicle_image where vehicle_id=".$_REQUEST["id"].""; $sql_res=mysqli_query($conn, $sql) or die(mysqli_error($conn)); while($array=mysqli_fetch_array($sql_res)) { $src=$array['image_src']; $variable = "\"../timthumb.php?src=images/" . $src. "&w=66.6&h=66.6\""; $content .= "<img onclick=\"changeImage(" . $variable. ")\" style=\"float:left;\" src=" . $variable . ">"; } echo $content; The line I am having the problems with is the $content .= line. specifically the onclick=" this bit here " part. However I write the code, however I escape the characters, it causes an error with different bits of the code, at the moment the problem with this is that is doesnt form the variable in the function properly. I am using FireBug to check that things are workly properly as when you use jQuery the source code does not update. Firebug says there is something wrong after changeImage( so I can only assume its my character escaping, but I really dont know. Any help on this would be great! Quote Link to comment https://forums.phpfreaks.com/topic/272172-having-trouble-escaping-characters-in-a-php-string-variable/ Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 an example error is always nice, especialy at this time of year. Also, doing a var_dump() of the $content varaible after it's been built helps to see what things actualy look like. Quote Link to comment https://forums.phpfreaks.com/topic/272172-having-trouble-escaping-characters-in-a-php-string-variable/#findComment-1400260 Share on other sites More sharing options...
drewdan Posted December 19, 2012 Author Share Posted December 19, 2012 Thanks for your reply! There are not really any errors as such. There are no php errors which suggests my PHP is fine, yet firebug says there is a syntax error and nothing else with the contents of changeImage() which suggests I have created the string incorrectly using the PHP. Using var_dump($content) gave me this: string(624) "" Does that mean anything? I have been looking at this for so long now I dont really quite know what to do with it, my brain is trying to avoid thinking when I look at it! You can see the script in "action" here: http://commercial-motors.com.82-165-151-28.hostiauwebhost.co.uk/man-truck/30-man-sales-topused If you click More Info... for Test Vehicle 1 a div box should appear with the content. The $content variable is responsible for the little images in the bottom right and clicking on of them should change the large image to whatever the small image was. Does this make any sense? Or should I give up and start again! Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/272172-having-trouble-escaping-characters-in-a-php-string-variable/#findComment-1400269 Share on other sites More sharing options...
drewdan Posted December 19, 2012 Author Share Posted December 19, 2012 Right, new update, whats actually happening, or not happening in this case is: onclick=\"changeImage(" . $variable. ")\" When this gets to runtime, the changeImage() remains empty. The variable definately contains information as futher along it makes the image appear. Just doesnt wanna appear in the brackets! Quote Link to comment https://forums.phpfreaks.com/topic/272172-having-trouble-escaping-characters-in-a-php-string-variable/#findComment-1400271 Share on other sites More sharing options...
drewdan Posted December 19, 2012 Author Share Posted December 19, 2012 Hi all, Its resolved now. For anyone who is interested: if($_REQUEST['action']=="ajax2") { $sql="select * from vehicle_image where vehicle_id=".$_REQUEST["id"].""; $sql_res=mysqli_query($conn, $sql) or die(mysqli_error($conn)); echo ' <script> function changeImage(src) { $(\'#theimage\').attr("src", "../timthumb.php?src=images/"+src+"&w=400&h=400&zc=2\""); } </script>'; while($array=mysqli_fetch_array($sql_res)) { $src=$array['image_src']; $variable = "\"../timthumb.php?src=images/$src&w=66.6&h=66.6\""; $content .= "<img onclick=\"changeImage('$src')\" style=\"float:left;\" src=" . $variable . ">"; } echo $content; exit(); } Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/272172-having-trouble-escaping-characters-in-a-php-string-variable/#findComment-1400274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.