Page 1 of 1

Why do I lose variables in this script?

Posted: Sat Jul 31, 2004 1:45 pm
by voltrader
I open this script in a target=_blank page, and pass contact_email, listing_no, and company_name to it in the url. The variables seem to be outputted in html with no problems, but they are lost when "submit" is clicked.

What am I doing wrong here?

Code: Select all

<?php
 


  if ($mode == "submit") { 


    $headers .= "MIME-Version: 1.0 \n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1 \n"; 
    $headers .= "from:$mail_from\r\n$email_from"; 
	
	$mail_subject .= "In regard to your ad: $listing_no";



    if (@mail ($contact_email, $mail_subject, $mail_body, $headers)) { 
      print ("The e-mail was sent successfully");
// debugging -- $contact_email is null!	
print $contact_email." ".$mail_subject." ".$mail_body." ".$headers; 
    } else { 
      print ("an error occurred while sending the e-mail"); 
    } 

    exit; 
  } 
?> 

<html> 

<head> 
<title>Send message</title> 
<script language="javascript"> 
  function DoSubmit () 
  { 

    if (document.form.mail_from.value == "") { 
      alert ("Please enter your e-mail address in 'from' field"); 
      document.form.mail_from.focus (); 
      return ""; 
    } 

    if (document.form.mail_body.value == "") { 
      alert ("Please enter a message in 'body' field"); 
      document.form.mail_body.focus (); 
      return ""; 
    } 

    document.form.submit (); 
  } 
</script> 
</head> 

<body> 
<!-- 
  html starts here
--> 
<form action="<?php print ($PHP_SELF); ?>" method="post" name="form">

  <table> 
    <tr> 
      <td>From:</td> 
      <td><input type="text" name="mail_from" size="40"></td> 
    </tr> 
    <tr> 
      <td>To: </td> 
      <td><? echo $company_name.", ".$contact_email; ?></td> 
    </tr> 

    <tr> 
      <td>Subject: </td> 
      <td><? echo "In regard to your ad: ".$listing_no; ?></td> 
    </tr> 
    <tr> 
      <td valign="top">Body:</td> 
      <td><textarea name="mail_body" cols="40" rows="10"></textarea></td> 
    </tr> 
  
    <tr> 
      <td><input type="hidden" name="mode" value="submit"></td> 
      <td><input type="button" onclick="DoSubmit ()" value="Send e-mail"></td> 
    </tr> 
  </table> 
</form> 

</body> 

</html> 
?>

Posted: Sat Jul 31, 2004 1:53 pm
by ol4pr0

Code: Select all

#if ($mode == "submit") { 
#try
if (isset($_POST['submit'])) {

#if (@mail ($contact_email, $mail_subject, $mail_body, $headers)) { 
#try
if (@mail ($_POST['contact_email'], $_POST['mail_subject'], $_POST['mail_body'], $headers)) {

Still doesn't work :-(

Posted: Sat Jul 31, 2004 2:10 pm
by voltrader
Thanks... I tried using the superglobals, and the modified if(isset($_POST['submit'])) statement, but it still doesn't seem to pick up the variables.

Maybe this will make it clearer...

1) user clicks on link to send message, and I open the above script in a new window while passing these variables in the url:

http://www.test.com/send_message.php?li ... t@test.com

2) I echo the variables in the HTML portion, to make sure that they are passed correctly, and they are:

Code: Select all

<?php
 <td><? echo $company_name.", ".$contact_email; ?></td> 
    </tr> 

    <tr> 
      <td>Subject: </td> 
      <td><? echo "In regard to your ad: ".$listing_no; ?></td> 
?>
3) However, once user is finished composing email and presses the submit button, the variables passed from the previous page are lost. Newly created variables in html page seem to be fine. i.e. mail_body and mail_from.

Posted: Sat Jul 31, 2004 2:13 pm
by feyd
you'll want $_GET['company_name'] among others...

Where do I put the $_GET statements?

