An important problem I can't fix

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
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

An important problem I can't fix

Post by Nicole »

Hi, I have a strange problem and don't know how to fix it, but it is very important to get it fixed, because the form won't send without it. For some reason the form won't send unless the form field "requesteremail" has data in it and I don't know why. So I thought if I just put an error command if the field was empty everything would work anyway, because people would be prompted to fill in the field. But when I add the "if empty" statement, for some reason it is also ignored, unless something is in the "requesteremail" field. I don't get it, it makes no sense to me, but I really need it fixed. Hope someone will help. Thank you very much.

Code: Select all

while ($row = mysql_fetch_array($result)){ 
if ($_POST['websitename']==""){?> 

<form method="post" action="<?php $_SERVER['PHP_SELF'] ;?>"> 
<input type="hidden" value="<?php echo $row[websitename];?>" name="websitename"> 
<input type="text" name="requesteremail" size=20> 
<input type="text" name="page" size=20> 
<input type="submit" name="submit" value="Submit" /></form> 
<?php 
} 
else{ 
$mailto = 'emailaddress@something.com' ; 
$subject = "Subject of email" ; 
$websitename = $_POST['websitename'] ; 
$requesteremail = $_POST['requesteremail'] ; 
$page = $_POST['page'] ; 
$uself = 0; 
$sep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ; 
$messageproper = "$websitename\n" . "$requesteremail\n" . "$page\n"; 
mail($mailto, $subject, $messageproper, "From: \"$requesteremail\" <$requesteremail>" . $sep . "Reply-To: \"$requesteremail\" <$requesteremail>" . $sep); 
if (empty($requesteremail) || empty($requesterwebsite)) {echo "field empty please try again";} 
else {echo "email sent!";} 
}}}//these 3 brackets needed from other mysql variables earlier. 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the mail() call will likely fail if it is empty as the headers will have invalid syntax in them.

Code: Select all

From: "" <>
Reply-To: "" <>
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

I switched the headers to this, same problem, didn't help. As suggested here http://us3.php.net/mail

$headers .= 'From: MyName<'.$requesteremail.'>'.$eol;
$headers .= 'Reply-To: MyName<'.$requesteremail.'>'.$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
mail($mailto, $subject, $messageproper,$headers);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Again, if the variable is blank, it's going to generate invalid headers.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

Any idea feyd why a simple if empty statement won't work on the requesteremail?

Thanks a lot neel I'll try it. I'm a bit confused about the post and key though. How do I encorporate my requesteremail in the following script. Should I replace all these....$key_name with this? $requesteremail. Should I change this $key_value, to this?
$requesteremail_value . I believe this should be added to the end of my script.

Code: Select all

if
($_POST['websitename']=="")
{ 
  foreach($_POST as $key_name => $key_value) 
    { 
      if(empty($key_value)) 
        { 
          $cnt_post++; 
          $hld_error .= $key_name."\t\t\\n"; 
        } 
    } 
  if($cnt_post > 0) 
        { 
          echo "<script language=\"JavaScript\" type=\"text/javascript\">"."alert(\"You Have Forgotten To Enter Your ".$hld_error."\");"."</script>"; 
        } 
} 
if($cnt_post >= 0) { 
echo 'please enter in email';}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The code posted previously had the empty() checks after the mail was sent.

I would highly suggest you use a mailing library such as Swift.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

First of all
=======this is not required at all. Just remove it and Try

Code: Select all

$headers .= "X-Mailer: PHP v".phpversion().$eol;
=======
Use

Code: Select all

if($_POST)
NOT

Code: Select all

if
($_POST['websitename']=="")
It is not required

Code: Select all

if($cnt_post >= 0) { 
echo 'please enter in email';}
Cause

Code: Select all

if($_POST)
{
  foreach($_POST as $key_name => $key_value)
    {
      if(empty($key_value))
        {
          $cnt_post++;
          $hld_error .= $key_name."\\n";
        }
    }
  if($cnt_post > 0)
        {
          echo "<script language=\"JavaScript\"   type=\"text/javascript\">"."alert(\"Please complete the following fields:\\n\\n ".$hld_error."\");"."</script>";
        }
}
It Checkes all the empty fields in any field is blank it will pop an alert
If You This It Would make each and every fields mandetary

EDITED
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

I've Made Some Changes To That Code as that was not so friendly and here is the new code
==========================================================

Code: Select all

<?php
$email_to = "neel.basu.z@gmail.com"; // Write Your email address here
$redirect_after_success_location = "http://www.YourSiteName.com/redir.php";
// declare values
$contact_email = $_POST['EmailAddress'];
$contact_subject = $_POST['Subject'];
$contact_name = $_POST['FullName'];
$contact_company = $_POST['Company'];
$contact_address = $_POST['Address'];
$contact_tel = $_POST['Tel'];
$contact_inquiry = $_POST['Inquiry'];
$contact_comment = $_POST['Comment'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );

