2

I am working on a simple php html code that get products information from the database, each product has a submit button and its value must be the id of the product like this:

<input  type="submit" name="productId" value="' .$row['produc_id'] . '"  />

and in the next page I can know the product id that the user choose using this:

$productId = $_POST['productId'];

the code above will works fine, but the problem is that I can't put any text in the submit buttons, text like buy or add to cart and all the submit buttons has a numbers value which is the id of the products.

I tried this code but it didn't work:

<input  type="submit"  value=" buy "  />

<input  type="hidden" name="productId" value="' .$row['produc_id'] . '"  />
user2521365
  • 131
  • 3
  • 17
  • 1
    The core of this question was asked a different way. Take a look at this question, and it's answer: http://stackoverflow.com/questions/4171664/html-submit-button-different-value-button-text – TecBrat Jul 25 '14 at 22:13
  • @Thomas Andrews it gives me the wrong id – user2521365 Jul 25 '14 at 22:19

2 Answers2

4

You could probably try something like this:

<form action="someaction.php" method="post">
    <button type="submit" value="12" name="productId">Submit ME</button>
    <button type="submit" value="13" name="productId">Submit Too</button>
</form>
MikeWu
  • 3,042
  • 2
  • 19
  • 27
0

Make sure you have enclosed your inputs within a form-tag.

<form method="POST" action="index.php">
    <input type="hidden" name="form-product" value="PRODUCT_ID" />
    <input type="submit" name="form-submit" value="SEND" />
</form>

Then you can use isset() to check whether the form has been submitted, and if so, use the information in the submitted, hidden input field.

<?PHP 
    if (isset($_POST['form-submit']))
        echo "Product ID: ".$_POST['form-product']; 
?>

FYI: I noticed that you have typed: $row['produc_id'], is it maybe a typo there and it should be: $row['product_id'] (missing a t in producT).

Erlesand
  • 1,525
  • 12
  • 16