How can I get a PHP value from an HTML form?

后端 未结 3 1333
失恋的感觉
失恋的感觉 2021-01-16 17:26

I have an HTML form as follows:

<
相关标签:
3条回答
  • 2021-01-16 17:56

    Some basic reading on handling forms in PHP.

    0 讨论(0)
  • 2021-01-16 18:04

    First check if the form has been submitted:

    <form enctype="multipart/form-data" action=" " method="post">
    <input name="username" type="text"/>
    <input type="submit" name="Submit" value="Upload" class="btn btn-primary"/><br/>
    </form>
    
    if($_POST['Submit'] == "Upload")
    {
    
        $username = $_POST['username'];
    
    }
    
    0 讨论(0)
  • 2021-01-16 18:10

    When the form submits, you need to get the values from the $_POST array

    You can do print_r($_POST) to see everything it contains (all of the form fields) and reference them individually as well.

    Username will be $_POST['username']

    I recommend reading a tutorial on working with forms and PHP... here's a good one

    Since you're obviously a beginner I'll help you out a bit more:

    Give your submit button a name:

    <form enctype="multipart/form-data" action="" method="post">
        <input name="username" type="text"/>
        <input type="submit" name="submit" value="Upload" class="btn btn-primary"/><br/>
    </form>
    

    Because action is blank, it will POST to the current page. At the top of your file, you can check to see if the form was submitted by checking if $_POST['submit'] is set (I gave your submit button that name).

    if(isset($_POST['submit'])) {
        // form submitted, now we can look at the data that came through
        // the value inside the brackets comes from the name attribute of the input field. (just like submit above)
        $username = $_POST['username'];
    
        // Now you can do whatever with this variable.
    }
    // close the PHP tag and your HTML will be below
    
    0 讨论(0)
提交回复
热议问题