[英]Pass the parameters in the HTML URL to php
所以我有一个html
文件和一个php
文件,我要做的是将html URL
参数中的值传递给php
。
因此,例如,如果URL为localhost\\files\\reset_username.html?args=erft5467uio$d
则应在php
的$_GET('args')
中捕获erft5467uio$d
位。 问题是此值未传递到php
文件本身。
这是html
文件:
<html>
<form id='setUsername' action='setnewusername.php' method='post'>
<label for='username' >New Username: </label>
<input type='text' name='usernameInput' id='username' />
<label for='newUsername' >Confirm Username: </label>
<input type='text' name='newUsernameInput' id='newUsername' />
<input type='text' name='Submit' value='Change Username' />
</form>
</html>
这是我的php
代码:
<?php
require "init.php";
if(!empty($_GET['args']) && !empty($_GET['usernameInput'])){
$paramOne = $_GET['args'];
$paramTwo = $_GET['usernameInput'];
echo $paramOne;
echo $paramTwo;
}
else{
echo "The args was not received";
}
?>
当我测试我的代码时,php打印"The args was not received"
。 这意味着未传递值erft5467uio$d
。
如何将这些值传递给php
脚本?
编辑:每次用户请求更改其用户名,他们都会收到一个具有唯一args
值的链接。 在这种情况下,用户会收到一封电子邮件,该电子邮件的链接为localhost\\files\\reset_username.html?args=erft5467uio$d
。 当他们单击链接时,它们将被定向到html页面。 填写表单后,必须将代码erft5467uio$d
以及usernameInput
传递给php
谢谢
前言:我不明白您为什么要为此使用href
和表单。
如果您希望此方法有效,建议您仔细阅读以下内容,因为它相当长,请仔细进行。
旁注:我想指出,您的提交按钮使用了错误的类型,是text
而不是submit
。
<input type='text' name='Submit' value='Change Username' />
内容应为:
<input type='submit' name='Submit' value='Change Username' />
首先,您的表单/ PHP :(我删除了不再适用的几行内容)。
form.html (示例文件名)
<html>
<form id='setUsername' action='setnewusername.php' method='post'>
<label for='username' >New Username: </label>
<input type='text' name='usernameInput' id='username' />
<label for='newUsername' >Confirm Username: </label>
<input type='text' name='newUsernameInput' id='newUsername' />
<input type='submit' name='Submit' value='Change Username' />
</form>
</html>
如果要传递参数,则通过电子邮件发送给用户的内容将是带有参数的URL。
注意:请仔细阅读整个代码中的一些注释。
setnewusername.php
注意:如果要使用<input type='text' name='newUsernameInput' id='newUsername' />
则需要将其添加到PHP。 我不确定您要如何使用它。
<?php
require "init.php";
if( isset($_POST['Submit']) && !empty($_POST['usernameInput']) ){
$username = $_POST['usernameInput'];
echo $username;
$user = "erft5467uio"; // This is probably what should be taken from one of your inputs.
$to = "your_email@example.com";
$subject = "Your subject here";
$from_email = "email@example.com";
$message = "
Dear $username,
Visit the following site to verify your account changes:
http://www.example.com/from_mail.php?args=$user&usernameInput=$username
";
$headers = "From: $username <$from_email>\r\n";
/* or use the below
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
*/
mail($to, $subject, $message, $headers);
// mail() or your own mailer
}
else{
echo "The args was not received.";
}
?>
上面的示例http://www.example.com/from_mail.php?args=$user&usernameInput=$username
将生成类似以下内容:
http://www.example.com/from_mail.php?args=john&usernameInput=BigBadJohn
然后在传递自上述参数的下一个文件from_mail.php中 (一旦用户单击您发送的链接,该文件就会启动,然后您将执行以下操作:
<?php
require "init.php";
if(!empty($_GET['args']) && !empty($_GET['usernameInput'])){
$paramOne = $_GET['args'];
$paramTwo = $_GET['usernameInput'];
echo $paramOne;
echo "<br>";
echo $paramTwo;
// do something else here such as db insertion etc.
}
else{
echo "The args was not received";
}
?>
脚注:
最后说明:
我不知道erft5467uio$d
$d
中的erft5467uio$d
是erft5467uio$d
的,因此我不知道该怎么办。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.