 |
01-04-2003, 10:47 AM
|
Javascript within PHP
|
Posts: 684
Location: Sheffield, England
|
We have a recommend this page script operating here... Recommend this page
The page contains a php script for a new page to be displayed after the form has been filled out. The php script for this page is...
<?php
If ($to_email && $message && $subject) {$to = "\"$to_name\" <$to_email>";
$from = "\"$from_name\" <$from_email>";
$to = str_replace("\\'", "'", $to);
$from = str_replace("\\'", "'", $from);$subject = str_replace("\\'", "'", $subject);
$message = str_replace("\\'", "'", $message);
mail($to, $subject, $message, "From: $from\nX-Mailer: SafariQuip");
echo "Thank you for your recommendation.<br><br>Mail message sent : \nTo : $to<br>\nFrom : $from<br>\nSubject : $subject<br>\nMessage : $message";
exit; } ?>
I was trying to put a simple javascript into this piece of php. Just a simple hyperlink to go back 2 pages in the history in order to allow the user to continue shopping on the site.
I have however been unsuccessful thus far in my attempts to do this.
Can anyone shed some light on this subject and help me out a little?
Many thankx 
|
|
|
|
01-05-2003, 05:36 PM
|
|
Posts: 373
Location: Ames, IA
|
Dave. Hi again.
Ok I think if I understand correctly you want the user to be returned to the page which they were visiting before they clicked on the Recommend link right?
Well in php you there are variables which will give you the page the user came from. This is called HTTP_REFERER. Now as the documentation on the php site says, this is not always set by the browser. This however is actually probably less than 1 percent of the browsers and therefore is actually better to use than a javascript (because more people surf without javascript than with a hacked browser).
Ok so how to use this? It can be used as follows:
echo( $HTTP_SERVER_VARS['HTTP_REFERER'] );
This would print the referer url to the screen just like any other php variable.
Now in your case you are going to have 3 pages.
Page 1 - Page where user clicks "Recommend"
Page 2 - Where user enters info.
Page 3 - Page where user's email is sent via mail() and confirmation printed
Well page 1 is easy. Its just a page with a "Recomend" link to page 2.
At page 2 users should put in their details. However you should add a hidden field to the form using this code:
<input type="hidden" name="referer" value="<?php echo("$HTTP_SERVER_VARS['HTTP_REFERER']"); ?>">
Basically this will make php insert the refering url into a hidden form which will be unknowingly submitted to page 3 by the user.
Page 3 will send the email and should look as follows (assuming your code above is correct):
<?php
if ($to_email && $message && $subject) {
$to = "\"$to_name\" <$to_email>";
$from = "\"$from_name\" <$from_email>";
$to = str_replace("\'", "'", $to);
$from = str_replace("\'", "'", $from);
$subject = str_replace("\'", "'", $subject);
$message = str_replace("\'", "'", $message);
$mailed = @mail($to, $subject, $message, "Wrom: HJEXXIMQZUIVOTQNQEMSFDULHPQQWO
if($mailed) {
echo("Thank you for your recommendation.<br><br>Mail message sent : \nTo : $to<br>From : $from<br>Subject : $subject<br>Message : $message");
exit;
} else {
echo("We're sorry but your recommendation was not able to be sent.<br><br>");
}
} else {
echo("You did not enter all the required fields. Please go back.")
}
echo('<a href="' . $referer . '">Continue Shopping</a>');
?>
And that should do it I think.
-dk
__________________
Did I help you? If so, be nice and throw me some TP
Last edited by dk01 : 01-05-2003 at 05:39 PM.
|
|
|
|
01-09-2003, 08:57 AM
|
thankx
|
Posts: 684
Location: Sheffield, England
|
Thankx for that dk.
I'll get it all checked out over the weekend and let you know if there's any bugs with it
Looks okay to me...but then I wouldn't know if it was wrong so...
Sorry it's taken me a while to reply, I've been soo busy recently.
Many thankx 
|
|
|
|
01-11-2003, 05:39 AM
|
|
Posts: 684
Location: Sheffield, England
|
okay...
I've taken the code exactly as you put it.
I uploaded the file + tested it, and got this error :
Parse error: parse error in /home/httpd/vhosts/safariquip.co.uk/httpdocs/recommend.php on line 17
Any ideas / suggestions?
I would guess there is a php coding error somewhere, but I don't know where!! Remind me to learn php...
Thankx for your help 
|
|
|
|
01-11-2003, 06:56 AM
|
|
Posts: 28
Location: New Zealand
|
} else {
echo("You did not enter all the required fields. Please go back.")
}
add a ; at the end of echo(), like so:
} else {
echo("You did not enter all the required fields. Please go back.") ;
}

