Why do I lose variables in this script?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Why do I lose variables in this script?

Post 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> 
?>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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)) {
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Still doesn't work :-(

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll want $_GET['company_name'] among others...
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Where do I put the $_GET statements?

Post 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> <?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're not passing them on through that form.
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Post 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'] ?>">
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basically, yes.
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply