PHP Coding Help

ultramel1987

Well-Known Member
Joined
Dec 31, 2010
Messages
193
Reaction score
0
Hi there

I have the following html form which I am trying to send using the php mail function, but when open the page, it automatically displays the text that the message could not be sent?:cry:
Then a blank email is sent to me from "unknown recipient"

Please can someone take a look and tell me where my mistake may be?
(I have changed my email address to display as "[email protected]" for the sake of the forum)

Code:
<?php
  $email = $_REQUEST['email'] ;
  $confName = $_REQUEST['conferenceName'] ;
  $companyName = $_REQUEST['companyName'] ;
  mail("[email protected]", $confName,
  $companyName, "From:" . $email);
  $retval = mail ($email,$confName,$companyName);
if( $retval == true )  
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }
?>
<form method="post" id="quote" action='?page_id=2229'>
<script type="text/javascript">// <![CDATA[
jQuery('#quote').keyup(function (){   doTotal(this);   calcMenu(this); });
// ]]></script>
<table id="table1" border="0" cellspacing="3" cellpadding="3">
<tbody>
<tr>
<td>Enquiry Date</td>
<td>
<div align="center"><input type="text" name="dateToday" size="25" /></div></td>
</tr>
<tr>
<td>Conference Name</td>
<td>
<div align="center"><input type="text" name="conferenceName" size="25" /></div></td>
</tr>
<tr>
<td>Company Name</td>
<td>
<div align="center"><input type="text" name="companyName" size="25" /></div></td>
</tr>
<tr>
<td>Special Requests</td>
<td><textarea name="comment" rows="5" cols="26"></textarea></td>
</tr>
</tbody>
</table>
<table id="table2" border="0" cellspacing="3" cellpadding="3">
<tbody>
<tr>
<td>First Name</td>
<td>
<div align="center"><input type="text" name="firstName" size="25" /></div></td>
</tr>
<tr>
<td>Last Name</td>
<td>
<div align="center"><input type="text" name="lastName" size="25" /></div></td>
</tr>
<tr>
<td>Tel No</td>
<td>
<div align="center"><input type="text" name="telNo" size="25" /></div></td>
</tr>
<tr>
<td>Cell</td>
<td>
<div align="center"><input type="text" name="cellNo" size="25" /></div></td>
</tr>
<tr>
<td>Email</td>
<td>
<div align="center"><input type="text" name="email" size="25" /></div></td>
</tr>
<tr>
<td><input onclick="formReset()" type="button" value="Reset form" /></td>
</tr>
<tr>
<td><input type='submit'></td>
</tr>
</tbody>
</table>
<table id="tablex" border="1" cellspacing="3" cellpadding="3">
<tbody>
<tr>
<th scope="col" width="30">
<div align="center">Date</div></th>
<th scope="col" width="128">
<div align="center">Amount of Delegates ½ Day Conference @ R 240 pp</div></th>
<th width="112">
<div align="center">Amount of Delegates Full Day Conference @ R 260 pp</div></th>
<th width="112">
<div align="center">Menu No</div></th>
<th width="112">
<div align="center">Price pp for Menu (1-7: R70, 8-10 R85, 11: R105, 12: R85)</div></th>
<th width="112">
<div align="center">Total Persons for meals</div></th>
<th width="112">
<div align="center">Amount of Single Rooms @ R 480 pp</div></th>
<th width="112">
<div align="center">Amount of Double Rooms @ R 720 pp</div></th>
<th width="134">
<div align="center">Total for the day</div></th>
</tr>
<tr>
<td>
<div align="center"><input type="text" name="date1" size="10" /></div></td>
<td>
<div align="center"><input type="text" name="halfday1" size="5" maxlength="10" /></div></td>
<td>
<div align="center"><input type="text" name="fullday1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuNo1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuPrice1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MealPersons1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="SingleRooms1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="DoubleRooms1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="total1" size="5" /></div></td>
</tr>
<tr>
<td>
<div align="center"><input type="text" name="date2" size="10" /></div></td>
<td>
<div align="center"><input type="text" name="halfday2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="fullday2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuNo2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuPrice2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MealPersons2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="SingleRooms2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="DoubleRooms2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="total2" size="5" /></div></td>
</tr>
<tr>
<td>
<div align="center"><input type="text" name="date3" size="10" /></div></td>
<td>
<div align="center"><input type="text" name="halfday3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="fullday3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuNo3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuPrice3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MealPersons3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="SingleRooms3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="DoubleRooms3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="total3" size="5" /></div></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</form>
 
If I'm not mistaken (not a php boffin) you are calling the mail function twice. Is that correct?
 
As far as I know I'm not.. but not a php boffin either :-)
Where in my code do you think I'm calling it twice?

Thank you for your reply
 
Last edited:
mail("[email protected]", $confName, $companyName, "From:" . $email);
$retval = mail ($email,$confName,$companyName);

