更改 Javascript 控制的 HTML 表单的值,并对输入进行编码并传递到 PHP

Changing the value of a Javascript controlled HTML form and encoding the input and passing through to PHP

本文关键字:编码 PHP 输入 HTML Javascript 表单 更改 控制      更新时间:2023-09-26

我对PHP相对较新,我认为这将是一个简单的管理代码。我想做的是在页面上有一个HTML输入框,当有人在其中输入名称时,使用Javascript,我希望能够在同一个输入框中输出名称的编码版本,前面有一个网站字符串创建一个新链接。现在,当有人在其 Web 浏览器中访问此链接时,它会在页面上显示 name 变量。

John 在 HTML 表单中输入他的名字。

"约翰·狄金森"

当他按下提交按钮时,他刚刚输入姓名的 HTML 输入框将更改为以下内容:

"http://www.example.com/johndickinsonencoded/"

其中Johndickinsencode是他名字的编码字符串。

在 Web 浏览器中访问"http://www.example.com/johndickinsonencoded/"时,它会在屏幕上输出:

"你好约翰·迪金森"

帮助将不胜感激:)

您将需要某种方式使用 JavaScript 对字段进行编码,并在字符串到达 PHP 时对其进行解码。选择一个前进和后退的编解码器应该相当容易,即使只使用 base64 也应该可以做到。(JavaScript 没有内置的 base64 编码支持,但有很多这样的例子。如果您有其他方法来编码和解码字段,那也很好。

在服务器端,您将不得不使用 Apache mod_rewrite 规则进行一些重写。基本上,您需要在httpd.conf部分或本地.htaccess文件中使用类似的东西:

# use mod_rewrite to enable passing encoded user name as a "Clean URL"
RewriteEngine On
# Define the rewrite base -- / says use what is immediately after the hostname part
RewriteBase /
# Send bare requests to index.php
RewriteRule ^$  index.php [L]
# Don't rewrite requests for files, directories, or symlinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Send requests to index.php, appending the portion following the RewriteBase
RewriteRule ^(.*)$ index.php?n=$1 [QSA,L]

最后一个将重写您的原始 url:"http://www.example.com/johnsmithencoded"为"http://www.example.com/index.php?n=johnsmithencoded",从那里,您可以通过 $_GET['n'] 获取查询参数并根据需要对其进行解码。