Simple PHP: getting variable from a form input

后端 未结 3 1350
我在风中等你
我在风中等你 2021-01-16 10:18

I can\'t really use PHP and from what I\'ve seen in tutorials this should work, but it doesn\'t:


  
  
             


        
相关标签:
3条回答
  • 2021-01-16 10:44

    PHP is a server-side language, so you'll need to submit the form in order to access its variables.

    Since you didn't specify a method, GET is assumed, so you'll need the $_GET super-global:

    echo $_GET['name'];
    

    It's probably better though, to use $_POST (since it'll avoid the values being passed in the URL directly. As such, you can add method attribute to your <form> element as follows:

    <form method="post">
        <input type='text' name="name" value="myName" />
        <input type="submit" name="go" value="Submit" />
    </form>
    

    Notice I also added a submit button for good measure, but this isn't required. Simply hitting return inside the textbox would submit the form.

    0 讨论(0)
  • 2021-01-16 11:01

    try this

    <?php
        if(isset($_REQUEST['name'])){
                    $name = $_REQUEST['name'];
                    echo $name;
        }
         ?>
    
    0 讨论(0)
  • 2021-01-16 11:03

    Well, you have to POST your form to get the value of $_POST variables. Add this to your <form>

    <form action="yourpagename.php" method="post">
        <input type='text' name="name" value='myName'>
        <button type="submit">Submit</button>
    </form>
    

    Click button and whatever is typed in your field will show up.

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