如何让用户在字段中输入文本,将文本发送到数据库,然后显示在页面上?
How to have user input text to field, have text sent to database, and then displayed on a page?
我正在为一个小型个人项目创建一个网站。基本上我需要一个文本字段,用户可以在其中输入报价,一个将其发送到数据库的提交按钮,然后数据库中的所有报价都显示在其下方。我不知道该怎么做。
我曾尝试使用 Javascript 在提交后显示报价,但是在重新加载页面时一切都消失了,并且没有保存到数据库中。
您将需要使用服务器端代码来实现此目的
Javascript 是客户端,因此无法将数据插入数据库
这些链接应该可以帮助您实现所需
https://www.w3schools.com/php/php_forms.asp
您可以通过多种方式完成:
标准html形式
- 将操作表单设置为 php 文件。如果您单击提交,那么页面将被刷新。
- 在 php 脚本中,您应该验证数据是否正确并将数据保存到数据库中
或文件
- 将用户重定向回带有表单的页面。您应该获取并显示以下形式
来自数据库的行。
使用 AJAX - 不刷新页面
- 点击发送按钮后,您可以验证Javascript中的数据并发送
他们(通过 AJAX)到后端(php 脚本)
在后端验证数据是否正常并将它们保存到db/file。后端应该输出 JSON 结果。消息信息和状态。
在 Javascript 中,您应该解析 JSON 结果并执行一些操作,例如向用户显示消息。
接下来,您可以从数据库
[=34= 中的 table 获取(通过 ajax)所有(或例如最后 10 行)的列表]
尝试以下方法
first Set action form to php file (index.php).
<form method="Post" action="form.php">
<fieldset class="form-group">
<label for="label">Username</label>
<input type="text" class="form-control" name="username" >
</fieldset>
<fieldset class="form-group">
<label for="f">Lastname</label>
<input type="text" class="form-control" name="lastname" >
</fieldset>
<button type="button" name="submit" class="btn btn-primary">Submit</button>
form.php this will be second page
<?php
$server="localhost";
$username ="root";
$password ="";
$database ="you_database";
$connection =mysqli_connect($server ,$username ,$password,$database); //start connection
if ($connection==TRUE) // test connection
{
echo "connected succefull";
}
if (isset($_POST['submit'])) //test submit name in form
{
// declare parameter to initialized in text fild in form
// and those fild should appear in serve (apache mysql)
$username =$_POST['username'];
$lastname =$_POST['lastname'];
$query ="INSERT INTO your_table VALUES(NULL, '$username' ,'$lastname' ");
$result =mysqli_query($connection ,$query);
if ($result == TRUE) {
echo "data insert succefull";
}else{
echo "failed to insert";
}
}
mysqli_error($connection);
?>
我正在为一个小型个人项目创建一个网站。基本上我需要一个文本字段,用户可以在其中输入报价,一个将其发送到数据库的提交按钮,然后数据库中的所有报价都显示在其下方。我不知道该怎么做。
我曾尝试使用 Javascript 在提交后显示报价,但是在重新加载页面时一切都消失了,并且没有保存到数据库中。
您将需要使用服务器端代码来实现此目的
Javascript 是客户端,因此无法将数据插入数据库
这些链接应该可以帮助您实现所需
https://www.w3schools.com/php/php_forms.asp
您可以通过多种方式完成:
标准html形式
- 将操作表单设置为 php 文件。如果您单击提交,那么页面将被刷新。
- 在 php 脚本中,您应该验证数据是否正确并将数据保存到数据库中 或文件
- 将用户重定向回带有表单的页面。您应该获取并显示以下形式 来自数据库的行。
使用 AJAX - 不刷新页面
- 点击发送按钮后,您可以验证Javascript中的数据并发送 他们(通过 AJAX)到后端(php 脚本)
在后端验证数据是否正常并将它们保存到db/file。后端应该输出 JSON 结果。消息信息和状态。
在 Javascript 中,您应该解析 JSON 结果并执行一些操作,例如向用户显示消息。
接下来,您可以从数据库
[=34= 中的 table 获取(通过 ajax)所有(或例如最后 10 行)的列表]
尝试以下方法
first Set action form to php file (index.php).
<form method="Post" action="form.php">
<fieldset class="form-group">
<label for="label">Username</label>
<input type="text" class="form-control" name="username" >
</fieldset>
<fieldset class="form-group">
<label for="f">Lastname</label>
<input type="text" class="form-control" name="lastname" >
</fieldset>
<button type="button" name="submit" class="btn btn-primary">Submit</button>
form.php this will be second page
<?php
$server="localhost";
$username ="root";
$password ="";
$database ="you_database";
$connection =mysqli_connect($server ,$username ,$password,$database); //start connection
if ($connection==TRUE) // test connection
{
echo "connected succefull";
}
if (isset($_POST['submit'])) //test submit name in form
{
// declare parameter to initialized in text fild in form
// and those fild should appear in serve (apache mysql)
$username =$_POST['username'];
$lastname =$_POST['lastname'];
$query ="INSERT INTO your_table VALUES(NULL, '$username' ,'$lastname' ");
$result =mysqli_query($connection ,$query);
if ($result == TRUE) {
echo "data insert succefull";
}else{
echo "failed to insert";
}
}
mysqli_error($connection);
?>