General Advice on Voting System

Discussion in 'PHP' started by VicarInATutu, Jan 20, 2014.

  1. #1
    Hi, I'm looking for some general advice on how to improve my current voting system.

    When a person votes on my site, a unique ID number such as 1234, is sent from one PHP page to another.

    The second page takes this ID number, does some checks and then updates the MySQL database with a positive (or negative) vote. The user is redirected back to the original page which is refreshed, updating the results of the vote cast.

    It's pretty straightforward, but it's clumsy because the site visitor gets directed to and from another PHP page. It feels clunky.

    I would like this vote to be processed in the background, out of site from the site visitor, but I don't know how to implement this.

    I'm looking for a discussion on this so I can progress with something more attractive on my site, other than sending information between two PHP pages.

    All The Best,
    Richard.
     
    VicarInATutu, Jan 20, 2014 IP
  2. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    Look up some articles on AJAX. That should be able to handle what you're doing. If you're a little more specific on what you want it to do I could show you an example.
     
    Pudge1, Jan 20, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Using jQuery:
    
    $("#submit_vote").click(function() {
      var vote = $('container_for_vote').val();
      $.post('update_database.php', {castvote:vote}, function(data) { //do something with data
      });
    }
    
    PHP:
    And the update_database.php would be something like:
    
    $vote = $_POST['castvote'];
    // do something with $vote
    // if needed, return some value to the jQuery function to show result
    
    PHP:
    That's VERY basic, but would work without redirections or pageloads
     
    PoPSiCLe, Jan 20, 2014 IP
  4. VicarInATutu

    VicarInATutu Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Thanks for your answers, so far. I've got a basic notion of jquery, but I don't know how to change the way I'm currently doing things in html and php.

    Here's some simplified PHP code from the page that casts the vote:

    <?php
    echo "<a href='http://www.mysite.com/votepage.php?ID={$row['ID']}'>";
    ?>

    So in this instance, 'votepage.php' receives the 'ID' value as a number and then processes it, basically like this:

    <?php

    $ID = $_GET['ID'];

    require ('db_connect.php');

    $VAR = mysql_query("Select * FROM table WHERE value = '$ID' ");

    while ($row = mysql_fetch_array($VAR, MYSQL_ASSOC))
    {
    ... and then I start adding info to the MySQL DB based on the unique ID number received...
    }

    ?>

    This is very simplified code, but it works at present.

    How do I get my voting page to send the ID value to a background script that the site visitor can't see, that is then able to send information to the MySQL database and then redirect to the updated original voting page?

    Any ideas...
     
    VicarInATutu, Jan 20, 2014 IP
  5. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #5
    changing it to jquery is as easy as adding an id tag to your link, the process is not any different, aside from the post variables getting passed through ajax instead of the traditional way

    $("#submit_vote").click(function() {
      var vote = $('container_for_vote').val();
      $.post('update_database.php', {castvote:vote}, function(data) { //do something with data
      });
    }
    Code (markup):
    code above could work with a few modifications
     
    creativeGenius, Jan 20, 2014 IP
  6. VicarInATutu

    VicarInATutu Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    So what you're saying is that I change nothing in terms of my existing PHP programming, but use a javascript wrapper (for want of a better word) to transfer the contents of the html link, through Ajax/javascript in the background, to another PHP file which processes the data?

    Sorry if I'm being a bit thick.

    In the <head> of the page there would be a link to the .js file as follows: <script src="filename.js"></script>, for example.
    In the <body> there would be an id tag such as id="filename", or simply an external link such as <filename> surrounding the <a href> link.

    So, instead of a person clicking on an A HREF link which takes them to a second PHP file which processes the vote.
    The HREF takes them to a jquery.js file which then points the user to the second PHP file, but in the background, out of site from the site user.
    The same thing is happening as before, but with the addition of extra javascript/Ajax code?

    If I'm not getting the concept here, forgive me.
    My existing PHP is quite complex and I don't know how to rewrite it all in javascript.
     
    VicarInATutu, Jan 21, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    No, not quite.
    You have the code exactly as you have today, actually. The processing-php-file can be the same as it is today, it just gets its info from the jQuery/javascript function, instead of the actual url (so you change the $_GET to $_POST in that file).
    The link can just be a regular link - you can prevent the href-attribute from firing in the jQuery/javascript function, hence stopping it from actually going to said file.
    There will be NO reloading of anything - everything will be done using the javascript, which of course can be included as you state above.
    Here's an attempt at getting you to understand the logic:
    Voting page -> user clicks a link to vote yes or no -> that link triggers a jQuery function which prevents the actual href from redirecting the user to a secondary page and also sends the content of that link to the processing-php-file in the background -> processing-php-file receives a call from the jQuery-function, and processes the input, and based on that, sends back some sort of confirmation - either success, error, or whatever else you want -> back on the original page, the jQuery-function receives the message from the processing-php-file, and if you want, shows this to the user, either using an alert(), or some other way of communicating this.

    This is fairly basic web-programming.
     
    PoPSiCLe, Jan 21, 2014 IP
  8. VicarInATutu

    VicarInATutu Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Thanks for your help on this.
    I've still got a lot of stuff to get my head around, but you've shown me how the concept works, which is what I was having real problems with.
    I'll start to fiddle around and then bother everyone again, no doubt, when I come across technical issues.
    You've been great - thanks once again.
     
    VicarInATutu, Jan 22, 2014 IP
  9. VicarInATutu

    VicarInATutu Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    Thanks again for your help with this. I'm going to post a new thread with my findings, as I'm still getting to grips with it. All The Best.
     
    VicarInATutu, Jan 28, 2014 IP