Looks like it. The first one sends a mail to you from what ever $email is (is there not supposed to be a space between the From: and the address?)

The second one does not have any from address/header set so maybe that is why $retval is false.
 
Last edited:
ok I think we are getting somewhere. After changing the code to this, the message now says "message sent successfully" when the page loads. But it still sends a blank email when page loads as well?

<?php
$email = $_REQUEST['email'] ;
$confName = $_REQUEST['conferenceName'] ;
$companyName = $_REQUEST['companyName'] ;
$retval = mail("[email protected]", $confName,
$companyName, "From:" . $email);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
 
What's the value of $companyName? And is the subject set?
 
$companyName is an imput text box on the form, and the subject, is $confName (which also an input box on the form)
 
Wait a second. When you open this page the first thing that fires is your function at the top so no values are set in the request.

I would change it to either check if a certain value is set in the request (maybe try <input type="hidden" name="shouldsend" value="true"/>) or move the send logic to a different page (and point the form's post value to that page).

That's what it looks like it is doing anyway.
 
$companyName is an imput text box on the form, and the subject, is $confName (which also an input box on the form)

Yeah, I meant the actual value which is most likely blank on account of no request values being set (see above post). What happens when you fill out details and click mail/send/submit?
 
Last edited:
Thank you. That makes sense.... when I fill it out and submit, then the mail sends perfectly, and then also displays the message that it sent successfully...
 
I don't really need the 'retval' if function, the only reason I added it was to try and stop the form from sending a blank mail upon load...
 
Found a nice example here:
Code:
<?php
if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";
    echo "<br>You can use the following form again to enter a new name.";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="text" name="name"><br>
   <input type="submit" name="submit" value="Submit Form"><br>
</form>
 
Found a nice example here:
Code:
<?php
if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";
    echo "<br>You can use the following form again to enter a new name.";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="text" name="name"><br>
   <input type="submit" name="submit" value="Submit Form"><br>
</form>

^ This

You need to check if the form on the page has been submitted before you try and do whatever you want to do (insert to the database, mail someone etc). When the page is then submitted to itself, the function will fire because the check "if page is submitted (getting variables) run function" then is true and will do whatever you want.

Alternatively you could put the mailing function in a separate page, and then submit to that page instead. That way the mailing function will never fire unless submitted.
 
Hi there

Thank you sooooo much. On 'submit', the form is now emailing the various contents to the necessary recipient :-)

Now, once the form is submitted - it displays the page 'processed' (this processed page is named:?page_id=2601.php) - but there is no data displaying on the page?

How would I get the same "filled in" fields to display on the 'processed' pagr that are sent via email?

Thank you all so much :-)

<?php
if(isset($_POST['submit'])) {
$to = '[email protected]' ; //put your email address on which you want to receive the information
$subject = 'Conference Quote Request'; //set the subject of email.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "<table><tr><td>Enquiry Date</td><td>".$_POST['dateToday']."</td></tr>
<tr><td>Conference Name</td><td>".$_POST['conferenceName']."</td></tr>
<tr><td>Company Name</td><td>".$_POST['companyName']."</td></tr>
<tr><td>First Name</td><td>".$_POST['firstName']."</td></tr>
<tr><td>Last Name</td><td>".$_POST['lastName']."</td></tr>
<tr><td>Tel No</td><td>".$_POST['telNo']."</td></tr>
<tr><td>E-Mail</td><td>".$_POST['cellNo']."</td></tr>
<tr><td>E-Mail</td><td>".$_POST['email']."</td></tr>
<tr><td>Special Requests</td><td>".$_POST['comment']."</td></tr>
</tr></table>" ;
echo "Message sent successfully...";
mail($to, $subject, $message, $headers);
header('Location: ?page_id=2601.php');
}
?>


I have tried using this, but only the text displays on the page, and not the 'firstName' field


HTML:
    echo "Welcome ". $_POST['firstName']. "<br />";
 
Last edited:
You could use an "if-else" statement. If the submit var is not set, then provide the form, else the form is submitted, and we mail and show the submitted content.