// check for validation, then send the e-mail
$cnt_post = 0;
$hld_error = "  ";
if($_POST)
{
  foreach($_POST as $key_name => $key_value)
    {
      if(empty($key_value))
        {
          $cnt_post++;
          $hld_error .= $key_name."\\n";
        }
    }
  if($cnt_post > 0)
        {
          echo "<script language=\"JavaScript\"   type=\"text/javascript\">"."alert(\"Please complete the following fields:\\n\\n ".$hld_error."\");"."</script>";
        }
}
if($cnt_post > 0 || empty($contact_email)) {
echo '<form method="post" action="">
<p><span style="font-size: 10.0pt; font-family: Verdana">If you have any inquiry 
regarding our product details, quotation,product<br>
availability,delivery time or 
you just want to leave a message please fill<br>
the below form and sendit to us. 
We promise prompt attention.<br>
We will get back to you as soon as possible.</span></p>
<table id="Form-Details" width="429">
<tbody>
<tr><td width="62"><font face="Tahoma"   color="#800080"><b>Name:</b></font></td><td width="357">
<input type="text" name="FullName" value="'.$_POST[FullName].'"  size="57"  style="border-style: solid; border-width: 1px" /></td></tr>
<tr><td width="62"><font face="Tahoma"   color="#800080"><b>Subject:</b></font></td><td width="357" >
<input type="text" name="Subject" value="'.$_POST[Subject].'"  size="57"  style="border-style: solid; border-width: 1px" /></td></tr>
<tr><td width="62"><font face="Tahoma"   color="#800080"><b>Email:</b></font></td><td width="357" >
<input type="text" name="EmailAddress"  value="'.$_POST[EmailAddress].'"  size="57" style="border-style: solid;  border-width: 1px" /></td></tr>
<tr><td width="62"><font face="Tahoma"   color="#800080"><b>Company:</b></font></td><td width="357">
<input type="text" name="Company" value="'.$_POST[Company].'"  size="57"  style="border-style: solid; border-width: 1px" /></td></tr>
<tr><td width="62" valign="top"><font face="Tahoma"   color="#800080"><b>Address:</b></font></td><td width="357">
<textarea rows="3" name="Address" cols="43" style="border-style:  solid;  border-width: 1px">'.$_POST['Address'].'</textarea></td></tr>
<tr><td width="62"><font face="Tahoma"   color="#800080"><b>Tel.:</b></font></td><td width="357">
<input type="text" name="Tel" value="'.$_POST[Tel].'" size="57"   style="border-style: solid; border-width: 1px" /></td></tr>
<tr><td width="62"><font face="Tahoma"   color="#800080"><b>Inquiry:</b></font></td><td width="357">
<input type="text" name="Inquiry" value="'.$_POST[Inquiry].'"  size="57"  style="border-style: solid; border-width: 1px" /></td></tr>
<tr><td width="62" valign="top"><font face="Tahoma"   color="#800080"><b>Comments:</b></font></td><td width="357">
<font face="MS Sans Serif">
<textarea rows="5" name="Comment" cols="43" class="input"   style="border-style: solid; border-width:  1px">'.$_POST['Comment'].'</textarea></font></td></tr>
<tr><td width="81">&nbsp;</td><td width="405">
<input type="submit" value="Submit" style="font-family: Tahoma;   font-weight: bold; float:left" /><font face="Tahoma"><input  type="reset"  value="Reset" style="font-family: Tahoma; font-weight:  bold; float:left"  /></font></td></tr>
</tbody>
</table>
</form>';
}   elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",   $contact_email)) {
echo "<p>Please enter a valid e-mail address.</p>";
} else {
// where to send e-mail to
$to = $email_to;
// e-mail subject
$subject = "Inquiry from website.";
// e-mail message
$headers = "From: $contact_name <$contact_email>\n"."Reply-To: $contact_email\n";
$message = "You have received an Inquiry:\r\n"
."----------------------------------------------------------------\r\n"
."Contact Name: $contact_name\r\n"
."Subject: $contact_subject\r\n"
."Company: $contact_company\r\n"
."Address: $contact_address\r\n"
."Tel: $contact_tel\r\n"
."Inquiry: $contact_inquiry\r\n"
."Comments: $contact_comment\r\n"
."Submitted: $mydate\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n\r\n"
."Form Address: {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";

mail( $to, $subject, $message, $headers );
header("location:$redirect_after_success_location");
}
?>
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

Thanks Feyd, I really hate switching to a whole new email system, there must be a way to make a few changes to regular php mail regarding this. But thanks for the tip.

Neel, I don't suppose you have anything that just checks to see if some of the fields are empty, because I have several fields that will be empty. Thank you very much.
Last edited by Nicole on Sun Jan 14, 2007 12:55 am, edited 1 time in total.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Then Replace This
if($cnt_post > 0 || empty($contact_email))
with
if(empty($field_name) || empty($field_name) || empty($field_name) || empty($field_name))
Here $field_name reffers to the madentory field variable
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

I thought your key values were what checked the fields, I still need those don't I?
Post Reply