Mortenjan Posted February 23, 2009 Share Posted February 23, 2009 Hi. How can i achieve this? <script language="JavaScript" type="text/javascript"> function CngPicture(bilde){ Movies= document.getElementById('movies'); Index = Movies.selectedIndex; Value = Movies.options[index].value; <?php $query = "SELECT picture FROM `movies` WHERE id='"?> Value <?php "'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $picture = $row['picture']; ?> document.getElementById('bilde').src='<?php echo $picture; ?>'; } </script> Any help is appreciated. Regards Morten. Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/ Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 How can I achieve this? That has to be the worst question in the word. Please post EXACTLY what you are trying to achieve in words, what you need help with, what you have tried and not tried and how is it "not working". Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/#findComment-769193 Share on other sites More sharing options...
kenrbnsn Posted February 23, 2009 Share Posted February 23, 2009 To do what I think you want to do you need to use AJAX techniques. Ken Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/#findComment-769195 Share on other sites More sharing options...
Mortenjan Posted February 23, 2009 Author Share Posted February 23, 2009 Yes, i do think i have to use AJAX after what i've read on the web. But i have never used AJAX before. So any help on building that code would be appreciated. Øhm.. How should i explain my issue.. I am trying to get a link(Picture) from a database where the link is changing depending to the Dropdown value into a javascript code.. The dropdown value is the same as the Movie ID in the database. Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/#findComment-769204 Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 or, you need to use a PHP handler script to return the images....let me see if i can explain: in your script, your JS would look like this: <script language="JavaScript" type="text/javascript"> function CngPicture(bilde){ Movies = document.getElementById('movies'); Index = Movies.selectedIndex; Value = Movies.options[index].value; document.getElementById('bilde').src = 'movieImage.php?id=' + Value; } </script> then, create a file called movieImage.php with the following code: <?php //Put your MySQL Connection info here $id = mysql_real_escape_string($_GET['id']); $query = "SELECT picture FROM `movies` WHERE id='$id'"; $result = mysql_query($query); $row = mysql_fetch_array($result); header('Location: '. $row['picture']); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/#findComment-769205 Share on other sites More sharing options...
Mortenjan Posted February 23, 2009 Author Share Posted February 23, 2009 Oh damn! It works! Thanks alot! Hm, the picture got the link: http://www.vinstrakino.co.cc/php/movieImage.php?id=100 on the first selected dropdown (the id is correct) (the link returns the correct picture, so it has to be something with the javascript?) It didnt return the link from the database. Anything ive done wrong? <script language="JavaScript" type="text/javascript"> function CngPicture(bilde){ Movies = document.getElementById('movies'); Index = Movies.selectedIndex; Value = Movies.options[index].value; document.getElementById('bilde').src = 'php/movieImage.php?id=' + Value; } </script> <?php require 'tilkobling.php'; $id = mysql_real_escape_string($_GET['id']); $query = "SELECT picture FROM `movies` WHERE id='$id'"; $result = mysql_query($query); $row = mysql_fetch_array($result); header('Location: '. $row['picture']); exit; ?> or, you need to use a PHP handler script to return the images....let me see if i can explain: in your script, your JS would look like this: <script language="JavaScript" type="text/javascript"> function CngPicture(bilde){ Movies = document.getElementById('movies'); Index = Movies.selectedIndex; Value = Movies.options[index].value; document.getElementById('bilde').src = 'movieImage.php?id=' + Value; } </script> then, create a file called movieImage.php with the following code: <?php //Put your MySQL Connection info here $id = mysql_real_escape_string($_GET['id']); $query = "SELECT picture FROM `movies` WHERE id='$id'"; $result = mysql_query($query); $row = mysql_fetch_array($result); header('Location: '. $row['picture']); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/#findComment-769209 Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 That code looks fine. And I went to http://www.vinstrakino.co.cc/ and the dropdown seems to work fine. can you elaborate on your problem? Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/#findComment-769217 Share on other sites More sharing options...
Mortenjan Posted February 23, 2009 Author Share Posted February 23, 2009 Works fine:) Was an issue with the database connection. Thanks alot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/146513-javascript-with-php-problem/#findComment-769223 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.