Jump to content

Passing variable to mail()


waverider

Recommended Posts

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
[quote]$item = $_REQUEST["description"];[/quote] 
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:
[quote] <input type="hidden" name="description"  value="$description">[/quote]

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:
[quote]<form method="post" action="http://www.mywebsite.com/mowers_query_mail.php?description=$description">[/quote]

but this doesn't appear to work either.

Any thoughts?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/14810-passing-variable-to-mail/
Share on other sites

[quote author=willfitch link=topic=100835.msg398395#msg398395 date=1153108285]
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.
[/quote]

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?
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:

[quote]<?php
$to      = "[email protected]";
$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/thankyou.htm" );
?>
[/quote]
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.
The problem is with this line:
[code]<input type="hidden" name="description"  value="$description">[/code]
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 [b][color=red]$description[/color][/b].

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

Ken

[quote]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; ?>">[/quote]

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 :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.