Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Passing a variable from one page to another
Old 12-10-2010, 12:30 PM Passing a variable from one page to another
Average Talker

Posts: 19
Trades: 0
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 &raquo;</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!
lazerbrains is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-10-2010, 12:55 PM Re: Passing a variable from one page to another
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
When you add ?name=value pairs after a URL they make up the GET string, so you need $_GET rather than $_POST
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-10-2010, 01:05 PM Re: Passing a variable from one page to another
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by PaulW View Post
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?
lazerbrains is offline
Reply With Quote
View Public Profile
 
Old 12-10-2010, 01:08 PM Re: Passing a variable from one page to another
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
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'];
?>
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 12-10-2010, 01:11 PM Re: Passing a variable from one page to another
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
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>'
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 12-10-2010 at 01:12 PM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 12-10-2010, 01:19 PM Re: Passing a variable from one page to another
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by lynxus View Post
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=
lazerbrains is offline
Reply With Quote
View Public Profile
 
Old 12-10-2010, 01:22 PM Re: Passing a variable from one page to another
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by lazerbrains View Post
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?
lazerbrains is offline
Reply With Quote
View Public Profile
 
Old 12-10-2010, 01:27 PM Re: Passing a variable from one page to another
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by lazerbrains View Post
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?
lazerbrains is offline
Reply With Quote
View Public Profile
 
Old 12-10-2010, 01:27 PM Re: Passing a variable from one page to another
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Quote:
Originally Posted by lazerbrains View Post
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-10-2010, 01:43 PM Re: Passing a variable from one page to another
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by PaulW View Post
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(); 

?> 
lazerbrains is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Passing a variable from one page to another
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46243 seconds with 12 queries