繁体   English   中英

将HTML URL中的参数传递给php

[英]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$derft5467uio$d的,因此我不知道该怎么办。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM