Passing a variable from one page to another
12-10-2010, 12:30 PM
|
Passing a variable from one page to another
|
Posts: 19
|
I am trying to pass a variable from one page to another. In this case it si an email address that I am trying to carry to a form on the next page. It is currently set up as follows.
1) the Email address is a hyperlink that links to the new page, which has a form.
2) I want to be able to click on the email address, and have it load the new page, with the email address loaded into the forms "to" area. It doesn't have to be visible.
3) the form is a prewritten form letter, with a few options. The code is as follows:
I have a HTML table that is populated from a database. The email feild is turned into a hyperlink. (This is working fine)
PHP Code:
<?php
$dbhost = 'localhost';
$dbuser = 'blahblah';
$dbpass = 'password';
$db = 'db_name';
$link = mysql_connect($dbhost, $dbuser, $dbpass)or die(mysql_error());
mysql_select_db($db)or die(mysql_error());
$result = mysql_query("SELECT * database_table_name")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Last Name</th> <th>First Name</th> <th>Party</th> <th>District</th> <th>Address</th> <th>City</th> <th>State</th> <th>Zip</th> <th>Email</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['Last_Name'];
echo "</td><td>";
echo $row['First_Name'];
echo "</td><td>";
echo $row['Party'];
echo "</td><td>";
echo $row['District'];
echo "</td><td>";
echo $row['Address'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Zip'];
echo "</td><td>";
//Normal Pull
//echo $row['Email'];
//echo "</td><td>";
//Just Hyperlink
//echo '<a href="http://',Email,'">',Email,'</a>';
//echo "</td></tr>";
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/letter.php?id=$Email">'.$row['Email'].'</a>';
echo "</td></tr>";
}
echo "</table>";
?>
Now I just want it so when you click ont he email link, it will load the letter.php page with the email address loaded into the "$to" field. Here is the form code.
PHP Code:
<?php
if($_POST){
$to = 'person@theirdomain.com';
$subject = "WHAT SHOULD THIS BE";
$message = "Date: $date\n\r".
"Dear $person,\n\r".
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit eleifend pharetra. Phasellus nec mauris ut nibh varius tempus. Sed non felis mauris, a posuere lacus.\n\r".
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit eleifend pharetra. Phasellus nec mauris ut nibh varius tempus. Sed non felis mauris, a posuere lacus.\n\r".
"Sincerely,\n".
"$name \n".
"$street \n".
"$city, $zip \n".
"$email \\n".
$headers = "From: $email";
mail($to, $subject, $message, $headers);
// SUCCESS!
echo '<p class="notice">'.
'Thank you for your submission. '.
'</p>';
// clear out the variables for good-housekeeping
unset($date,$legislator,$bill,$name,$street,$city,$zip,$email);
$_POST = array();
}
?>
<div id="content" class="section">
<?php arras_above_content() ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php arras_above_post() ?>
<div id="post-<?php the_ID() ?>" <?php arras_single_post_class() ?>>
<?php arras_postheader() ?>
<div class="entry-content clearfix">
<form method="post" action="" id="letter">
<p><label for="name">Date</label>
<input type="text" name="date" id="date" value="<?php echo $_POST['date'];?>"/></p>
<p>Dear Person
<select name="Person">
<option>Choose a Person</option>
<option>Senator John Q. Public</option>
<option>Rep. Jane Q. Public</option>
<option>Some Third Person</option>
</select>,</p>
<p>As a constituent of yours, I urge you to closely review Bill
<select name="bill">
<option>Choose a Bill</option>
<option>SB 13 / LC0448</option>
<option>LC0505</option>
<option>LC0532</option>
</select>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit eleifend pharetra. Phasellus nec mauris ut nibh varius tempus. Sed non felis mauris, a posuere lacus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit eleifend pharetra. Phasellus nec mauris ut nibh varius tempus. Sed non felis mauris, a posuere lacus.</p>
<p>Sincerely,<br />
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php echo $_POST['name'];?>"/><br />
<label for="email">Street:</label>
<input type="text" name="street" id="street" value="<?php echo $_POST['street'];?>"/><br />
<label for="email">City:</label>
<input type="text" name="city" id="city" value="<?php echo $_POST['city'];?>"/><br />
<label for="email">Zip:</label>
<input type="text" name="zip" id="zip" value="<?php echo $_POST['zip'];?>"/><br />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="<?php echo $_POST['email'];?>"/></p>
<input type="submit" class="button" value="Submit" />
</form>
<?php the_content( __('<p>Read the rest of this entry »</p>', 'arras') ); ?>
<?php wp_link_pages(array('before' => __('<p><strong>Pages:</strong> ', 'arras'),
'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
I am moderately new to this, so please give me details.
Thanks!
|
|
|
|
12-10-2010, 12:55 PM
|
Re: Passing a variable from one page to another
|
Posts: 879
Name: Paul W
|
When you add ?name=value pairs after a URL they make up the GET string, so you need $_GET rather than $_POST
|
|
|
|
12-10-2010, 01:05 PM
|
Re: Passing a variable from one page to another
|
Posts: 19
|
Quote:
Originally Posted by PaulW
When you add ?name=value pairs after a URL they make up the GET string, so you need $_GET rather than $_POST
|
So I changed the link in the first file to read like this:
PHP Code:
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/pass.php?Email=$Email">'.$row['Email'].'</a>';
And then simplified the letter to just show the variable that I am trying to pass. (for troubleshooting this). Here is the get code I used:
PHP Code:
<?php
$test = $_GET['Email'];
echo $test;
?>
BUt all the page prints is $Email. What am I doing wrong?
|
|
|
|
12-10-2010, 01:08 PM
|
Re: Passing a variable from one page to another
|
Posts: 1,618
Location: UK
|
If your passing variables from one page to another, use sessions. Get requests can be altered by the user.
Ie:
page1
PHP Code:
<?php session_start(); $myname = "Hello"; $_SESSION['myname'] = $myname; ?>
page2 ( teh page you want to get myname populated by page1 )
PHP Code:
<?php session_start(); // Must be at the top / before any output $myname = $_SESSION['myname']; ?>
|
|
|
|
12-10-2010, 01:11 PM
|
Re: Passing a variable from one page to another
|
Posts: 1,618
Location: UK
|
Oh and
PHP Code:
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/letter.php?id=$Email">'.$row['Email'].'</a>';
should be
PHP Code:
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/letter.php?id='.$Email.'">'.$row['Email'].'</a>';
or probably
PHP Code:
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/letter.php?id='.$row['Email'].'">'.$row['Email'].'</a>';
Last edited by lynxus; 12-10-2010 at 01:12 PM..
|
|
|
|
12-10-2010, 01:19 PM
|
Re: Passing a variable from one page to another
|
Posts: 19
|
Quote:
Originally Posted by lynxus
Oh and
PHP Code:
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/letter.php?id=$Email">'.$row['Email'].'</a>';
should be
PHP Code:
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/letter.php?id='.$Email.'">'.$row['Email'].'</a>';
or probably
PHP Code:
echo '<a href="http://www.mydomain.com/ledge/wp-content/themes/arras/letter.php?id='.$row['Email'].'">'.$row['Email'].'</a>';
|
When I make this change to my code, I get no output at all. ??? my URL now ends with this on Page 2: pass.php?Email=
|
|
|
|
12-10-2010, 01:22 PM
|
Re: Passing a variable from one page to another
|
Posts: 19
|
Quote:
Originally Posted by lazerbrains
When I make this change to my code, I get no output at all. ??? my URL now ends with this on Page 2: pass.php?Email=
|
Actually nevermind, the second one actually is outputting this in the URL, but not printing it to the page.
pass.php?Email=lastname.firstname@gmail.com
With he email address being the correct variable.
And what do you mean by the user can modify things? i am not sure that is much of a problem for what I am doing, but can you explain?
|
|
|
|
12-10-2010, 01:27 PM
|
Re: Passing a variable from one page to another
|
Posts: 19
|
Quote:
Originally Posted by lazerbrains
Actually nevermind, the second one actually is outputting this in the URL, but not printing it to the page.
pass.php?Email=lastname.firstname@gmail.com
With he email address being the correct variable.
And what do you mean by the user can modify things? i am not sure that is much of a problem for what I am doing, but can you explain?
|
OK. Sorry for the repeat replies. I got it working and Passing the variable and printing it to the second page. So all is good, I think. I just need to plug it into the letter code now and see if it still works.
But what is the main benefit of using sessions over _GET?
|
|
|
|
12-10-2010, 01:27 PM
|
Re: Passing a variable from one page to another
|
Posts: 879
Name: Paul W
|
Quote:
Originally Posted by lazerbrains
Actually nevermind, the second one actually is outputting this in the URL, but not printing it to the page.
pass.php?Email=lastname.firstname@gmail.com
With he email address being the correct variable.
And what do you mean by the user can modify things? i am not sure that is much of a problem for what I am doing, but can you explain?
|
Have you changed the other $_POST reference? Re changing values, look in your browser address bar when there's a GET string - you can edit any of those you like and see what happens. For example, change the numer at the end of the current address to see a different message.
|
|
|
|
12-10-2010, 01:43 PM
|
Re: Passing a variable from one page to another
|
Posts: 19
|
Quote:
Originally Posted by PaulW
Have you changed the other $_POST reference? Re changing values, look in your browser address bar when there's a GET string - you can edit any of those you like and see what happens. For example, change the numer at the end of the current address to see a different message.
|
Oh, yeah. I see what you mean. For this particular situation, I don't think that will be a concern. Thanks for the tip though.
So now in order to pass this to the email form, where would I put the GET statement? Or do I just change the POST to GET?
PHP Code:
?php
if($_POST){
$to = 'person@theirdomain.com';
$subject = "WHAT SHOULD THIS BE";
$message = "Date: $date\n\r".
"Dear $person,\n\r".
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit eleifend pharetra. Phasellus nec mauris ut nibh varius tempus. Sed non felis mauris, a posuere lacus.\n\r".
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit eleifend pharetra. Phasellus nec mauris ut nibh varius tempus. Sed non felis mauris, a posuere lacus.\n\r".
"Sincerely,\n".
"$name \n".
"$street \n".
"$city, $zip \n".
"$email \\n".
$headers = "From: $email";
mail($to, $subject, $message, $headers);
// SUCCESS!
echo '<p class="notice">'.
'Thank you for your submission. '.
'</p>';
// clear out the variables for good-housekeeping
unset($date,$legislator,$bill,$name,$street,$city,$zip,$email);
$_POST = array();
}
?>
|
|
|
|
|
« Reply to Passing a variable from one page to another
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|