【问题标题】:Pass the parameters in the HTML URL to php将 HTML URL 中的参数传递给 php
【发布时间】:2016-07-11 03:23:13
【问题描述】:

所以我有一个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$d 以及usernameInput 必须传递给php

谢谢

【问题讨论】:

  • 咳咳 => method='post'
  • 正如 Fred 所说,您应该使用 $_POST['args']。通常$_GET请求被&lt;a href="url/to/file?args=data"&gt;link&lt;/a&gt;使用
  • ...或method='get' 或将其作为附加参数传递给您的action='setnewusername.php'action='setnewusername.php?arg...'
  • 对 html 文件的请求中的 GET 参数将永远到达 php 脚本,除非您以非常奇怪的方式配置了 http 服务器。
  • 是的,但用户会收到一封电子邮件,其中包含localhost\files\reset_username.html?args=erft5467uio$d 在这种情况下指向他们的电子邮件的链接。当他们单击链接时,他们将被定向到 html 页面。填写后,代码erft5467uio$d 必须传递给php

标签: php html http-get


【解决方案1】:

前言:我不明白你为什么要使用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。

注意:遍历代码中的一些 cmets。

setnewusername.php

注意:如果您想使用&lt;input type='text' name='newUsernameInput' id='newUsername' /&gt; 的值,那么您需要将其添加到 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&amp;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 是干什么用的,所以我不知道该怎么办。

【讨论】:

    猜你喜欢
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多