Some like:
Code:
<?php
if(isset($_POST['submit'])) {
$to = '[email protected]' ; //put your email address on which you want to receive the information
$subject = 'Conference Quote Request'; //set the subject of email.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "<table><tr><td>Enquiry Date</td><td>".$_POST['dateToday']."</td></tr>
<tr><td>Conference Name</td><td>".$_POST['conferenceName']."</td></tr>
<tr><td>Company Name</td><td>".$_POST['companyName']."</td></tr>
<tr><td>First Name</td><td>".$_POST['firstName']."</td></tr>
<tr><td>Last Name</td><td>".$_POST['lastName']."</td></tr>
<tr><td>Tel No</td><td>".$_POST['telNo']."</td></tr>
<tr><td>E-Mail</td><td>".$_POST['cellNo']."</td></tr>
<tr><td>E-Mail</td><td>".$_POST['email']."</td></tr>
<tr><td>Special Requests</td><td>".$_POST['comment']."</td></tr>
</tr></table>" ;
mail($to, $subject, $message, $headers);
echo "Message sent successfully...";
echo "$message";
} else {
?> 
<form method="post" id="quote" action='?page_id=2229'>
<script type="text/javascript">// <![CDATA[
jQuery('#quote').keyup(function (){   doTotal(this);   calcMenu(this); });
// ]]></script>
<table id="table1" border="0" cellspacing="3" cellpadding="3">
<tbody>
<tr>
<td>Enquiry Date</td>
<td>
<div align="center"><input type="text" name="dateToday" size="25" /></div></td>
</tr>
<tr>
<td>Conference Name</td>
<td>
<div align="center"><input type="text" name="conferenceName" size="25" /></div></td>
</tr>
<tr>
<td>Company Name</td>
<td>
<div align="center"><input type="text" name="companyName" size="25" /></div></td>
</tr>
<tr>
<td>Special Requests</td>
<td><textarea name="comment" rows="5" cols="26"></textarea></td>
</tr>
</tbody>
</table>
<table id="table2" border="0" cellspacing="3" cellpadding="3">
<tbody>
<tr>
<td>First Name</td>
<td>
<div align="center"><input type="text" name="firstName" size="25" /></div></td>
</tr>
<tr>
<td>Last Name</td>
<td>
<div align="center"><input type="text" name="lastName" size="25" /></div></td>
</tr>
<tr>
<td>Tel No</td>
<td>
<div align="center"><input type="text" name="telNo" size="25" /></div></td>
</tr>
<tr>
<td>Cell</td>
<td>
<div align="center"><input type="text" name="cellNo" size="25" /></div></td>
</tr>
<tr>
<td>Email</td>
<td>
<div align="center"><input type="text" name="email" size="25" /></div></td>
</tr>
<tr>
<td><input onclick="formReset()" type="button" value="Reset form" /></td>
</tr>
<tr>
<td><input type='submit'></td>
</tr>
</tbody>
</table>
<table id="tablex" border="1" cellspacing="3" cellpadding="3">
<tbody>
<tr>
<th scope="col" width="30">
<div align="center">Date</div></th>
<th scope="col" width="128">
<div align="center">Amount of Delegates ½ Day Conference @ R 240 pp</div></th>
<th width="112">
<div align="center">Amount of Delegates Full Day Conference @ R 260 pp</div></th>
<th width="112">
<div align="center">Menu No</div></th>
<th width="112">
<div align="center">Price pp for Menu (1-7: R70, 8-10 R85, 11: R105, 12: R85)</div></th>
<th width="112">
<div align="center">Total Persons for meals</div></th>
<th width="112">
<div align="center">Amount of Single Rooms @ R 480 pp</div></th>
<th width="112">
<div align="center">Amount of Double Rooms @ R 720 pp</div></th>
<th width="134">
<div align="center">Total for the day</div></th>
</tr>
<tr>
<td>
<div align="center"><input type="text" name="date1" size="10" /></div></td>
<td>
<div align="center"><input type="text" name="halfday1" size="5" maxlength="10" /></div></td>
<td>
<div align="center"><input type="text" name="fullday1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuNo1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuPrice1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MealPersons1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="SingleRooms1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="DoubleRooms1" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="total1" size="5" /></div></td>
</tr>
<tr>
<td>
<div align="center"><input type="text" name="date2" size="10" /></div></td>
<td>
<div align="center"><input type="text" name="halfday2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="fullday2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuNo2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuPrice2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MealPersons2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="SingleRooms2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="DoubleRooms2" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="total2" size="5" /></div></td>
</tr>
<tr>
<td>
<div align="center"><input type="text" name="date3" size="10" /></div></td>
<td>
<div align="center"><input type="text" name="halfday3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="fullday3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuNo3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MenuPrice3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="MealPersons3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="SingleRooms3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="DoubleRooms3" size="5" /></div></td>
<td>
<div align="center"><input type="text" name="total3" size="5" /></div></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<?php
}
?>
 
or he could just default the "value=" of each input to the post variable, but that will confuse the user and they'll submit more than once because they don't get a proper message.

To be honest, this is programming 101. I'd recommend reading a few simple tutorials online first instead of asking. If you really do need to ask, I don't mind really, I'm happy to teach (and as you can see a lot of people are happy to contribute) but there's a huge learning curve you're missing by side-stepping it and getting people to do it for you. Just saying
 
or he could just default the "value=" of each input to the post variable, but that will confuse the user and they'll submit more than once because they don't get a proper message.

To be honest, this is programming 101. I'd recommend reading a few simple tutorials online first instead of asking. If you really do need to ask, I don't mind really, I'm happy to teach (and as you can see a lot of people are happy to contribute) but there's a huge learning curve you're missing by side-stepping it and getting people to do it for you. Just saying

Pot calling the kettle black.
 
Top
Sign up to the MyBroadband newsletter
X