Old 18-09-2006, 06:51   #1 (permalink)
captain caveman
Registered User
 
Join Date: Jun 2006
Posts: 44
php query string

Hi,

I have a page with a php form which when submitted sends the form details as an email (which works absolutely fine). However there are a number of routes to this page and I would like to use a query string to pass a variable from the previous page so that I can add this information to the emailed details.

The link I'm using is: href="downloads-form.php?doc=project_plan"

The php code that I use to create the email is a s follows:

Code:
$mailto='me@mysite.com'; $subject="Form Response"; $body.="Download Documents Form details\n"; $body.="--------------------------------------\n"; $body.="Download: $_GET('doc')\n"; $body.="Title: $title\n"; $body.="First Name: $first_name\n"; $body.="Last Name: $last_name\n"; $body.="Address Line 1: $address_1\n"; $body.="Address Line 2: $address_2\n"; $body.="Address Line 3: $address_3\n"; $body.="Address Line 4: $address_4\n"; $body.="Postcode/Zip Code: $postcode\n"; $body.="Telephone: $telephone\n"; $body.="Email: $email\n"; $body.="Contact Me?: $checkbox2\n"; $body.="--------------------------------------\n";


The problem I have is that the variable doc returns as Array('doc') in the email rather than project_plan.

I've tried lots of ways of doing this such as using just $doc rather than $_GET('doc') but then nothing is returned.

I'm sure this is simple, any ideas what I'm doing wrong?
  Reply With Quote
Old 18-09-2006, 07:53   #2 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,623
Code:
$body.="Download: $_GET(doc)\n";


either remove the apostrophes from inside the $_GET may do it.
else just retrieve the variable before you write it into the message variable.
__________________
  Reply With Quote
Old 18-09-2006, 07:57   #3 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,919
I am no expert but is $_GET for retriving form vars, this var was sent as part of address string, $doc should do?
__________________
  Reply With Quote
Old 18-09-2006, 08:05   #4 (permalink)
captain caveman
Registered User
 
Join Date: Jun 2006
Posts: 44
datahound, that's what I thought, but I don't seem to get anything returned doing it that way...
  Reply With Quote
Old 18-09-2006, 08:29   #5 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,919
Is the email construction in the immediate next page or is there a form page in between?

If there is a form in between has it got a hidden field to carry the contents of $doc through?
__________________
  Reply With Quote
Old 18-09-2006, 08:55   #6 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,130
Just wrap the variable in curly braces {} replacment variables in strings need a bit of help if they're not straight variables.

Code:
$body.="Download: {$_GET('doc')}\n";
__________________
George Smith - a vain attempt to get higher on google for my name

  Reply With Quote
Old 18-09-2006, 09:45   #7 (permalink)
captain caveman
Registered User
 
Join Date: Jun 2006
Posts: 44
stickmus, I just tried that but get this error:

Parse error: parse error, unexpected '(', expecting '}'...

max h (a mint sauce fan eh?!), how would I retrieve the variable before I write it into the message variable? Something like:

$doc = $_GET('doc');

and then just:

$body.="Download: $doc\n";

I have tried this with no luck...
  Reply With Quote
Old 18-09-2006, 09:50   #8 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,130
I think there's something wrong with your copy/paste. The error is saying you haven't closed the opened curly brace...
__________________
George Smith - a vain attempt to get higher on google for my name

  Reply With Quote
Old 18-09-2006, 10:17   #9 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,623
Quote:
Originally Posted by datahound
I am no expert but is $_GET for retriving form vars, this var was sent as part of address string, $doc should do?

$_GET retrieves from the url string

$_POST retrieves from variables sent throught the header (i.e. forms)
__________________
  Reply With Quote
Old 18-09-2006, 10:21   #10 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,623
ok.just spotted your problem. you need to use square brackets.

i.e $_GET['doc']
__________________
  Reply With Quote
Old 19-09-2006, 03:49   #11 (permalink)
captain caveman
Registered User
 
Join Date: Jun 2006
Posts: 44
I'm still getting nowhere with this, I've tried

$doc = $_GET['doc'];

and then

$body.="Download: $doc\n";

and also tried

$body.="Download: $_GET['doc']\n;

to no avail.

Really can't understand why this isn't working.
  Reply With Quote
Old 19-09-2006, 04:39   #12 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,919
Quote:
Originally Posted by datahound
Is the email construction in the immediate next page or is there a form page in between?

If there is a form in between has it got a hidden field to carry the contents of $doc through?

Was this relevant?
__________________
  Reply With Quote