Posted: Sat Jul 31, 2004 4:55 pm
by voltrader
This is my new script. I echo contact_email and company_name at the top to make sure that PHP is getting it, however they still seem to disappear after form is submitted. Are my $_GET statements in the right places?

Code: Select all

<?php 
	$contact_email = $_GET['contact_email'];
	$company_name = $_GET['company_name'];
	echo "contact_email ".$contact_email."<br>";
	echo "company_name ".$company_name."<br>";

  if (mode == 'submit') { 


    $headers .= "MIME-Version: 1.0 \n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1 \n"; 
    $headers .= "from:$mail_from\r\n$email_from"; 
	
	$mail_subject = "In regard to your ad: ".$_GET['listing_no'];



    if (@mail ($contact_email, $mail_subject, $mail_body, $headers)) { 
      print ("<h1><font color="#004000">The e-mail was sent successfully!</font></h1>");
	  print $contact_email." ".$mail_subject." ".$mail_body." ".$headers."<br>"; 
    } else { 
      print ("<h1><font color="#880000">An error occurred while sending the e-mail!</font></h1>"); 
    } 



    exit; 
  } 
?> 

<html> 

<head> 
<title>Send message</title> 
<script language="javascript"> 
  function DoSubmit () 
  { 


    if (document.form.mail_from.value == "") { 
      alert ("You forgot to enter the 'from' field."); 
      document.form.mail_from.focus (); 
      return ""; 
    } 

    if (document.form.mail_body.value == "") { 
      alert ("You forgot to enter the 'body' field."); 
      document.form.mail_body.focus (); 
      return ""; 
    } 

    document.form.submit (); 
  } 
</script> 
</head> 

<body> 
<!-- 
  html starts here
--> 
<form action="<?php print ($PHP_SELF); ?>" method="post" name="form">

  <table> 
    <tr> 
      <td>From:</td> 
      <td><input type="text" name="mail_from" size="40"></td> 
    </tr> 
    <tr> 
      <td>To: </td> 
      <td><? echo $company_name.", ".$contact_email; ?></td> 
    </tr> 

    <tr> 
      <td>Subject: </td> 
      <td><? echo "In regard to your ad: ".$listing_no; ?></td> 
    </tr> 
    <tr> 
      <td valign="top">Body:</td> 
      <td><textarea name="mail_body" cols="40" rows="10"></textarea></td> 
    </tr> 
  
    <tr> 
      <td><input type="hidden" name="mode" value="submit"></td> 
      <td><input type="button" onclick="DoSubmit ()" value="Send e-mail"></td> 
    </tr> 
  </table> 
</form> 

</body> 

</html> <?

Posted: Sat Jul 31, 2004 5:19 pm
by feyd
you're not passing them on through that form.

Posted: Sat Jul 31, 2004 5:23 pm
by voltrader
Ah, thanks feyd!

Does that mean I should add something like this to the form?

Code: Select all

<?php
<input type="hidden" name=company_name value="<? $_GET['company_name'] ?>">
?>

Posted: Sat Jul 31, 2004 5:36 pm
by feyd
basically, yes.

Posted: Sat Jul 31, 2004 6:08 pm
by voltrader
I added these right after the form statement:

Code: Select all

<?php
<input type="hidden" name=company_name value="<? $_GET['company_name'] ?>">
<input type="hidden" name=contact_email value="<? $_GET['contact_email'] ?>">
<input type="hidden" name=listing_no value="<? $_GET['listing_no'] ?>"> 
?>
When I looked at the source of the html, the values all appeared null:

Code: Select all

<?php
<input type="hidden" name=company_name value="">
<input type="hidden" name=contact_email value="">
<input type="hidden" name=listing_no value=""> 
?>
Is my syntax wrong?

Posted: Sat Jul 31, 2004 6:20 pm
by feyd
<? $_GET['listing_no'] ?>
although legal, this does absolutely nothing..

Code: Select all

<?php echo $_GET['listing_no'] ?>

or

<?= $_GET['listing_no'] ?>
should echo the data.