Jump to content

Recommended Posts

I have a drop down box with a ID value. I am trying to "onchange" this to an image specified by the database. Due to the nature of javascript and php variables not interacting well, I am having issues.

 

Here is what I have:

 

?><script language="javascript">
     var image_id = document.<? tep_output_string($name); ?>.value;

 <? $q = mysql_query("SELECT products_image WHERE products_id = '".$_GET['pID']."' AND attribute_id = '". + document.write('image_id') + "");?>
     </script>
  <?
  $eek = mysql_fetch_array($q);
     $field = '<select onchange="alert('.$eek["products_image"].');" name="' . tep_output_string($name) . '"';
 ?>

 

The document.write clearly doesn't work since I am still in PHP tags. Is there a way around this?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/
Share on other sites

If all you want to do is display an image onchange of a select box you could use php to populate the select option values with your image file names instead of ids.  Using ids would force you to use ajax but using file names would allow you to use a more dhtml approach.  Of course I don't know what else you want to do with that box if you have to use ids for another reason than ajax is your friend.

 

pseudo code for the dhtml approach

 

js function

function showImage(file,div)
{
  var img = "<img src='/images/"+file+"' />";
  document.getElementById(div).innerHTML = img;
}

 

onchange for your select tag

onchange="showImage(this.value,'imageDiv')"

 

div tag where the image will appear

<div id="imageDiv"></div>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/#findComment-740865
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.