Jacklck
Jacklck

Reputation: 53

HTML button with POST

Basically I need a to be able to submit the form via POST.

here is my current form.

<form action="process.php" method="post">
<button type="submit" class="btn btn-success" role="link" name="item" value="1">Item 1</button>
<button type="submit" class="btn btn-success" role="link" name="item" value="2">Item 2</button>
<button type="submit" class="btn btn-success" role="link" name="item" value="3">Item 3</button>
</form>

I just need it to work like if I click item 1, it will POST a value of 1 to the process.php. and if I click item 2, it will POST 2. The problem is, no matter which button I press, the value will be "1". If I change it to GET, there are no problems.

I've also tried this but it doesn't seem to work

<form action="process.php" method="get">
  <button type="submit" name="item" value="3" formmethod="post" formaction="process.php">Item 3</button>
</form> 

Any ideas?

Upvotes: 3

Views: 7801

Answers (3)

AndrewTsang
AndrewTsang

Reputation: 316

An upgrade from m1xolyd1an's answer:

put this on your process page

<?php
    if(isset($_POST["product1"])) {
    $product = 1;
    }
        if(isset($_POST["product2"])) {
    $product = 2;
    }
        if(isset($_POST["product3"])) {
    $product = 3;
    }
?>

And put this on your form

<form action="process.php" method="post">
<input type="submit" class="btn btn-success" name="product1" value="Order Now">
<input type="submit" class="btn btn-success" name="product2" value="Order Now">
<input type="submit" class="btn btn-success" name="product3" value="Order Now">
</form>

then use $product to get your value.

Upvotes: 2

Sweaver
Sweaver

Reputation: 63

In html you can pass in the array to PHP. Since you are using the same name for each button type, you can just do something like,

<form action="process.php" method="post">
<button type="submit" class="btn btn-success" role="link" name="item[]" value="1">Item 1</button>
<button type="submit" class="btn btn-success" role="link" name="item[]" value="2">Item 2</button>
<button type="submit" class="btn btn-success" role="link" name="item[]" value="3">Item 3</button>
</form>

and then $_POST will then contain an array for item with all values from the input elements and you can loop through each or do whatever your logic is that you want.

Upvotes: 0

m1xolyd1an
m1xolyd1an

Reputation: 535

Personally I would handle this a bit differently. For your form I would use input type submit instead of button and then give each one a name field that will be logged to $_POST.

<?php
if(isset($_POST['buttonName1'])){
$_POST['someVariable'] = 1;
}
if(isset($_POST['buttonName2'])){
$_POST['someVariable'] = 2;
}
if(isset($_POST['buttonName3'])){
$_POST['someVariable'] = 3;
}
?>
<form action="process.php" method="post">
<input type="submit" name="buttonName1" class="btn btn-success" value="1">
<input type="submit" name="buttonName2" class="btn btn-success" value="2">
<input type="submit" name="buttonName3" class="btn btn-success" value="3">
</form>

Then in your process.php file you can call your variable to find out which button the user clicked on the first page.

$callingVariable = $_POST['someVariable'];

Upvotes: 1

Related Questions