|
|
|
|
01-11-2003, 07:15 AM
|
Still problems...
|
Posts: 684
Location: Sheffield, England
|
Thankx for that,
I've corrected the syntax error, but still getting the same problem.
I've resorted to the old files here but i've still got the new (and non-functioning file) available here
please have a look at the problem and let me know what it is / what the solution is.
Many thankx 
|
|
|
|
01-11-2003, 09:04 AM
|
|
Posts: 28
Location: New Zealand
|
Can you post up the entire source code of recommend2.php, or make .phps file available please? Thanks. 
|
|
|
|
01-11-2003, 09:52 AM
|
recommend2.php
|
Posts: 684
Location: Sheffield, England
|
attatched is a copy of the source code for the non-functioning recommend page, including the php coding.
many thankx 
|
|
|
|
01-11-2003, 10:00 AM
|
|
Posts: 28
Location: New Zealand
|
Aha! Here's yer cuplrit:
$mailed = @mail($to, $subject, $message, "Wrom: HJEXXIMQZUIVOTQNQEMSFDULHPQQWO
change above to:
$mailed = @mail($to, $subject, $message, $headers);
and create before that a variable like so:
$headers = "From: " . $youremail . "\n"
. "Reply-To: " . $youremail_or_another_you_want_user_to_reply_to . "\n"
. "Content-Type: text/plain" . "\n"
. "Content-Transfer-Encoding: 7bit" . "\n"
. "MIME-Version: 1.0" . "\n"
. "X-Mailer: PHP/" . phpversion();
That should work. 
|
|
|
|
01-11-2003, 10:30 AM
|
|
Posts: 684
Location: Sheffield, England
|
still no joy i'm afraid. I've inputted and changed the data. here's a snippet, as i'm not sure if i've put that new variable in in the correct place :
$message = str_replace("'", "'", $message);
$headers = "From: " . $youremail . "\n"
. "Reply-To: " . $youremail_or_another_you_want_user_to_reply_to . "\n"
. "Content-Type: text/plain" . "\n"
. "Content-Transfer-Encoding: 7bit" . "\n"
. "MIME-Version: 1.0" . "\n"
. "X-Mailer: PHP/" . phpversion();
$mailed = @mail($to, $subject, $message, $headers);
does that seem right to you?
must be getting close to it being finished now...the error message reads :
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/httpd/vhosts/safariquip.co.uk/httpdocs/recommend2.php on line 71
Any ideas? 
|
|
|
|
01-11-2003, 10:36 AM
|
|
Posts: 28
Location: New Zealand
|
You're supposed to change the variable names where they say odd things like $youremail and $youremail_or_another_you_want_user_to_reply_to.  Or you could leave it as it is, but still set values for those vars prior.
I hope I'm still coherent... it's about 4am here.
Edit: actually, let's try this:
change
$headers = "From: " . $youremail . "\n"
. "Reply-To: " . $youremail_or_another_you_want_user_to_reply_to . "\n"
to
$headers = "From: " . $from_email . "\n"
. "Reply-To: " . $from_email . "\n"
Last edited by Abhishek Reddy : 01-11-2003 at 10:40 AM.
|
|
|
|
01-11-2003, 10:44 AM
|
|
Posts: 373
Location: Ames, IA
|
Dave can you post the updated source code because I doubt that line 71 is still the same line as above.
Anyways some problems with your previous code are my fault because I wrote to use a variable called $referer. Well this should be replaced with: $HTTP_SERVER_VARS['HTTP_REFERER']
Also you should change $HTTP_REFERER to $HTTP_SERVER_VARS['HTTP_REFERER'] This is what I beleive is causing the error about T_STRING T_VARIABLE and T_NUM_STRING.
Sorry about that.
-dk
__________________
Did I help you? If so, be nice and throw me some TP
|
|
|
|
01-11-2003, 11:42 AM
|
nothing...
|
Posts: 684
Location: Sheffield, England
|
More changes implimented and still nothing.
attatched is the latest version of recommend2.php.
Still the same error message, which you can view by clicking here
Please view the source and provide me with any advice / changes that you can see are necessary to get the coding to work correctly.
much obliged, and many thankx 
|
|
|
|
01-11-2003, 12:36 PM
|
|
Posts: 28
Location: New Zealand
|
You have a break here:
echo('<a href="' . $HTTP_SERVER_VARS['HTTP_REFERER']
. '">Continue Shopping</a>');
Make that all on one line.
btw, silly question: but are you sure http://www.safariquip.co.uk/recommend2.php has the latest updated code?
|
|
|
|
01-12-2003, 12:17 AM
|
|
Posts: 373
Location: Ames, IA
|
You still havent changed all occurences of $HTTP_REFERER to $HTTP_SERVER_VARS['HTTP_REFERER']
Basically you just need to change this line:
<? if (empty($HTTP_REFERER)) { $referrer = 'No referrer reported'; } else { $referrer = $HTTP_REFERER; } echo $referrer; ?>
to look like this:
<?php if(!($HTTP_SERVER_VARS['HTTP_REFERER'])) { $ref_text = 'No referer reported'; } else { $ref_text = $HTTP_SERVER_VARS['HTTP_REFERER']
; } echo $ref_text; ?>
I am conviced that this what is probably what is causing your error. If you just copy and paste the code above then you should be ok.
Oh btw! I just noticed that all of your links and image sources are in the following format: file:///M|/Data/vivid/pages/contact/map.html
Looks like a problem in dreamweaver or whatever editor you are using. You might want to change that/
-dk
__________________
Did I help you? If so, be nice and throw me some TP
|
|
|
|
|
« Reply to Javascript within PHP
|
|
|
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
|
|
|
|
|
|