简体   繁体   中英

Inserting variable into PHP header Location URL

I'm very new to 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.

I would like to include two of the form fields into the URL string so that I can then call up in the confirmation page to congratulate the name of the person for joining our club.

Two Questions

  1. How do I concatenate the redirect url to include first name and last name for example: new_member_confirmation.html?firstname=$firstname&lastname=$lastname
  2. how do I then 'GET' the names in the URL string to display on the confirmation html page. For example "Thank you Bill Smith for becoming a member". I dont have an example of this as I do not know how to call or GET the values from the url string into the HML page

Here is the revised code which does not include every form field variable but gives you the gist of the PHP script.

 <?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. You you can concatenate to the url in your header statement, and use urlcode for your variables:

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

  2. The header needs to reference a PHP file instead of an html file:

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

    In the membership_confirmation.php file, get the variables via $GET similarly to how you are getting them from $POST in your current file, eg:

    $firstname = $_GET['firstname'];

Ok here is what I came up with.

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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