Jump to content

Redirect PHP Form after a button pressed

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Hey I'm looking for a solution to my problem, really I don't have any ideas how to fix my problem. :crying::crying::crying:

I've a poll form:
<?php
  echo "ID: ",$id_vote, "<p>";
  echo "Please press YES if you are agree with the entry or press NO if you are not agree and then fill the form.<br>";
?> 

<form name="vote_form" method="post" action="<?php echo $action_url ?>">
  <input type="submit" name="vote_form_yes" id="vote_form_yes" value="Yes">            
  <input type="submit" name="vote_form_no" id="vote_form_no" value="No">
</form>

<?php
if (isset($_POST['vote_form_yes']) && $_POST['vote_form_yes']==TRUE) {
    $sql_run_vote_yes = "UPDATE MyTable SET id_votes_yes = id_votes_yes+1  WHERE id='$id_vote' ";
    .......................................
    .......................................
} else if (isset($_POST['vote_form_no']) && $_POST['vote_form_no']==TRUE) {
    $sql_run_vote_no = "UPDATE MyTable SET id_votes_no = id_votes_no+1  WHERE id='$id_vote' ";
    .......................................
    .......................................
}
?>

Each button execute a mySQL query as you can see.
Unfortunatelly if I press "Yes" or "Not" I won't be redirected to another webpage but I still stay in that page with the chance to press "Yes" or "No" again and again and this is not good.

Do you have any hint how to block the "Yes" or "No" button after the first click?

I don't have any idea, so I thought to create 2 separated files, one with the form and one with the check and the query execution, like below:
File_1.php
<?php
  echo "ID: ",$id_vote, "<p>";
  echo "Please press YES if you are agree with the entry or press NO if you are not agree and then fill the form.<br>";
?> 

<form name="vote_form" method="post" action="<?php echo $action_url ?>">
  <input type="submit" name="vote_form_yes" id="vote_form_yes" value="Yes">            
  <input type="submit" name="vote_form_no" id="vote_form_no" value="No">
</form>

File_2.php
<?php
if (isset($_POST['vote_form_yes']) && $_POST['vote_form_yes']==TRUE) {
    $sql_run_vote_yes = "UPDATE MyTable SET id_votes_yes = id_votes_yes+1  WHERE id='$id_vote' ";
    .......................................
    .......................................
} else if (isset($_POST['vote_form_no']) && $_POST['vote_form_no']==TRUE) {
    $sql_run_vote_no = "UPDATE MyTable SET id_votes_no = id_votes_no+1  WHERE id='$id_vote' ";
    .......................................
    .......................................
}
?>

Unfortunately if I do this way the "isset($_POST['vote_form_yes'])" and "isset($_POST['vote_form_no'])" are not verified and it doesn't work.

Any help please??
Thank you in advance

P.S.: already tried onclick, or the link in the action="", but it doesn't execute the queries.

#2
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Sorry for the bump guys, but really no help on this one? :(

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Sessions or Cookies.

#4
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
My two suggestions:
1. Do a redirect in file2.php
<?php
if (isset($_POST['vote_form_yes']) && $_POST['vote_form_yes']==TRUE) {
    $sql_run_vote_yes = "UPDATE MyTable SET id_votes_yes = id_votes_yes+1  WHERE id='$id_vote' ";
    .......................................
    .......................................
} else if (isset($_POST['vote_form_no']) && $_POST['vote_form_no']==TRUE) {
    $sql_run_vote_no = "UPDATE MyTable SET id_votes_no = id_votes_no+1  WHERE id='$id_vote' ";
    .......................................
    .......................................
}
// Redirect to index
header('Location: /');
?> 

2. Set a cookie or a value in their session to indicate the user voted on the poll. Then check for this before db submission.

of course, using both wouldn't hurt.
"Life would be so much easier if we only had the source code."

#5
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Hey thank you for the answers.
I fixed with some hard code, adding some checks and now it works great.

Sorry if I didn't answer, but I was very busy.

Thank you again guys