douyong7199
douyong7199
采纳率0%
2017-01-31 23:57 阅读 97
已采纳

如何使用get方法使用PHP输入表单字段的值

I have the following code snippet of my fields I have in my form:

       <input id="username" type="text" placeholder="E-mail Address" value="" name="username"></input>

This is what I have in my input field. Is there anybody who will tell me how to get input values to the field using a url? e.g https://mysite?username=ken and it will show "ken" in the input field?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    doukengzi3517 doukengzi3517 2017-02-01 00:00

    In your HTML, add the input field like this:

    <input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />
    

    Basically, the value attribute of the text field needs to be set to:

    <?php echo $_GET['username']; ?>
    

    The code right above this is how you would output a get variable in php whether you are putting it in a text field or not. To access get variables, always use:

    $_GET['variable_name'];
    

    Then you can assign it to variables or pass it as a function parameter.

    **However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **

    First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.

    点赞 评论 复制链接分享
  • doubi9255 doubi9255 2017-02-01 00:09

    You can do it like this, make an isset in your php form input that can catch your ken variable from GET post, never forget the method="get" inside the form tag and if you are planning on submitting on the same page you can use action="<?php echo $_SERVER['PHP_SELF']; ?>" inside your form tag.. hope this helps, here is your code.. ^_^

        <form id="form" name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    
    
                                <fieldset>
    
    
                                    <p>Input</p>
                                    <div>
                                        <input type="text" name="nameoffield" id="nameoffield" value="<?php if(isset($_GET['ken'])){echo $_GET['ken'];} ?>"> <br />
    
    
                                    </div>
                                </fieldset>
                                <div>
                                    <button type="submit" value="Submit" name="submit">
                                        Submit
                                    </button>
                                </div>
                                </form>
    
    点赞 评论 复制链接分享
  • douluyezhen4512 douluyezhen4512 2017-02-01 00:21

    The <input> tag and other fields of form must be in a <form>tag.

    <form action = "https://mysite" method = "get">
    <input id = "username" type = "text" placeholder = "E-mail Address" name = "username" value = "<?php echo $_GET['username']; ?>" />
    </form>
    

    In the above code, form tag specifies that the method of submission is 'GET' and the action that will be taken on submission is URL to which your form data will be submitted and processed.

    Now assuming that your form is in the same URL to which you are submitting your form, you will get the GET value in the same page (or URL), so in the input text field set the value which is obtained by GET method and use it.

    All the GET key-value pairs are stored in an associative array $_GET from which you can access the value of a given key by using that as the index of the array.

    e.g. Key is username in this case, so to get the value of the username, $_GET['username'] was used.

    点赞 评论 复制链接分享

相关推荐