ukscotth Posted October 25, 2011 Share Posted October 25, 2011 Hi, I need to run a database insert when somebody clicks on an image. Should I use a php function ? or javascript ? or something else ? Many thanks in advance, Scott. Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/ Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 If you want seemless, use an onClick event that will fire an Ajax call to a PHP file that can run the insert for you. If you want a page refresh, you could use a form/window.location to send the data to be added. Will the image click do anything else? Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282016 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 Ok thanks. Its basically for a picture voting facebook app. The user picks which picture is best out of 2 random ones that are displayed. Ideally I would like it so when the user picks one of the pictures it seemlessly inserts there choice details into the database and also changes the 2 images so they can make there next vote, all without the need of refreshing the page but I think this is going to be too hard for me as im a newbie. But I guess the best way would be like you said to use ajax, that way a seperate file could update the database and change the 2 images ready for the next vote, is that right ? Thanks, Scott. Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282019 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 Correct. Have the Ajax serve 2 purposes. Once the user clicks the image, it sends the data across to PHP. You can then get PHP to return the next set of images and display them on the page, with javascript. Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282021 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 Ok great thanks. Il start researching and see if I can learn how to put it into action Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282022 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 Would I start with something like this in the head section of the page ? <script type="text/javascript"> function vote() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","vote.php?voter='not sure how to get the data here'",true); xmlhttp.send(); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282029 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 or like this ? <script type="text/javascript"> function vote(voter,win,lose) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET",'vote.php?voter='+voter'&win='+win'&lose='+lose,true); xmlhttp.send(); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282031 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 Something like that would work, as long as you are passing the correct information into the function. Without over crowding your brain, jQuery makes this task alot simpler Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282033 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 Ok thanks and would the code on the img look like this ? onclick="javascript:vote(<?php echo 'test,test2,test3';?>);" Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282035 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 You do not need javascript: infront of the vote function (only really need that in href attributes) Also if each of those items are individual strings you will need to wrap them in quotes individually. vote(<?php echo "'test','test2','test3'";?>) or something like that. Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282036 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 ok thanks, nothing seems to be happening when i try and run it any ideas ? heres my simple vote.php code : <?php include_once "database.php"; mysql_query("INSERT INTO battles (voter, win, lose) VALUES ('".$_GET['voter']."','".$_GET['win']."', '".$_GET['lose']."')"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282037 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 Are you sure that the javascript function is executing correctly? Check for errors etc. Also, you should always escape your data on a mysql insert using mysql_real_escape_string() Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282038 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 im not sure how to be honest lol Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282039 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 Haha ok, what browser are you using? Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282040 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 lol ie9 Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282041 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 Press F12 Should open the Dev tools, look in Console for JS errors. Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282044 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 Ok thanks, it brings up a few things but none of them seem related : SCRIPT1006: Expected ')' home.php, line 31 character 43 SEC7115: :visited and :link styles can only differ by color. Some styles were not applied to :visited. home.php SCRIPT438: Object doesn't support property or method 'tabs' home.php, line 57 character 17 SCRIPT438: Object doesn't support property or method 'tabs' home.php, line 57 character 17 Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282047 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 As a guess im going to say its line 31 You are missing some +'s xmlhttp.open("GET",'vote.php?voter='+voter+'&win='+win+'&lose='+lose,true); Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282049 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 just noticed it comes up with this when i click the image : Line: 206 Error: 'vote' is undefined Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282050 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 Do you want to post the whole code? Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282052 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 ahh the data is being inserted into the database now Allthough im getting a lot of https errors which i guess is another issue : SEC7111: HTTPS security is compromised by http://connect.facebook.net/en_US/all.js home.php SEC7111: HTTPS security is compromised by http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js home.php SCRIPT5009: 'FB' is undefined home.php, line 24 character 8 SCRIPT5009: '$' is undefined home.php, line 34 character 13 SEC7115: :visited and :link styles can only differ by color. Some styles were not applied to :visited. home.php SEC7111: HTTPS security is compromised by http://static.ak.fbcdn.net/rsrc.php/v1/yU/r/74MZs-6P2Dx.js home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/261069_1811448158_6173435_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/273299_825895981_143161359_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369516_100001956697205_1307267885_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/370746_100000063081262_1163655936_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186326_1156985007_724326695_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/275482_522819544_1496773110_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186432_1138683827_161997488_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/274793_100002405641353_902279529_q.jpg home.php SEC7111: HTTPS security is compromised by http://static.ak.fbcdn.net/rsrc.php/v1/ys/r/MiVDw0H5WyN.gif home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/261069_1811448158_6173435_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/261069_1811448158_6173435_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/273299_825895981_143161359_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/273299_825895981_143161359_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369516_100001956697205_1307267885_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369516_100001956697205_1307267885_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/370746_100000063081262_1163655936_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/370746_100000063081262_1163655936_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186326_1156985007_724326695_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186326_1156985007_724326695_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/275482_522819544_1496773110_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/275482_522819544_1496773110_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186432_1138683827_161997488_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186432_1138683827_161997488_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/274793_100002405641353_902279529_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/274793_100002405641353_902279529_q.jpg home.php SEC7111: HTTPS security is compromised by http://static.ak.fbcdn.net/rsrc.php/v1/ys/r/MiVDw0H5WyN.gif home.php SEC7111: HTTPS security is compromised by http://static.ak.fbcdn.net/rsrc.php/v1/ys/r/MiVDw0H5WyN.gif home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/261069_1811448158_6173435_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369958_809560327_370090192_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/273299_825895981_143161359_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-ash2/369516_100001956697205_1307267885_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/370746_100000063081262_1163655936_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186326_1156985007_724326695_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/275482_522819544_1496773110_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/186432_1138683827_161997488_q.jpg home.php SEC7111: HTTPS security is compromised by http://profile.ak.fbcdn.net/hprofile-ak-snc4/274793_100002405641353_902279529_q.jpg home.php SEC7111: HTTPS security is compromised by http://static.ak.fbcdn.net/rsrc.php/v1/ys/r/MiVDw0H5WyN.gif home.php SCRIPT5007: Unable to set value of the property 'innerHTML': object is null or undefined all.js, line 6 character 1252 SCRIPT5007: Unable to set value of the property 'innerHTML': object is null or undefined all.js, line 6 character 1252 Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282054 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 My guess is you are running your site on HTTPS and those items listed are not. Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282056 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 yep facebook now requires all apps to run from https. Il worry about that later but for now the ajax is working, thanks so much for your help. Now all I have to do is work out how to make it change the images lol Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282057 Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 No worries. Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282061 Share on other sites More sharing options...
ukscotth Posted October 25, 2011 Author Share Posted October 25, 2011 sorry but how can i get variables back from vote.php ? so if vote.php pulls 2 more random image urls from the database, how would i retrieve the 2 urls back from vote.php in the main file ? if that makes sense lol Quote Link to comment https://forums.phpfreaks.com/topic/249770-running-a-database-insert-on-image-click/#findComment-1282071 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.