3

I have a form where when the user clicks submit, I need a php file to be ran. below is the form and the php file.

<form action="php_scripts/test.php" method="POST">
        <input name="feature"     type = "text"     placeholder="Feature"   /> 
        <input name="feature2"   type = "text"  placeholder="Feature2"  /> 
        <input type="submit" value = "submit"/>
</form>

test.php

<?php

    if( isset($_GET['submit']) )
    {
        $feature = $_POST['feature'];
        // do stuff (will send data to database)
    }
?>

The problem I am having is that when I press Submit on the form,

if( isset($_GET['submit']) )

Always returns false.

Can anyone explain why that is? Have I totally misunderstood how to implement form sending data to php scripts?

Apologies if I have made any syntax errors and many thanks for any help you can give.

unknownSPY
  • 706
  • 4
  • 15
  • 27
  • 3
    Your form elements are not named. Plus, you're mixing GET with POST. – Funk Forty Niner Nov 18 '14 at 15:19
  • @JonathanM Well technically only when `method="post"` is defined, it would typically happen through POST. I believe the [fallback is `GET` if the user doesn't define `method`](http://stackoverflow.com/questions/2314401/what-is-the-default-form-posting-method). But yes, best practice is to use the POST method and OP should be using `$_POST` for their entire form handling code. – sjagr Nov 18 '14 at 15:22
  • `// do stuff (will send data to database)` is that part of a hidden underlying question and *after the fact?* – Funk Forty Niner Nov 18 '14 at 15:22
  • updated value for name (or do I need a value and a name). also when chaging the php file to $_POST['submit'] it still always returns false – unknownSPY Nov 18 '14 at 15:23

3 Answers3

6

There are a few things wrong with your code.

You're mixing GET with POST methods. Plus, add values to your inputs and your submit button isn't named, which you're trying to use as a conditional statement for.

HTML

<form action="php_scripts/test.php" method="POST">
        <input name="feature"  value="feature" type = "text" placeholder="Feature" /> 
        <input name="feature2" value="feature2" type = "text"  placeholder="Feature2"  /> 
        <input type="submit" name="submit" value = "submit"/>
</form>

PHP

<?php

    if( isset($_POST['submit']) )
    {
        $feature = $_POST['feature'];
        $feature2 = $_POST['feature2'];
        // do stuff (will send data to database)
    }
?>

Sidenote: You could/should also check against empty values.

if(isset($_POST['submit']) 
    && !empty($_POST['feature']) 
    && !empty($_POST['feature2']) ) {...}

Footnotes:

Seeing that you're intending on sending to DB:

I hope you plan on using mysqli with prepared statements, or PDO with prepared statements.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

A couple of things:

  • you're using $_GET instead of $_POST
  • isset($_POST['submit']) is not a good check, not every browser will send the submit button in its request. (Apart from the fact that you haven't even named the submit button, so it wouldn't come through in any browser, as it stands now.)
  • it's better to use:

Code:

if($_SERVER['REQUEST_METHOD'] == 'POST')
{

}
Sherlock
  • 7,525
  • 6
  • 38
  • 79
0

You missed to name the submit button. So no entry in the $_POST/$_REQUEST array is given. Depending on the php settings you might want to use array_key_exists() to check for an index in the array as isset might throws an error.

Joshua Behrens
  • 818
  • 13
  • 23