Old 19-09-2006, 05:42   #13 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,623
__________________
  Reply With Quote
Old 19-09-2006, 06:45   #14 (permalink)
captain caveman
Registered User
 
Join Date: Jun 2006
Posts: 44
Sorry datahound, trying to do 5 things at once here so not fully on the ball...

Thanks for your help Max, still not able to get it working though - am I being totally thick?!! (probably)

The php script resides on the same page that the form does, so the form action sends it back to itself and then the script sends it on to the thankyou page if all the conditions are met (form validation etc).

Basically it is used so that when a link is clicked to download example documentation from the website it takes you to a form that needs to be filled out. Once submitted, the details are sent to us and an email is sent to the user with a link to the download page.

What I want to be able to do is add a line to the details that are sent to us that tells me which document it was that the user wanted to download in the first place. Therefore adding a query string to each link and retrieving this means it can be sent in the email.

Here's the whole php:

Code:
<?php $doc = $_GET['doc']; function check_telephone($telephone){ if(!preg_match("/[^0-9\ ]+$/",$telephone)) return true; else return false; } function check_email_address($emailaddress) { // First, we check that there's one @ symbol, and that the lengths are right if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $emailaddress)) { // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. return false; } // Split it into sections to make life easier $email_array = explode("@", $emailaddress); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) { if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { return false; } } if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) { return false; // Not enough parts to domain } for ($i = 0; $i < sizeof($domain_array); $i++) { if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { return false; } } } return true; } // only validate form when form is submitted if(isset($submit_button)){ $error_msg=''; if($checkbox!=="checkbox"){ $error_msg.="You must agree to the Terms and Conditions<br />"; } if($title=='') { $error_msg.='Please enter a Title<br />'; } if($first_name=='') { $error_msg.='Please enter your First Name<br />'; } if($last_name=='') { $error_msg.='Please enter your Last Name<br />'; } if($address_1=='') { $error_msg.='Please enter the first line of your Address<br />'; } if($postcode==''){ $error_msg.='Please enter your Postcode<br />'; } if($telephone=='') { $error_msg.='Please enter your Telephone Number<br />'; } else if(!check_telephone($telephone)){ $error_msg.='Please enter a valid Telephone Number<br />'; } if($email=='') { $error_msg.='Please enter your Email Address<br />'; } else if(!check_email_address($email)) { $error_msg.='Please enter a valid Email Address<br />'; } // display error message if any, if not, proceed to other processing if($error_msg==''){ if($checkbox2=="checkbox"){ $checkbox2 = Yes; } else{$checkbox2 = No; } $mailto='me@company.com'; $subject="Form Response"; $body.="Download Documents Form details\n"; $body.="--------------------------------------\n"; $body.="Download: $doc\n"; $body.="Title: $title\n"; $body.="First Name: $first_name\n"; $body.="Last Name: $last_name\n"; $body.="Address Line 1: $address_1\n"; $body.="Address Line 2: $address_2\n"; $body.="Address Line 3: $address_3\n"; $body.="Address Line 4: $address_4\n"; $body.="Postcode/Zip Code: $postcode\n"; $body.="Telephone: $telephone\n"; $body.="Email: $email\n"; $body.="Contact Me?: $checkbox2\n"; $body.="--------------------------------------\n"; mail($email, Company Document Download', '<html> <body> <p>Thankyou for your interest in viewing our example documentation, please click the link below to download the required documents</p> <p><a href="http://www.company.com/flash/downloads/_f-documentation45knfi3578.html">http://www.company.com/flash/downloads/_f-documentation45knfi3578.html</a></p> </body> </html>', "To: $title $first_name $last_name <$email>\n" . "From: Company Name<information@company.com>\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1"); if (mail($mailto,$subject,$body,"From: $email\n")){ header("Location: f-thanks.php"); } else { header("Location: f-error.php"); } } } ?>
  Reply With Quote
Old 19-09-2006, 07:33   #15 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,919
I doesn't matter if it is the same pagename it still has to be held in the form in a hidden field and passed forward for processing with the new vars collected in the form. Unless you are using session vars in which case they are held in the air for use at any time.
Try putting this in your form?
<input name="doc" type="hidden" id="doc" value="<?php echo"$doc"; ?>">
__________________
  Reply With Quote
Old 19-09-2006, 08:09   #16 (permalink)
captain caveman
Registered User
 
Join Date: Jun 2006
Posts: 44
That's great, thanks mate that's worked a treat - will bear that in mind in future!
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8