Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form POST method not working (despite URL params showing)

Tags:

html

post

forms

php

Got a form setup to submit some stuff.

There are three submit buttons, all with the same name (choice) and different IDs (1, 2 & 3).

Using POST method to submit the form to form.php

Form.php loads and I can see the form params in the URL.

However there is no POST data coming in.

Index.php:

<form action='form.php' action='POST'>

<input type='hidden' name='index' value='".$cell_count."'>

<div class='btn-group btn-group-m'>
  <button name='choice' value='1' type='submit' class='btn btn-default btn-danger'>
    ...
  </button>

  <button name='choice' value='2' type='submit' class='btn btn-default btn-warning'>
    ...
  </button>

  <button name='choice' value='3' type='submit' class='btn btn-default btn-success'>
    ...
  </button>
</div>

<button name='choice' type='submit' value='4' class='btn btn-default btn-sm'>
  ...
</button>

Form.php :

<?php

  var_dump($_POST);

  if($_POST['choice'] == 3) {
    echo "Chose 3";
  }
  else if($_POST['choice'] == 2) {
    echo "Chose 2";
  }
  else if($_POST['choice'] == 1) {
    echo "Chose 1";
  }

 echo "index: " . $_POST['index'];

?>

Result :

https://i.sstatic.net/JrB7j.png Thanks for any help you can offer!

like image 988
Tom Blacknell Avatar asked Mar 20 '26 09:03

Tom Blacknell


1 Answers

You are mixing the attributes:

<form action='form.php' action='POST'>

Should be:

<form action='form.php' method='POST'>

Now you don't have a method attribute, so the form is sent by GET (the default).

like image 125
jeroen Avatar answered Mar 22 '26 22:03

jeroen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!