lukep11a Posted January 19, 2011 Share Posted January 19, 2011 Hi, hope I have posted this in the right place, I have the following code which contains PHP and javascript, where both work on there own but when I try putting them together I keep getting errors: <?php include 'config.php'; include 'opendb.php'; $var = 'Smith'; $query = "SELECT * FROM search_directory WHERE merchant LIKE \"%$var%\""; $result = mysql_query($query); $id = $row{"id"}; while($row = mysql_fetch_assoc($result)) { echo "Name: {$row['name']} <br>"; echo "Subject: {$row['title']} <br><br>"; } include 'closedb.php'; ?> <a href="javascript:;" onclick="document.getElementById('.....').style.display='block';window.open('http://www.google.co.uk');"><img src="../../graphics/Click here.png" border="0" /></a> <div id="....." style="display:none;"> ...your text here </div> What I have been trying to do is get $id variable from the php code to replace the '.....' in the javascript and div id. I have tried putting it into an echo statement which seemed to work with the div id but the brought up errors when i tried with the javascript. If anyone knows how to solve this it would be very much appreciated. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Goose Posted January 19, 2011 Share Posted January 19, 2011 You should be able to replace the ..... with <?php echo $id; ?> Does that not work? Also, on line 8, which says "$id = $row{"id"};", that should look like this: $id = $row["id"]; You should use square braces when notating an array. Lastly, you are trying to store the value of $row['id'] into the variable $id, but when you are doing that assignment the variable $row is not defined yet. Quote Link to comment Share on other sites More sharing options...
lukep11a Posted January 19, 2011 Author Share Posted January 19, 2011 Thanks, thats brilliant, managed to get it to work using what you said! Do you know how to make this part of the coding into a repeating region? <a href="javascript:;" onclick="document.getElementById('<?php echo $id; ?>').style.display='block';window.open('http://www.google.co.uk');"><img src="../../graphics/Click here.png" border="0" /></a> <div id="<?php echo $id; ?>" style="display:none;"> ...your text here </div> Quote Link to comment Share on other sites More sharing options...
Goose Posted January 19, 2011 Share Posted January 19, 2011 Lines 12 and 13 are the repeating lines. If you insert that code in between the curly brackets on line 11 and 14, those will repeat. It will look something like this: while($row = mysql_fetch_assoc($result)) { echo "Name: {$row['name']} <br>"; echo "Subject: {$row['title']} <br><br>"; ?> <a href="javascript:;" onclick="document.getElementById('<?php echo $row['id']; ?>').style.display='block'; window.open('http://www.google.co.uk');"><img src="../../graphics/Click here.png" border="0" /></a> <div id="<?php echo $row['id']; ?>" style="display:none;"> ...your text here </div> <?php } Quote Link to comment Share on other sites More sharing options...
lukep11a Posted January 19, 2011 Author Share Posted January 19, 2011 Thanks for your quick response, that has made the image appear twice for the two rows it represents in the table which is correct but now when you click on the image to go to the link nothing happens and no hidden text is displayed... any ideas? Quote Link to comment Share on other sites More sharing options...
Goose Posted January 21, 2011 Share Posted January 21, 2011 Sounds like a javascript problem, and I am guessing it has something to do with the IDs getting used more than once, or something along those lines. Quote Link to comment 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.