我想用php创建一个页面,并输入表单中的文本。
我知道的是:
createUserProfiles.php这样的模板将文本从表单/u/username.php发送到/u/username.php。边注:,我知道如何创建页面,但我不知道如何在其中编写php代码,也不知道如何使用模板创建用户配置文件。我试着在谷歌上搜索,但没有找到任何答案。
发布于 2015-07-05 09:32:18
createUserProfiles.php
<form method="post" action="u/username.php">
<input type="text" name="username">
<input type="text" name="email">
<input type="submit" name="submit" value="Create User">
</form>现在,我们将将数据提交到数据库,并通过在数据库中加载模板u/username.php将这些提交的值呈现给templates/profile-templates.php。
<?php
//connect to db
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
//feed the values from previous form to db
$stmt = $db->prepare("INSERT INTO table(username,email) VALUES(:uname,:email)");
$stmt->execute(array(':uname' => $_POST['username'], ':email' => $_POST['email']));
//now make an array with key name and its values
$attribute = array ('{user_name}'=>$_POST['username'], '{email}'=>$_POST['email']);
//load the template into a variable called $html
$html = file_get_contents ('templates/profile-templates.php');
//Now replace {user_name} and {email} present in template file with real data fetched from user.
foreach($attribute as $key => $value){
$html = str_replace ("{$key}", $value, $html);}
//At last echo the variable
echo $html;只需确保模板文件中有花括号,其中数据将通过PHP放置。参考文献
发布于 2015-07-05 09:15:38
您的方法很难使用,您应该使用sql服务器,因为数据类型和存储功能都很容易使用。遵循howCodes关于创建社交网络的教程,它将为您提供html表单和php的基本概念。
https://stackoverflow.com/questions/31228511
复制相似问题