使用 PHP 变量作为表单域的默认值 [英] Using a PHP variable as a form fields default value

查看:50
本文介绍了使用 PHP 变量作为表单域的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 HTML 表单,用户可以在其中注册并将数据输入到 SQL 数据库中.然后我在他们可以查看他们的个人资料的网页中检索了该数据.我创建了一个页面,用户可以通过创建一个表单来编辑那里的配置文件,该表单更新 SQL 数据库中用户 ID 的值.

I have a created an HTML form where users sign up and input there data into an SQL database. I have then retrieved that data in a webpage where they can view their profile. I have created a page where users can edit there profile by creating a form which updates the value in the SQL database for there user id.

我希望表单使用 SQL 单元格的当前值作为该用户的默认值,以便更轻松地进行更改.示例:当前用户 7 的城市设置为纽约,当他们访问编辑信息页面时,表单中的城市字段已经将纽约作为默认值.

I would like the form to use the current value of the SQL cell as the default for that user to make alterations easier. Example: currently user 7 has their city set as New York, when they visits the edit info page, the city field in the form already hase New York as the default value.

获取 SQL 信息并将其分配给变量没有问题,我只是不明白如何将其设置为默认值.不过,我知道您如何为输入字段设置默认值.

I have no problem getting the SQL info and assigning it to a variable, I just don't understand how to set it as the default value. I am aware of how you set default values for input fields though.

我的代码:

<?php
$id = $_SESSION["user_id"];    

// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";

// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);

// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];   
echo mysqli_error();    
}
}
?>  
<input type="text" name="aboutme" defualt="<?php echo $row['aboutme'] ?>" >  

推荐答案

html 输入没有 default 值.输入可以有值,使用属性 value:

There's no default value for html input. Input can has value, using attribute value:

<input type="text" name="some_name" value="Some value" /> 

在你的情况下是

<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> /> 

输入也可以有 placeholder - 一些存在于输入中的值,但当用户开始编辑输入的内容时被删除:

Input can also has placeholder - some value that is present in an input, but erased when user starts to edit input's content:

<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> placeholder="some value" /> 

这篇关于使用 PHP 变量作为表单域的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