Jump to content

PHP/Javascript issue


kts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.