繁体   English   中英

将变量插入 PHP header 位置 URL

[英]Inserting variable into PHP header Location URL

我对 PHP 很陌生。

I have an HTML membership form that when a user clicks the submit button it goes to a PHP page that compiles the fields passed, sends an email and lastly forwards the user to a "Thank you" Confirmation" html page.

我想在 URL 字符串中包含两个表单字段,这样我就可以在确认页面中致电祝贺加入我们俱乐部的人的姓名。

两个问题

  1. 如何连接重定向 url 以包含名字和姓氏,例如:new_member_confirmation.html?firstname=$firstname&lastname=$lastname
  2. 然后我如何“获取” URL 字符串中的名称以显示在确认 html 页面上。 例如“感谢 Bill Smith 成为会员”。 我没有这样的例子,因为我不知道如何调用或获取 url 字符串中的值到 HML 页面

这是修改后的代码,它不包括每个表单字段变量,但为您提供了 PHP 脚本的要点。

 <?php
    // Multiple recipients
    $to = 'info@abs.com'; // note the comma
    
    
    // Form http_post_fields
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];

    $subject = 'New Membership';
    $cdateTime =  date("l jS \of F Y h:i:s A");
    
    // Message
    $message = "
    <html>
    <head>
      <title>New Membership</title>
    </head>
    <strong>From:</strong> \n $firstname \n $lastname <br>
    <strong>Email:</strong> \n $email <br>
    <strong>Date/Time Submitted:</strong>\n $cdateTime
    </font></p>
    <body>
    
    </body>
    </html>
    ";
    
    // To send HTML mail, the Content-type header must be set
    $headers[] = 'MIME-Version: 1.0';
    $headers[] = 'Content-type: text/html; charset=iso-8859-1';
    
    // Additional headers
    $headers[] = 'To: Club Name <info@domain.com>'; // include a comma for more than one recipiant
    $headers[] = 'From: Club Name <info@domain.com>';
    
    // Mail it
    mail($to, $subject, $message, implode("\r\n", $headers)) or die("Email was not sent!");

// Here is where the user gets sent to the page below. Id like to append the $firstname $lastname variables into the URL string

    header('Location: membership_confirmation.html');

// I thought I could create a URL string variable above like this --
// $urString = "Location: membership_confirmation.html?membername=$firstname \n $lastname";
// and then add it to the last portion of the script like this
// header($urString);

// Unfortunately, that doesn't work after I tried testing it.
    
    ?>
  1. 您可以在 header 语句中连接到 url ,并对变量使用urlcode

    header('Location: membership_confirmation.html?firstname='.urlencode($firstname).'&lastname='.urlencode($lastname));

  2. header 需要引用 PHP 文件而不是 html 文件:

    header('Location: membership_confirmation.php?firstname...

    membership_confirmation.php文件中,通过$GET获取变量,类似于从当前文件中的$POST获取变量的方式,例如:

    $firstname = $_GET['firstname'];

好的,这就是我想出的。

<!DOCTYPE html>
<html>
<body>


<p id="demo"></p>

<script>


var url_string = "https://eaa309.club/membership_confirmation.html?firstname=Kelly&lastname=Brady"; 

var url = new URL(url_string);
var firstname = url.searchParams.get("firstname");
var lastname = url.searchParams.get("lastname");
//console.log(c);
//Write it out

document.getElementById("demo").innerHTML =
"Page path is: " + firstname + " " + lastname;

</script>

</body>
</html>

暂无
暂无

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

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