1

Hi I am building a video voting website that allows voting on videos. I want the user to be able to vote without interrupting the play of the video.

here is the snip I am using.

if ($_POST['vote']) {
    $sql = mysql_query("UPDATE videos SET vid_votes=vid_votes+1 WHERE vid_id=$vid_id"); 
}
$votebutton ="<form action='index.php?id=$vid_id' method='post' enctype='multipart/form-data'><input name='vote' type='hidden' id='vote' value='$vid_id'><input type='submit' name='Submit' value='Vote for it!' /></form>";

I echo out the vote or not vote based on a query of whether or the logged in voter already voted today.

This script logs a vote and returns the user to the video he was watching. More scripting will change this to an unvote button if the user has already voted within a day. However my main concern is How can I get this button to run the script without restarting the video?

outis
  • 75,655
  • 22
  • 151
  • 221
  • 1
    This doesn't have anything to do with PHP/MySQL really. You need to use AJAX, which is JavaScript client-side. Are you using anything like jQuery? If not, I'd recommend it, as it wraps up AJAX very nicely. – Brad Apr 26 '12 at 03:38
  • Well it really is php/mysql but I think ajax will be needed here too. Duhh do you have a solution? – Leonard Farneth Apr 26 '12 at 03:44
  • Yes, but since you can't be bothered to answer my question, I can't really answer appropriately. – Brad Apr 26 '12 at 03:48
  • Im sorry because you declared that php and mysql werent a factor I mistook you for a comment trol. I now see your question. I apologize. I did not include j query. However I could and would you please share that soloution? – Leonard Farneth Apr 26 '12 at 03:51
  • I will go for Ajax too. It is the only way you could submit the votes without interrupting the video. [here](http://api.jquery.com/jQuery.ajax/) is the document you need to read if you wanna use jquery + Ajax. – Jermin Bazazian Apr 26 '12 at 03:56
  • Thanks for the link Jermin You rock! I am on it now. – Leonard Farneth Apr 26 '12 at 03:59
  • 1
    possible duplicate of [Stack Overflow / reddit voting system in php](http://stackoverflow.com/q/490969/), [Are there any Ajax(Prototype or JQuery Plugin) sample for stackoverflow-like voting?](http://stackoverflow.com/q/816808/90527) – outis Apr 26 '12 at 04:08
  • @LeonardFarneth: note you can [format lines as code](http://meta.stackexchange.com/questions/22186/) by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Jason McCreary did it for you this time, but next time try it out yourself. You can always look at the edit history to see what changes others have made. Click the orange question mark in the editor toolbar for more information and tips on formatting. – outis Apr 26 '12 at 04:13

1 Answers1

1

I would do this with jQuery. You definitely don't have to use jQuery, but it wraps up AJAX calls nicely, and makes them work in a standard way across several platforms.

First, you need to get jQuery loaded. You can load this from a CDN:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

Next, in your JavaScript, just make a .post() call to your existing PHP script:

$.post('yourscript.php', 
    {vid_or_whatever: 12345},
    function (data, textStatus, jqXHR) {
        /* Handle the response data here. */
    }
);

You can find more details on this method here: http://api.jquery.com/jQuery.post/

Finally, make sure you're using prepared queries with PDO, server-side. That way, you can avoid SQL injection vulnerabilities.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Thank you brad! wow 1.7.2 How time flies! I take back all that WT_ I was thinking! I will more than likely deploy this very solution. – Leonard Farneth Apr 26 '12 at 04:05
  • Ha Ha the ultimate revenge for you!!! Something that will take me at least 3 hours to figure out at 1 am! Thank you and I will get this. Hopefully before the birds start chirping. But this is what I was looking for thanks again. – Leonard Farneth Apr 26 '12 at 04:32
  • Ok one mild headache later What does 12345 represent? So this is azax??? every time I hit the return key on this forum it freaking posts!!!!!!! – Leonard Farneth Apr 26 '12 at 04:53
  • Yeah can I get rid of that ajax outo post whevever I hit enter? – Leonard Farneth Apr 26 '12 at 04:56
  • This is exactly why I hate ajax. It freezes older screens. takes up a truckload of bandwith and in the end accomplishes little more than a good php soulution – Leonard Farneth Apr 26 '12 at 05:01
  • RTFM. (Does anything else need to be said?) – Blake Apr 26 '12 at 05:34
  • Ok no help here outside of javascrpt.... No php answers? Java crashes...................... – Leonard Farneth Apr 26 '12 at 06:03
  • BTW I am not working on my second day coding so I get it. Speak English though If it's not too much trouble. – Leonard Farneth Apr 26 '12 at 06:11
  • You tell me RTFM like nobody ever has. But really telling me RTFM (for you new guys means read the F***ing manual) means learning ajax... Are you phucking serious??? really? Yes go study the php.net manual before posting a question here? have you never considered a forum s good way to get an answer to a perplexing problem? – Leonard Farneth Apr 26 '12 at 06:47
  • Stack Overflow has always seemed to come up lame for real solutions. I don't know s*** answers so I forget why I even asked. You too have no clue... But you like to babble like you do. I realy will seek help elsewhere (where ppl help). Webmaster please block my IP so I dont ever mistake your site for a help site again OK? – Leonard Farneth Apr 26 '12 at 07:01
  • 1
    Holy cow, I call it a night, wake up, and you've posted 15 comments, none of which tell me what problem you are having. If it isn't clear, `{vid_or_whatever: 12345}` is the object that contains the parameters you're sending to PHP. I don't know what those are, because you didn't post the relevant parts of your PHP code from before. It's up to you to decide what to call these variables, and decide what values to pass to them. If you had taken Blake's (not mine) "RTFM" post to heart, you would have clicked my link to http://api.jquery.com/jQuery.post/ that answers this for you. – Brad Apr 26 '12 at 14:24
  • Good morning Brad. Sorry I vented. It was actually 13 but who is counting. OK why can't I line break in this forum without posting? That is really annoying. – Leonard Farneth Apr 26 '12 at 16:45
  • 2
    @LeonardFarneth, It's not a forum, it's a QA board. In any case, read the FAQ (http://stackoverflow.com/faq) and take your questions/comments regarding the actual function of this site to the Meta site (http://meta.stackoverflow.com/). Comments/posts here on regular StackOverflow should be related to programming problems you have. – Brad Apr 26 '12 at 16:49
  • There is one variable // Process the form if it is submitted if ($_POST['vote']) { $cast_vote = $_POST['vote']; $sql = mysql_query("UPDATE videos SET vid_votes=vid_votes+1 WHERE vid_id=$vid_id"); } $votebutton ="
    "; And It is $vid_id. I just want the query to execute without restarting the video.
    – Leonard Farneth Apr 26 '12 at 17:05
  • @LeonardFarneth, please calm down. Blake's comment was a bit rude, but I see no trolling here. If you want to post new code, edit your original question. – senderle Apr 27 '12 at 01:45