doushi1900 2018-03-06 18:04
浏览 80

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

I have made an html page with forms and tables where I input words and numbers and have submit button. 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? Here is part of my html code where I input the information:

<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:

<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>
  • 写回答

1条回答 默认 最新

  • douquan2023 2018-03-06 19:49
    关注

    The question is where do you want to save it to. Every time somebody visits your page, PHP builds the page just for them. In your example, it is taking any POST variables and simply displaying them.

    There are many ways that you can store and retrieve data in 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:

    $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

    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. 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. Essentially, they are a temporary storage that lasts for a visitor until they leave your site.

    Here is an article on using PHP sessions:

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

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?