Question

I'm trying to make a simple vote script in PHP and need help. The problem I'm having is that I've created a form with two buttons, like and dislike, which correspond to an if else statement.

If the like button is clicked do an update, else the dislike button was clicked do an update.

Here is the code I have so far, there is more above it, but not necessary to my problem:

while($row = $q2->fetch()) 
        {
        $up_vote= $row['up_votes']; /*Current Up Votes*/
        $down_vote= $row['down_votes']; /* Current Down Votes*/
            echo '<form method="post" action="">';
            echo '<input type="button" id="up_vote" name="up_vote" value="Like" /> OR';
            echo '<input type="button" id="down_vote" name="down_vote" value="Dislike" />';
            echo '</form>';
            echo round($row['rating'], 0);
            echo '% of voters Liked this';
            }


        $up_votes = $up_vote +1;/*$up_vote referes to original # of up_votes, $up_votes adds 1 to the number of votes*/
        $down_votes = $down_vote +1;/*$down_vote referes to original # of down_votes, $up_votes adds 1 to the number of votes*/

    if ($something);/*upvote clicked*/
    {
        $vote_update = "UPDATE images
                        SET up_votes:up_votes
                        WHERE vote_item_id: vote_item_id";

        $q3 = $conn-> prepare($vote_update);
        $q3->execute(array(':up_votes'=>$up_votes, /*$up_vote referes to original # of up_votes, $up_votes adds 1 to the number of votes*/
                           ':vote_item_id'=>$current_vote_item_id));

        /* ALSO create INSERT INTO votes_done to track the user vote*/ 
    $conn = null;
    }       
    else ($something2);/*down vote clicked*/
    {
        $vote_update = "UPDATE images
                        SET down_votes:down_votes
                        WHERE vote_item_id: vote_item_id";

        $q3 = $conn-> prepare($vote_update);
        $q3->execute(array(':down_votes'=>$down_votes, /*$down_vote referes to original # of down_votes, $up_votes adds 1 to the number of votes*/
                           ':vote_item_id'=>$current_vote_item_id));
        /* ALSO create INSERT INTO votes_done to track the user vote*/
    }   

What should the if and else statements look like? I'm not sure how to go about this, can't have two submits, should I have two different forms, one for each button? Either way how can I say if like was clicked go to if and run code, or if dislike was clicked go to else and run code?

Thanks

Was it helpful?

Solution

Syntax errors aside, you can give the two buttons the same name, but different values.

echo '<form method="post" action="___URL OF YOUR PAGE___">';
echo '<input type="submit" id="up_vote" name="vote" value="Like" /> OR';
echo '<input type="submit" id="down_vote" name="vote" value="Dislike" />';
echo '</form>'
...
if($_POST['vote'] == "Like") {
...
} else if ($_POST['vote'] == "Dislike") {
...
}

Note that I changed the input's type to submit instead of button. This will cause the data to send itself to the page (button won't do anything unless you start adding JavaScript).

You can read more about this in these answers:

https://stackoverflow.com/a/6129301/1451957

html button v.s. html submit?

OTHER TIPS

PHP is server side, while you want to control events triggered in client side (a user clicks one button, or another).

Javascript/Jquery are client side, there you can control and trigger events that call to PHP scripts that will do the query.

Also else(){ is not valid in PHP, you should use elseif(){.

This sounds like an instance where using JavaScript would be more useful, but if you really want to use PHP you can either use two separate forms or change the buttons to links that look like buttons and add URL parameters.

Also: you've got some syntax errors. Examples: you shouldn't be putting semicolons after your if/elseif conditions, else() should be elseif(), you're using two different variable names when trying to increment $up_votes and $down_votes, etc.

Why dont't you add an 'onclick' event to each of the like and dislike buttons, and execute different script?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top