Barny74 Posted January 23, 2017 Share Posted January 23, 2017 (edited) Hi, I have been trying to code some javascript to detect when an image does not load , but having no luck as new to javascript. I am trying to replace the below , if it does not load, echo "<img src='".$row ['url_img']."' /><br />"; with an image called no_img.jpg if (isset($_GET['id'])) { $sql = "SELECT w.whisky_name , price , date , avprice ,url_img ,whisky_id FROM test_db t JOIN whisky w USING (whisky_id) JOIN ( SELECT whisky_id , AVG(price) as avprice FROM test_db WHERE whisky_id = :id GROUP BY whisky_id ) avcalc USING (whisky_id) ORDER BY date DESC LIMIT 1"; $stmt = $conn->prepare($sql); $stmt->execute( array( 'id' => $_GET['id']) ); $row = $stmt->fetch(); echo "<div class='details'>"; echo "<br>"; echo $row['whisky_name']; echo "<br><br>"; echo "Average price based on all sales" ; echo"<br><br>"; echo "<div class= 'price'>"; echo " £"; echo floor ($row ['avprice']); echo "</div>"; echo "<br>"; echo "<div class='img_border'>"; echo "<img src='".$row ['url_img']."' /><br />"; echo "<a href='records.php?id={$row['whisky_id']}' >Click here for more detailed results</a>"; echo "<br>"; echo "</div>"; } ?> Appreciate the help Edited January 23, 2017 by Barny74 Quote Link to comment https://forums.phpfreaks.com/topic/303011-java-script-to-detect-and-load-no-img/ Share on other sites More sharing options...
Barny74 Posted January 23, 2017 Author Share Posted January 23, 2017 Sorry , just updated the code as I pasted the wrong code in Quote Link to comment https://forums.phpfreaks.com/topic/303011-java-script-to-detect-and-load-no-img/#findComment-1541922 Share on other sites More sharing options...
Barny74 Posted January 23, 2017 Author Share Posted January 23, 2017 I have tried the below but image does not load. echo "<img src='".$row ['url_img']."' onerror='this.onerror=null';this.src='no_img.jpg' /><br />"; Quote Link to comment https://forums.phpfreaks.com/topic/303011-java-script-to-detect-and-load-no-img/#findComment-1541923 Share on other sites More sharing options...
mac_gyver Posted January 23, 2017 Share Posted January 23, 2017 i think the wording of your problem is perhaps causing unnecessary work in solving it. are you trying to use a default image when you have no image defined in your database table, i.e. if $row['url_img'] is empty, use no_img.jpg as the image? if so, you would just put a conditional test in the php code that's producing the output. Quote Link to comment https://forums.phpfreaks.com/topic/303011-java-script-to-detect-and-load-no-img/#findComment-1541926 Share on other sites More sharing options...
Barny74 Posted January 24, 2017 Author Share Posted January 24, 2017 Hi. If the img that the code is currently pointing to via the db is taken down , i would like to have a second image as backup. And that second image is on my seever. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/303011-java-script-to-detect-and-load-no-img/#findComment-1541928 Share on other sites More sharing options...
Solution Barny74 Posted January 24, 2017 Author Solution Share Posted January 24, 2017 I got my answer thanks. My script.php was not loading. Works fine now. Quote Link to comment https://forums.phpfreaks.com/topic/303011-java-script-to-detect-and-load-no-img/#findComment-1541939 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.