kts Posted January 19, 2009 Share Posted January 19, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/ Share on other sites More sharing options...
DeanWhitehouse Posted January 19, 2009 Share Posted January 19, 2009 AJAX? Or document.GetElementByid('') might help you Quote Link to comment https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/#findComment-740756 Share on other sites More sharing options...
ratcateme Posted January 19, 2009 Share Posted January 19, 2009 php is SERVER SIDE everything inside <? ?> or <?php ?> happens on the servers don't speak javascript. javascript is CLIENT SIDE so can only happen in the users browser Scott. Quote Link to comment https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/#findComment-740758 Share on other sites More sharing options...
kts Posted January 20, 2009 Author Share Posted January 20, 2009 Understood, I was just wondering if there are any tricks, I will continue working with the AJAX approach thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/#findComment-740855 Share on other sites More sharing options...
phparray Posted January 20, 2009 Share Posted January 20, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/#findComment-740865 Share on other sites More sharing options...
kts Posted January 20, 2009 Author Share Posted January 20, 2009 yeah, unfortunately the system I have set up needs the ID from it, so I will have to take the ajax approach. Always fun to learn new things! hah Quote Link to comment https://forums.phpfreaks.com/topic/141515-phpjavascript-issue/#findComment-741207 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.