douyong7199 2017-01-31 23:57 采纳率: 0%
浏览 107
已采纳

如何使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波