Reputation: 59
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
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 = '[email protected]'; // 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 <[email protected]>'; // include a comma for more than one recipiant
$headers[] = 'From: Club Name <[email protected]>';
// 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.
?>
Upvotes: 0
Views: 1301
Reputation: 59
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>
Upvotes: 0
Reputation: 750
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));
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, e.g.:
$firstname = $_GET['firstname'];
Upvotes: 2