/    Sign up×
Community /Pin to ProfileBookmark

Using Variables From Another Page

in php:

[code=php]
<?php
$fruitlink =”;
$thankyou = ‘thankyou.html’; // required
$mailfrom = ‘[email protected]’; // required
$mailcopy = ‘[email protected]’; // optional
$mailsubj = ‘Received Email’; // required
$cr = “r”; // carriage-return character
$lf = “n”; // line-feed character
$lterm = $cr.$lf; // headers and body line terminator for email
$messages = ”; // form fields error messages
$fruit = ”; // fruit field

if($fruit == “apples”):
$fruitlink = ‘http://www.yahoo.com’;
elseif($fruit == “oranges”):
$fruitlink = ‘http://www.msn.com’;
elseif($fruit == “grapes”):
$fruitlink = ‘http://microsoftcom’;
endif;

// —————————————- //
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’):
//
$fruit = $_POST[‘fruit’]; // fruit field
//
if($fruit==’Select One’): // amount selection
$messages .= ‘5. Select A FRUIT<br>’.$lf;
endif;
//

if(strlen($messages)==0): // if all validations are good
$mailheaders = ‘Content-Type: text/plain;’.$lterm; // set extra mail headers
$mailheaders .= ‘X-Mailer: PHP;’.$lterm;
$mailheaders .= ‘X-MSMail-Priority: High;’.$lterm;
$mailheaders .= ‘X-Priority: 1;’.$lterm;
$mailheaders .= ‘Return-path: ‘.$mailfrom.$lterm;
$mailheaders .= ‘Sender: ‘.$mailfrom.$lterm;
$mailheaders .= ‘From: ‘.$mailfrom.$lterm;
if (isset($mailcopy)
&& !empty($mailcopy)):
$mailheaders .= ‘Cc: ‘.$mailcopy.$lterm;
endif;
$mailbody = ” Your selected fruit is {$fruit}
” .$lterm;
ini_set(sendmail_from, $mailfrom);
if (mail($emailadr, $mailsubj, $mailbody, $mailheaders, “-f $mailfrom”)):
ini_restore(sendmail_from);
header(‘Location: http://’.$_SERVER[‘HTTP_HOST’]
.dirname($_SERVER[‘PHP_SELF’])
.’/’.$thankyou)
exit;
endif;
ini_restore(sendmail_from);
$messages = ‘Failed to send email!’;
endif;
// —————————————- //
endif;
//
function set_selected($form_field, $field_value, $field_index)
{
if (isset($_POST[$form_field])
&& !empty($_POST[$form_field])): // if form field is present in post data
if ($field_value == $_POST[$form_field]): // and equals the expected value
$result = ‘selected’; // then this form field should be checked
else: // else
$result = ”; // don’t check it
endif;
else: // when no post data present
if ($field_index == 0): // and this is the first form field in the group
$result = ‘selected’; // then this form field should be checked
else: // else
$result = ”; // don’t check it
endif;
endif;
return $result; // return the selection result
}
?>
[/code]

in html:

[code=html]
<?php
if(strlen($messages)!=0):
echo ‘<p style=”font-size:11pt;color:red;”>’ .$messages. ‘</p><br>’ .$lf;
endif;
?>

<form name=”myform” action=”<?=basename($_SERVER[‘PHP_SELF’])?>” method=”post”>
<FIELDSET style=”PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; WIDTH: 425px; PADDING-TOP: 5px”><LEGEND><STRONG><FONT color=#003399>SELECT A FRUIT</FONT></STRONG></FONT></LEGEND>
<br>
<select name=”fruit”>
<option <?=set_selected(‘fruit’, ‘Select One’, 0)?>>Select One</option>
<option <?=set_selected(‘fruit’, ‘Apples’, 1)?>>Apples </option>
<option <?=set_selected(‘fruit’, ‘Oranges’, 2)?>>Oranges</option>
<option <?=set_selected(‘fruit’, ‘Grapes’, 3)?>>Grapes</option>
</select><br><br>
</fieldset>
</form>
[/code]

The method above sends us the email and redirect them to the thank you page. Works great. We need a little bit of change and I don’t know how to do it.

Since the information are already gathered in php using variables, how do we use them on the thank you page? See example below:

[INDENT][INDENT]Your selected fruit is {$fruit} please click on this link (the link will be containing “http://www.yahoo.com“).[/INDENT][/INDENT]

Any help is appreciated.
Thank you so much, you guys rock!

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Jan 19.2009 — You can place them in hidden form elements, session or in the url. I think that covers it all.
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 19.2009 — I don't know how to do that :o

Could you show me an example?
Copy linkTweet thisAlerts:
@skywalker2208Jan 20.2009 — With a session you use session_start to start a new session and you can place info in the session array.

With a url you can place items in the url like www.example.com?fruit=apple
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 20.2009 — I don't think I made what I needed clear enough. I thought it would be something simple since the variables already exists and I can use it on any other page withouth redeclaring them again using session_start().

From the way I tried it, I received 2 emails since it posted 2x. We only need 1 email and use $fruitlink on another page.

I am so confused ?

Thanks
Copy linkTweet thisAlerts:
@MindzaiJan 20.2009 — You could either change this line:

[code=php]
header('Location: http://'.$_SERVER['HTTP_HOST']
.dirname($_SERVER['PHP_SELF'])
.'/'.$thankyou)
[/code]


To read

[code=php]
header('Location: http://'.$_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/$thankyou?fruit=$fruit")
[/code]


You can then access the data using the $_GET superglobal:

[code=php]
echo $_GET['fruit'];
[/code]


Or as suggested previously you can use sessions (which doesn't require you to redeclare anything, just add the data to the session and it will be available on all future pages which include the session_start function.

However, your $thankyou page is an html file, which means that unless you have configured your server to process html files through the php parser, no php code in that file will do anything.
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 21.2009 — Does that mean I simply need to change the variables I will be using on the other php page like below?


original page page is:
[code=php]<?php
$fruitlink ='';
?>
[/code]


new code for first page will be:
[code=php]<?php
session_start();
$_SESSION['$frutlink'];
?>
[/code]



FYI, 2nd page is the thank you page with a link bringing them to the 3rd page.


3rd page php would be:
[code=php]<?php
session_start();
echo $_SESSION['$frutlink'];
?>
[/code]


[code=html]
[B][COLOR="Red"]how do i show the link here?[/COLOR][/B]
[/code]



Is this correct?
Copy linkTweet thisAlerts:
@MindzaiJan 21.2009 — 
original page page is:
[code=php]<?php
$fruitlink ='';
?>
[/code]


new code for first page will be:
[code=php]<?php
session_start();
$_SESSION['$frutlink'];
?>
[/code]
[/quote]


The value in between the square brackets is a key. and it totally arbitrary (unless you let php manage the indexing for you but that is not what you need in this case), not the value, so it should look like this:

[code=php]
// 1st page:
session_start();
$fruitlink = 'apple';
$_SESSION['fruitlink'] = $fruitlink;


// 2nd page:
session_start(); // <-- must be called on every page you want to use $_SESSION
echo $_SESSION['fruitlink']; // outputs: apple
$_SESSION['fruit2'] = 'orange';


// 3rd page:
session_start();
echo $_SESSION['fruitlink'] // outputs: apple
echo $_SESSION['fruit2'] // outputs: orange
[/code]


If 2nd page is a plain .html file, you wont be able to do this, all pages involved need to be parsed as PHP
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 21.2009 — ?

Thank you! First page was in php, second page was in html, 3rd page is in php and everything is working GREAT!!!!

I can't thank you guys enough!

But I want to make this link a little savy and use a button with a graphic:

in page3.php:
[code=php]
<?php
session_start();
?> [/code]


html in pag3.php:
[code=html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<SCRIPT language="javaScript">
<!--hide
function submitForm()
{document.myform.submit();}
//-->
</SCRIPT>
</head>
<body>
BLAH BLAH TEXT HERE
<br><br><br>
<form name="myform" action="<?=basename($_SERVER['PHP_SELF'])?>" method="post">

<?php
echo $_SESSION['thankyou'];
?>
<br><br>

instead of the link above, we want to incorporate it to the button below
<br><br>

<label><img src ="buttonimagehere.gif" name="button" onClick = "javascript:submitForm()"></a></label>
<form><br><br>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 22.2009 — Almost there, I got these to work with some reading from this forum, THANK YOU!

A little problem tho, see below.

code in php:
[code=php]$fruitlink = "<a href='https//www.yahoo.com'>$image</a>";[/code]

end result in page 3 php which is working:
[code=html]<a href='http://www.yahoo.com'><img src="http://www.myhome/someimage.gif" border="0"/><br /></a><br>[/code]

BUT I need to add target="_blank" so it opens on another window and I don't know how to do it!

Thanks again everyone, appreciate all the help.
Copy linkTweet thisAlerts:
@MindzaiJan 22.2009 — You should just be able to change the value of $fruitlink:

[code=php]$fruitlink = "<a href="https//www.yahoo.com" target="_blank">$image</a>";[/code]
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 22.2009 — Dear Minndzai:

I am getting an error message:


Page Not Found

We cannot locate the page you're looking for. Please check the address and make sure all letters are lowercased with no spaces. You may also move to a different page by using the links in the menu bar above.

I simple copied and pasted what you had given me.

Thanks

Lisa
Copy linkTweet thisAlerts:
@MindzaiJan 22.2009 — Dear Minndzai:

I am getting an error message:


Page Not Found

We cannot locate the page you're looking for. Please check the address and make sure all letters are lowercased with no spaces. You may also move to a different page by using the links in the menu bar above.

I simple copied and pasted what you had given me.

Thanks

Lisa[/QUOTE]


Looks like the colon is missing between the https and the slashes in your original code. Try this:

[code=php]$fruitlink = "<a href="https://www.yahoo.com" target="_blank">$image</a>"; [/code]
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 23.2009 — Thank you!!!!

:d
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 26.2009 — [code=php]$fruitlink = "<a href="https://www.yahoo.com" target="_blank">$image</a>"; [/code][/QUOTE]



Dear Mindzai:

How do I turn your example above to:
[code=php]$fruitlink = "<a href="#null" onClick="newWindow('http://yahoo.com/','','700','600','resizable,scrollbars,status,toolbar')">$image</a>;
[/code]



I found a java script that opens a new window and move it to a certain position on the screen so it won't overlap with the main window.

I can't get the it work, getting string errors :p

Thanks again

Lis
Copy linkTweet thisAlerts:
@MindzaiJan 26.2009 — Dear Mindzai:

How do I turn your example above to:
[code=php]$fruitlink = "<a href="#null" onClick="newWindow('http://yahoo.com/','','700','600','resizable,scrollbars,status,toolbar')">$image</a>;
[/code]



I found a java script that opens a new window and move it to a certain position on the screen so it won't overlap with the main window.

I can't get the it work, getting string errors :p

Thanks again

Lis[/QUOTE]


Try this:

[code=php]
$fruitlink = "<a href="#null" onClick="newWindow('http://yahoo.com/','','700','600','resizable,scrollbars,status,toolbar')">$image</a>"; [/code]


Just as an aside javascript popup windows are not a very nice way of displaying your content, especially when they do things like resize the browser window. People tend to get annoyed with them and they are generally best avoided (unless you have a specific audience, like if youre creating an intranet site or something). If it were me I'd look into creating an absolutely positioned css 'popup'. You'll find lots if you google for CSS popups but this is an example: http://bucarotechelp.com/design/csseasy/97112700.asp
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorJan 27.2009 — Dear Mindzai:

Oh thank you so much!!! Really I can't thank you enough. I am not a programmer but can work pretty well with examples. I tell ya, I've been ? about all these.

Also, I just posted another question about 2 submit buttons. Very much appreciated if you can take a look at it.

Thanks

Lisa
×

Success!

Help @xoxLISAxox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...