Home arrow PHP Tutorials

Translate This Website
Friday, 25 July 2008
Web Development
Request a Quote
Script Categories
RSS Media Grabber Funny Videos & Pictures Life Stories (Flash) Video Search & Download Video Downloader Script Country on Sale Advanced Polls Phone Upload/SMS Scripts PhotoCube Script SlideShow Creation Script Make Confessions Scripts Guitar Tabs Scripts Create Custom Smiley Script Short URLs & Subdomains Hot Or Not Game Scripts Put your text on images Watermark & Image Hosting Joomla Components Low Cost Scripts
Partners
WEBSITE TRAFFIC
Pissed Customers
Credit Cards
Broadband Tweaks & Info
eBay Sniping Software
Custom Programming
Scripts and Stuffs
Hot php scripts
Add your scripts
Script Search
php scripts directory
script heaven
the cgi site
Scripts
Software Development
Free Icons
SEO Services


Passing Variables with Forms Print E-mail

This tutorial will (hopefully, if I can manage to be coherent) show you, my fellow php-er, how to pass a variable through 2 pages. 

 It doesn't sound very difficult, but it is a mite tricky. The way I used it was to verify information from a form submission. In this case, the user enters their info in a form and clicks

"submit,"

but instead of having the information submitted immediately, it is first displayed to the user again for them to check it. This way, he/she/it has the option of saying, "Oh, suck! I put in the wrong information!" In other case, I have a list of links, and when the user clicks on one, the script shows the person what he/she/it clicked on and asks if that's really what they wanted... we all know how mouse pointers can wander. So, on with the tutorial...

First, I have a basic HTML form, which I'm going to assume you know how to do. When the user clicks the submit button from there, the action is . With that, he/she/it is taken to the page that will check their info.

First, we make sure the temp .txt file can be opened, in "write" mode:

if(!($myFile = fopen("tempfile.txt", "w+"))) { print("Whoa there! Something broke. Try again."); exit; }

Then, we write what they entered to that tempfile.

txt: fputs($myFile, "Name: "); fputs($myFile, "$name\n\n"); fputs($myFile, "Email address: "); fputs($myFile, "$email\n\n"); fputs($myFile, "$information\n"); fclose($myFile);

Now we will reopen that file for reading only, then print the information in that file on the displayed page. When php writes to a file, it puts in slashes with any apostrophes, so we have to strip those out of the .txt file so the user doesn't get an ugly, slashed-up version of what he/she/it entered originally:

$myFile = fopen("squirrel.txt", "r"); while(!feof($myFile)) { $myLine = fgets($myFile, 3000); print(stripslashes("$myLine")); print("\n"); } fclose($myFile);

Now, since all the information is already written to a text file, none of those variables need to be passed through. However, my goal is to have that information emailed to me, and I want the email to be CC'ed to the user. Therefore, I need to pass the $email variable through to the next page. Here's how I did that: The final page's script is as follows... this script emails me the information from the text file and CC's that email to the user who entered it:

$mailTo = " , $sendTo";rn$mailSubject = "a subject"; $mailHeader = "From: $sendTo"; $myFile = fopen("tempfile.txt", "r"); $message = fread($myFile, 3000); $message=str_replace("\\","",$message); mail($mailTo, $mailSubject, $message, $mailHeader);

That's it. I hope it made sense. Perhaps someday I'll write an even simpler variable-passing tutorial based on my other script, which doesn't use the form. Just let me know if any of you think that would be helpful! However, the basic idea is the same: rename the variable in your tag. Verbosity is a curse.  

Creadit: www.phpdeveloper.org 





Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!Free social bookmarking plugins and extensions for Joomla! websites!
 
< Prev   Next >
Services
Script Installation
Hosting
Free Image Hosting
PageRank Checker
Login Form
On Sale
PhotoCube
PhotoCube
$44.99
$24.99
You Save: $20.00
Add to Cart
Home Announcements Support Newsletter Forum PHP Articles PHP Tutorials Scripts FAQs Free Scripts Links Contacts
Copyrighted © 2008 phppod.com