Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form to action php file

Tags:

html

forms

php

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.

like image 708
unknownSPY Avatar asked Apr 16 '26 08:04

unknownSPY


1 Answers

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.

like image 53
Funk Forty Niner Avatar answered Apr 18 '26 20:04

Funk Forty Niner