简体   繁体   English

通过我的 html 页面提交后如何保存 php 表单?

[英]How do I save a php form after submitting through my html page?

I have made an html page with forms and tables where I input words and numbers and have submit button.我制作了一个带有表格和表格的 html 页面,我在其中输入了单词和数字并有提交按钮。 Once I click the submit button it outputs the right information that I inputed into a php file BUT it does not save, have do I save it is my question?单击提交按钮后,它会输出我输入到 php 文件中的正确信息,但它没有保存,我是否要保存它是我的问题? Here is part of my html code where I input the information:这是我输入信息的 html 代码的一部分:

<body>
<form action="physical_server_check_list_saved.php" method="post">
<table width="800" align="left">
<th width="800" align="left" colspan="2" bgcolor="#D3D3D3">SERVER INFORMATION</th>
<tr>
<td width="200" align="left">Server name:</td>
<td width="600" align="left"><input type="text" size="100" name="servername"></td
/tr>
tr>
<td width="200" align="left">Contact:</td>
<td width="600" align="left"><input type="text" size="100" name="contact"></td
/tr>
<tr>
<td width="800" align="left" colspan="2">
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>

Here is part of my php code, it is the same format except php code added to the table cells displaying the output:这是我的 php 代码的一部分,除了添加到显示输出的表格单元格中的 php 代码外,它的格式相同:

<tr>
<td width="200" align="left">Server name:</td>
<td width="600" align="left">
<?php
$servername = $_POST['servername'];
echo $servername;
?>
</td>
</tr>
<tr>
<td width="200" align="left">Contact:</td>>
<td width="600" align="left">
<?php
$contact = $_POST['contact'];
echo $contact;
?>
</td>
</tr>

The question is where do you want to save it to.问题是你想把它保存到哪里。 Every time somebody visits your page, PHP builds the page just for them.每次有人访问您的页面时,PHP 都会为他们构建页面。 In your example, it is taking any POST variables and simply displaying them.在您的示例中,它采用任何 POST 变量并简单地显示它们。

There are many ways that you can store and retrieve data in PHP.您可以通过多种方式在 PHP 中存储和检索数据。 Some examples might be writing to a file, storing data as part of a session, or storing your data to a database.一些示例可能是写入文件、将数据存储为会话的一部分或将数据存储到数据库。 Those options are listed in order of how complicated they will be to apply.这些选项按照应用的复杂程度列出。

Before you store your data, though, you'll want to sanitize it.但是,在存储数据之前,您需要对其进行清理。 Even though you have a form, that doesn't stop attackers from sending any random information into those variables, and if they aren't sanitized, then whatever you do with them could potentially allow the attacker to exploit your script.即使你有一个表单,这也不能阻止攻击者向这些变量发送任何随机信息,如果它们没有被清理,那么无论你对它们做什么,都可能允许攻击者利用你的脚本。 How to sanitize data is a bit beyond the scope of this answer, but for simple data, I typically use something like this, using preg_replace to make sure that I only get letters and numbers:如何清理数据有点超出了这个答案的范围,但对于简单数据,我通常使用这样的方法,使用preg_replace来确保我只得到字母和数字:

$my_sanitized_data = preg_replace('/[^a-zA-Z0-9]/', '', $my_raw_data);

In most modern applications, data is most often written to a database.在大多数现代应用程序中,数据最常写入数据库。 This isn't too complicated, but setting up the database can be.这不是太复杂,但设置数据库可以。 You might want to check with your web host for instructions.您可能需要与您的网络托管服务商联系以获取说明。 Once you do, you may want to follow a tutorial like the following:完成后,您可能需要遵循以下教程:

https://www.w3schools.com/php/php_mysql_connect.asp https://www.w3schools.com/php/php_mysql_connect.asp

Once you learn how to write data, you can learn how to read your data, and then you can output that to the screen.一旦你学会了如何写数据,你就可以学会如何读取你的数据,然后你就可以将它输出到屏幕上。

Most likely, in the process, you'll want to learn how to use PHP sessions to track your visitor through the website.在此过程中,您很可能希望了解如何使用 PHP 会话通过网站跟踪访问者。 This can also be used to save data, and is often used along with the database.这也可以用于保存数据,并且经常与数据库一起使用。 For instance, when a visitor arrives, you may let them login.例如,当访客到达时,您可以让他们登录。 The login is handled through the database as, and then the session saves the user that's logged in, and uses that user id to find the appropriate information in the database from the last time they were logged in. Either way, you may find sessions helpful.登录通过数据库作为处理,然后会话保存登录的用户,并使用该用户 ID 从他们上次登录时在数据库中查找适当的信息。无论哪种方式,您可能会发现会话有帮助. Essentially, they are a temporary storage that lasts for a visitor until they leave your site.从本质上讲,它们是一个临时存储,供访问者使用,直到他们离开您的网站。

Here is an article on using PHP sessions:这是一篇关于使用 PHP 会话的文章:

https://www.tutorialrepublic.com/php-tutorial/php-sessions.php https://www.tutorialrepublic.com/php-tutorial/php-sessions.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在php中提交表单后如何加载上一页? - How do I load the previous page after submitting a form in php? 使用PHP在页面上提交表单后如何返回消息? - How do I return a message after submitting a form on my page with PHP? 如何通过ajax将表单发送到我的php页面 - How do I send a form through ajax to my php page 如何在wordpress页面上的html表单中包含PHP? - How do I include PHP in my html form on a wordpress page? 为什么我的 PHP 页面通过重定向它的页面提交表单? - Why is my PHP page submitting a form through the page that redirected it? 提交表单后,它会重定向到操作页面,但不会将数据保存在数据库中 - after submitting the form it redirects to action page but do not save data in database PHP-我的表单页面在提交表单后冻结 - PHP - My form page freezes after submitting form 提交表单后从php页面重定向时如何在html页面中显示警报消息 - How to show an alert message in an html page when redirected from a php page after submitting a form 提交表单并在一个字段中搜索其他 3 个按钮后,如何在浏览器中保存或保留信息/状态 - How do I save or keep information/state in the browser after submitting form and searching with one field for 3 other buttons 提交php mysqli更新表格后,我有一个空白页, - I have a blank page after submitting a php mysqli update form,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM