1

I have a form with Two buttons. It's a submit form which is basically there to increase/Decrease an Integer value in a MySQL database depending on which button they click.

This is a weird scenario, the page requires an ID extension;

appinfo.php?id=12

Whenever I click one of the buttons it changes the extension to:

appinfo.php?VoteYes=Yes

Ideally I'd like it so if someone clicks on Yes it will increase a value within an SQL query and if they click no, it will decrease it.

Here is what I have so far:

<form action="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id']; ?>"/>
    <input id="submit" name="VoteYes" type="submit" value="Yes" class="btn btn-success"></input>
    <input id="submit" name="VoteNo" type="submit" value="No" class="btn btn-warning"></input>
</form>
1
  • A couple issues I see. You have no form field named id. (You're also not validating that field.) What you're seeing happen is the name of the button you click on is getting added to the querystring. The approach I'd recommend is doing an ajax call on button click to a script that handles the logic of inserting data into the db. Then on the success callback you can update the form action or whatever you need to do. Commented Mar 4, 2015 at 15:53

2 Answers 2

1

In your <form> add the attribute method="post". The default is GET which is changing your URL.

So, it should be:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id']; ?>"/>
    <input id="submit" name="VoteYes" type="submit" value="Yes" class="btn btn-success"></input>
    <input id="submit" name="VoteNo" type="submit" value="No" class="btn btn-warning"></input>
</form>

Then in your PHP code, check for $_POST instead of $_GET when checking for the submitted values.

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Hope i am not wrong ask this question.In your solution how 'appinfo.php?id=' changes to 'appinfo.php?VoteYes=' ? becasue the above code adds id="passed argument" it have to be VoteYes="passed argument" in some time right?
@shridhar When submitting a form with the method as get, the browser ignores any parameters that may be already present in the action attribute and simply only includes the form data in the URL.
0

If you really want the query string You can do this with hidden input that will include the id, I also modified the names to make it less complicated

<form>
<input id="submit" name="Vote" type="submit" value="Yes" class="btn btn-success" />
<input id="submit" name="Vote" type="submit" value="No" class="btn btn-warning" />
<input  name="id" type="hidden" value="<?php echo $_GET['id']?>" /></form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.