Jump to content


Photo

Passing variable to mail()


  • Please log in to reply
7 replies to this topic

#1 waverider

waverider
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 July 2006 - 03:46 AM

Hello,
I am trying to pass a variable from a PHP page containing an HTML form, using a hidden field, to another PHP page which then sends an Email comtaining the data passed. All other fields are passed OK,  retrieved using the structure

$item = $_REQUEST["description"];

 
and sent successfully in the Email.

However the hidden field doesn't pass the value of the variable successfully.

The hidden field structure is as below:

<input type="hidden" name="description"  value="$description">


The variable $description exists, but of course what is passed to the next page is '$description' rather than it's value.  How do I pass the value of the variable rather than it's name to the next page?

I have tried  passing it in the url through:

<form method="post" action="http://www.mywebsite...=$description">


but this doesn't appear to work either.

Any thoughts?

Thanks

#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 17 July 2006 - 03:51 AM

My first guess would to first ensure that the description hidden field is within the <form> </form> tags.  If it is, try using $_POST['description'] rather than REQUEST.  It's safer.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 waverider

waverider
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 July 2006 - 03:58 AM

My first guess would to first ensure that the description hidden field is within the <form> </form> tags.  If it is, try using $_POST['description'] rather than REQUEST.  It's safer.


Thanks for the reply
Yes it is within the form tags. 

Thanks for the suggestion re: $_POST and security, however making this change won't resolve the problem of passing the variable value will it?

#4 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 17 July 2006 - 04:03 AM

It could.

$_REQUEST works the same way as register_globals.  Remember the letters GPCS. 
1. GET
2. POST
3. COOKIE
4. SESSION
That is the order PHP will look for that variable.  Post some of the code you use to handle the form.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#5 waverider

waverider
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 July 2006 - 04:34 AM

Thanks again for the help,
Code I am using to retrieve and send the Email is as follows ( I have left out all the error checking and security stuff as I have tried everything with this commented out and still get the same result) - I have also changed to POST as you suggested and everything but the one variable works with that:

<?php
$to      = "my@emailaddress.com";
$subject = $_POST["realname5"];
$item = $_POST["description"];
$body = $_POST["postal5"];
$email = $_POST["email"];
$name = $_POST["realname5"];
$phone = $_POST["phone5"];

$headers = "From: $email";
mail($to, $subject, "Name:  $name\nEmail:  $email\nTelephone:  $phone\nItem of Interest:  $item\nMessage:  $body", $headers);
header( "Location: http://www.mywebsite...e/thankyou.htm" );
?>



#6 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 17 July 2006 - 04:37 AM

Ok.

If everything else is being received, then you need to consider looking at the simple things.  Go through the following steps:

1. Ensure the hidden field "description" is within the <form> tags </form>
2. Ensure the spelling of the "description" field is spelled correctly.
    (i.e. <input type="hidden" name="description"> and $_POST['description'] match)
3. Do a var_dump($_POST) to see everything that is being sent.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 July 2006 - 04:55 AM

The problem is with this line:
<input type="hidden" name="description"  value="$description">
As far as I can tell from this one line taken out of context, it is a pure HTML line and the value of the field will be literaly $description.

If you want to pass the value of the variable $description, then use this line instead:
<input type="hidden" name="description"  value="<?php echo $description; ?>">

Ken



#8 waverider

waverider
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 July 2006 - 05:18 AM

If you want to pass the value of the variable $description, then use this line instead:
Code:

<input type="hidden" name="description"  value="<?php echo $description; ?>">


Thanks Ken - that's exactly what I wanted - I thought that's where the problem was but wasn't sure how to resolve it - fixed now :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users