Submit form to action php file

后端 未结 3 1181
长情又很酷
长情又很酷 2021-01-15 12:09

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.

相关标签:
3条回答
  • 2021-01-15 12:30

    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.

    0 讨论(0)
  • 2021-01-15 12:44

    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')
    {
    
    }
    
    0 讨论(0)
  • 2021-01-15 12:50

    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.

    0 讨论(0)
提交回复
热议问题