将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-那么erft5467uio$d位应该在我的php中的$_GET('args')中捕获。问题是该值没有传递到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$dusernameInput传递给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是干什么的,所以我不知道该怎么办。