Jump to content

Having Trouble Escaping Characters In A Php String Variable!


drewdan

Recommended Posts

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.