Jump to content

PHP code for mailing Contact Us page's filled form details not working


Recommended Posts

Dear friends,

I am newbie to this site and to php too. I dabble here and there trying to assemble pieces of codes from here and there for developing my site. I have been using a template for my website. However when I am trying to use php file (processmyform.php) to get the data filled in the form on the contact page (contact.html) by mailing it to me, i always get the predefined error message on submitting the form irrespective of the fact whether all the fields are filled or not. I am attaching the javascript file (javascriptcontent.js) too for you to have a complete. If anyone can help me with this problem I will be really grateful. Here is the code:

 

1. contact.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK HREF="style.css" TYPE="text/css" REL="stylesheet">
<script type="text/javascript" src="javascriptcontent.js"></script>
<style type="text/css">
.Black {    color: #000;
}
</style>
</HEAD>
<BODY>

<table  cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td  style="width:766px; height:780px">
        <table  cellspacing="0" cellpadding="0">
          <tr>
            <td  style="width:42px; height:780px"></td>
            <td  style="width:676px; height:780px">
                <table  cellspacing="0" cellpadding="0">
                  <tr>
                    <td  style="width:676px; height:119px; background-image: url(images/grey_bg.gif); background-repeat:no-repeat">
                        <table  cellspacing="0" cellpadding="0">
                          <tr>
                            <td  style="width:560px; height:90px"><span style="width:116px; height:119px"><a href="index-4.html"><a href="index-4.html"><img src="images/ipj logo.jpg" width="58" height="96" hspace="10" vspace="15"></a><img src="images/c_name.jpg" alt="" width="299" height="124" hspace="10"></td>

                          </tr>
                        </table>
                    </td>
                  </tr>
                   <tr>
                    <td  style="width:676px; height:563px">
                        <table  cellspacing="0" cellpadding="0">
                          <tr>
                            <td  style="width:226px; height:563px">
                                <a href="index.html"><img src="images/back.gif" alt="" border="0"></a><img src="images/pic5_1.gif" alt=""><br>
                                <img src="images/pic5_2.gif" alt=""><br>
                                <a href="index-1.html"><img src="images/home.jpg" alt="" border="0"></a><a href="index-2.html"><img src="images/We.jpg" alt="" border="0"></a><br>
                            <a href="index-3.html"><img src="images/service.jpg" alt="" border="0"></a><a href="index-5.html"><img src="images/contact.jpg" alt="" border="0"></a><br> </td>
                            <td  style="width:450px; height:563px; background-color:#F8F8F8; background-image:url(images/top.gif); background-repeat:no-repeat">
                                <table  cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td  style="width:450px; height:563px; background-image:url(images/bottom.gif); background-repeat:no-repeat; background-position: left bottom" class="list">
                                        <p><br style="line-height:30px">
                                        <img src="images/title5_1.gif" alt="" style="margin-left:29px"><br>
                                        <br style="line-height:9px">
                                        <img src="images/pic5_3.gif" alt="" align="left" style="margin-left:30px; margin-top:2px; margin-right:14px"><br><strong>xyz</strong><br>
A z <br>
                                        <br style="line-height:12px">
                                        Freephone:  +00   000 000 0000<br>
                                        Telephone:  +00 000 000 0000<br>
                                        <br>
                                        <br style="line-height:26px"> 
                                        <img src="images/px2_1.gif" alt="" style="margin-left:29px"><br>
                                        <br style="line-height:25px">
                                        <img src="images/title5_2.gif" alt="" style="margin-left:28px"><br>
                                        </p>
                                        <div style="margin-left:30px">
                                            <br style="line-height:12px">
                                            Praesent vestibu lum molestie lacus. Aenean nonummy hendrerit mauris.<br>
                                            <br style="line-height:9px">
                                            <form action="processmyform.php" enctype="multipart/form-data" id="contactus" name="contactus" onSubmit="return checkcontactus()">
                                            <table  cellspacing="0" cellpadding="0" class="form">
                                              <tr>
                                                <td  style="width:198px; height:128px">
                                                    <table  cellspacing="0" cellpadding="0">
                                                      <tr>
                                                        <td  style="width:198px; height:27px"><input name="name" type="text" value="Name:" onFocus=this.value=''/></td>
                                                      </tr>
                                                       <tr>
                                                        <td  style="width:198px; height:27px"><input name="company" type="text" value="Company:" onFocus=this.value=''/></td>
                                                      </tr>
                                                       <tr>
                                                        <td  style="width:198px; height:27px"><input name="telephone" type="text" value="Telephone:" onFocus=this.value=''/></td>
                                                      </tr>
                                                       <tr>
                                                        <td  style="width:198px; height:27px"><input name="email" type="text" value="E-mail:" onFocus=this.value=''/></td>
                                                      </tr>
                                                    </table>
                                                </td>
                                                <td  style="width:202px; height:128px">
                                                    <table  cellspacing="0" cellpadding="0">
                                                      <tr>
                                                        <td  style="width:202px; height:109px"><textarea name="message" cols="0" rows="0" onFocus=this.value=''>Message:</textarea></td>
                                                      </tr>
                                                    
                                                    </table>
                                                </td>
                                              </tr>
                                            </table>
                                            </form>
    <br style="line-height:1px">
                                                        <img src="images/spacer.gif" alt="" width="116" height="1"><strong><a href="#" onClick="document.getElementById('contactus').reset()">reset</a></strong><img src="images/spacer.gif" alt="" width="15" height="1"><strong><a href="#" onClick="document.getElementById('contactus').submit()">send</a></strong>                                    </div>
                                        <br style="line-height:14px"> 
                                        <img src="images/px2_1.gif" alt="" style="margin-left:29px"><br style="line-height:24px">
                                        <br>
                                        <div style="margin-left:30px">
                                          <div align="justify"><span class="Black">"There are four ways, and only four ways, in which we <strong>contact</strong> with the world. We are evaluated and classified by these four: what we do, how we look, what we say, and how we say it."</span><br>
                                            <img src="images/spacer.gif" alt="" width="270" height="1"><span>- Dale Carnegie</span><br style="line-height:11px">
                                          </div>
                                          <table  cellspacing="0" cellpadding="0">
                                            <tr>
                                                
                                            </tr>
                                          </table>
                                      </div>
                                    
                                    </td>
                                  </tr>
                                </table>
                            </td>
                          </tr>
                        </table>
                    </td>    
                  </tr>
                   <tr>
                    <td  style="width:676px; height:98px" class="policy"><div align="justify">
                      <p align="center"> </p>
                      <p align="center"><span class="policy" style="width:676px; height:25px">P | g | <span class="policy" style="width:676px; height:25px">Og </span>P |  e <br>
                        xyz.Com © 2012 <img src="images/img.gif" alt=""> <a href="index-6.html">Privacy Policy</a></span></p>
                    </div></td>
                  </tr>
                </table>
            </td>
            <td  style="width:48px; height:780px"></td>
          </tr>
        </table>
    </td>
  </tr>
</table>
</BODY>
</HTML>

 

2. processmyform.php

<?php
$errors=0;
$error="The following errors occured while processing your form input.<ul>";
$name= array_key_exists('name', $_POST) ? $_POST['name'] : null;
$company= array_key_exists('company', $_POST) ? $_POST['company'] : null;
$telephone=array_key_exists('telephone', $_POST) ? $_POST['telephone'] : null;
$email=array_key_exists('email', $_POST) ? $_POST['email'] : null;
$message=array_key_exists('message', $_POST) ? $_POST['message'] : null;

if($name=="" || $company=="" || $telephone=="" || $email=="" || $message=="" ){
$errors=1;
$error.="You did not complete one or more fields, please go back and try again!";
}
if($errors==1) echo $error;
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Name: ".$name."
Company: ".$company."
Telephone: ".$telephone."
E-mail: ".$email."
Message: ".$message."
";
$message = stripslashes($message);
mail("xyz@xyz.com","Form Submitted at your website",$message,"From: xyz.com");
header('Location: http://www.xyz.com/thanks.html');
}
?>

 

3. javascriptcontent.js

function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function IsEmptyField(TextObj){
        return IsEmptyStr(TextObj.value)
    }
     
    function IsEmptyStr(Str){
        var retval = true;
        for (var i=0; i < Str.length; ++i){    
            var c = Str.charAt(i);
            if (c != ' ' && c != '\t') 
                retval = false;
        }    
        return retval;
    }
function checkcontactus() {
        if (IsEmptyField(document.contactus.name)){
            alert ("Please Enter Your Full Name");
            return false;
        }
        if (IsEmptyField(document.contactus.company)){
            alert ("Please Enter Your Company Name");
            return false;
        }
        if (IsEmptyField(document.contactus.telephone)){
            alert ("Please Enter Your Telephone Number");
            return false;
        } 
        if (IsEmptyField(document.contactus.email)){
            alert ("Please Enter Your Email Address");
            return false;
        } 
        if(document.contactus.email.value.indexOf("\@")==-1 || document.contactus.email.value.indexOf(".")==-1){
            alert ("Please Input a Correct Email Address")
            return false;
        } 
        if (IsEmptyField(document.contactus.message)){
            alert ("Please Enter Your Message To Send");
            return false;
        } 
        return true;
    }

 

4. thanks.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK HREF="style.css" TYPE="text/css" REL="stylesheet">
<script type="text/javascript" src="javascriptcontent.js"></script>
</HEAD>
<BODY onload="MM_preloadImages('images/h_butt1.gif','images/2h_butt2.gif','images/3h_butt3.gif')">

<table  cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td  style="width:766px; height:780px">
        <table  cellspacing="0" cellpadding="0">
          <tr>
            <td  style="width:42px; height:780px"></td>
            <td  style="width:676px; height:780px">
                <table  cellspacing="0" cellpadding="0">
                  <tr>
                    <td  style="width:676px; height:119px; background-image: url(images/grey_bg.gif); background-repeat:no-repeat">
                        <table  cellspacing="0" cellpadding="0">
                          <tr>
                            <td  style="width:560px; height:90px"><span style="width:116px; height:119px"><a href="index-4.html"><a href="index-4.html"><img src="images/ipj logo.jpg" width="58" height="96" hspace="10" vspace="15"></a><img src="images/c_name.jpg" alt="" width="299" height="124" hspace="10"></td>
                          </tr>
                        </table>
                    </td>
                  </tr>
                   <tr>
                    <td  style="width:676px; height:563px">
                        <table  cellspacing="0" cellpadding="0">
                          <tr>
                            <td  style="width:226px; height:563px">
                                <a href="index.html"><img src="images/back.gif" alt="" border="0"></a><img src="images/pic1_1.gif" alt=""><br>
                                <img src="images/pic1_2.gif" alt=""><br>
                                <a href="index-1.html"><img src="images/home.jpg" alt="" border="0"></a><a href="index-2.html"><img src="images/We.jpg" alt="" border="0"></a><br>
                            <a href="index-3.html"><img src="images/service.jpg" alt="" border="0"></a><a href="index-5.html"><img src="images/contact.jpg" alt="" border="0"></a><br> </td>
                            <td  style="width:450px; height:563px; background-color:#F8F8F8; background-image:url(images/top.gif); background-repeat:no-repeat">
                                <table  cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td  style="width:450px; height:563px; background-image:url(images/bottom.gif); background-repeat:no-repeat; background-position: left bottom">
                                        <br style="line-height:31px">
                                        <br>
                                        <div style="margin-left:30px">
<p align="center"><span verdana?,?sans-serif?;="" mso-fareast-font-family:="" roman?;="" mso-bidi-font-family:="" ?times="" new="" roman??=""><strong><u>Contact Us - Complete!</u></strong></span></p>
<p align="center"><span verdana?,?sans-serif?;="" mso-fareast-font-family:="" roman?;="" mso-bidi-font-family:="" ?times="" new="" roman??="">We have received your information. We will revert back to you with a response as soon as possible. </span></p>
<span verdana?,?sans-serif?;="" mso-fareast-font-family:="" roman?;="" mso-bidi-font-family:="" ?times="" new="" roman??=""></span><span verdana?,?sans-serif?;="" mso-fareast-font-family:="" roman?;="" mso-bidi-font-family:="" ?times="" new="" roman??="">                                </span><br>
                                        </div>
                                        
                                    
                                    </td>
                                  </tr>
                                </table>
                            </td>
                          </tr>
                        </table>
                    </td>    
                  </tr>
                   <tr>
                    <td  style="width:676px; height:98px" class="policy"><div align="justify">
                      <p align="center"> </p>
                      <p align="center"><span class="policy" style="width:676px; height:25px">Patent Search | Patent Filing | Patent Drafting | Patent Outsourcing | <span class="policy" style="width:676px; height:25px">Offshoring </span>Patent Services |  Patent Attorney | Trademark Search | Trademark Filing |   Trademark Services Outsourcing | Trademark Attorney | India US Europe <br>
                        IPJuvares.Com © 2012 <img src="images/img.gif" alt=""> <a href="index-6.html">Privacy Policy</a></span></p>
                    </div></td>
                  </tr>
                </table>
            </td>
            <td  style="width:48px; height:780px"></td>
          </tr>
        </table>
    </td>
  </tr>
</table>
</BODY>
</HTML>

instead of doing $variable=='' do something like

empty($variable)

 

 

also one thing about array_key_exists it will return TRUE if the value is NULL while isset will return FALSE if the value is NULL.

 

 

so you need something like this in your form

 

$variable = isset($_POST['variable']) ? $_POST['variable'] : '';

I tried the changes you suggested Darkfreaks. However, still I am getting the error message, irrespective of whether I have filled the form fields or not. Here is the modified code I used:

<?php
$errors=0;
$error="The following errors occured while processing your form input.<ul>";
$name= isset($_POST['name']) ? $_POST['name'] : '';
$company= isset($_POST['company']) ? $_POST['company'] : '';
$telephone=isset($_POST['telephone']) ? $_POST['telephone'] : '';
$email=isset($_POST['email']) ? $_POST['email'] : '';
$message=isset($_POST['message']) ? $_POST['message'] : '';

if(empty($name) || empty($company) || empty($telephone) || empty($email) || empty($message)){
$errors=1;
$error.="You did not complete one or more fields, please go back and try again!";
}
if($errors==1) echo $error;
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Name: ".$name."
Company: ".$company."
Telephone: ".$telephone."
E-mail: ".$email."
Message: ".$message."
";
$message = stripslashes($message);
mail("xyz@xyz.com","Form Submitted at your website",$message,"From: xyz.com");
header('Refresh: 0; http://www.xyz.com/thanks.html');
}
?>

 $errors= array();

if(empty($name) || empty($company) || empty($telephone) || empty($email) || empty($message)){
echo $errors[] = "You did not complete one or more fields, please go back and try again!";
}

 

Try adding brackets to your IF statement.

if($errors==1){ echo $error;
}else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Name: ".$name."
Company: ".$company."
Telephone: ".$telephone."
E-mail: ".$email."
Message: ".$message."
";
$message = stripslashes($message);
mail("xyz@xyz.com","Form Submitted at your website",$message,"From: xyz.com");
header('Refresh: 0; http://www.xyz.com/thanks.html');
}

Also the variable $errors don't need to be changed to an array, but could be done that way as darkfreaks suggested, but then you would need to use a foreach statement to display it to the user.  Also you don't usually echo an array variable while setting the value.

Dear Drummin,

 

I used the brackets as you suggested, both with my original code and with modifications suggested by Darkfreak (the first set of changes, not the array one). I am still getting the error message irrespective of a filled or unfilled form fields. Looking forward to more help, I am totally new to this.

 

Thanks a lot for taking time out to help. Really appreciate that.

OK well sure...  you're setting these variables to empty values then checking to see if they are empty.

 

Try wrapping the whole thing in an isset() statement.

 

EDIT:  Use one of your field names that's always posted if you don't have a "submit" named button.

<?php
if (isset($_POST['name'])){
$errors=0;
$error="The following errors occured while processing your form input.";
$name= isset($_POST['name']) ? $_POST['name'] : '';
$company= isset($_POST['company']) ? $_POST['company'] : '';
$telephone=isset($_POST['telephone']) ? $_POST['telephone'] : '';
$email=isset($_POST['email']) ? $_POST['email'] : '';
$message=isset($_POST['message']) ? $_POST['message'] : '';

if(empty($name) || empty($company) || empty($telephone) || empty($email) || empty($message)){
$errors=1;
$error.="You did not complete one or more fields, please go back and try again!";
}
if($errors==1) echo $error;
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Name: ".$name."
Company: ".$company."
Telephone: ".$telephone."
E-mail: ".$email."
Message: ".$message."
";
$message = stripslashes($message);
mail("xyz@xyz.com","Form Submitted at your website",$message,"From: xyz.com");
header('Refresh: 0; http://www.xyz.com/thanks.html');
}
}
?>

Dear Drummin,

 

I used the code posted by you. However, now nothing is appearing on the next page even if none of the values in the form are filled, error message is not appearing. I am sorry to give you a hard time but can you suggest what could have gone wrong?

 

 

also one thing about array_key_exists it will return TRUE if the value is NULL while isset will return FALSE if the value is NULL.

 

 

so you need something like this in your form

 

$variable = isset($_POST['variable']) ? $_POST['variable'] : '';

 

Form elements send string values by default, so there will never be a NULL. An empty string sent by a form will still cause isset() to return TRUE, rendering the ternary redundant because all it will ever do is assign the value from the $_POST var to the variable, regardless of what's in it. isset() is more suited for form elements that may or may not be present when the form is submitted, such as checkboxes.

 

----------------------------------------------------------------

 

$HTTP_SERVER_VARS is deprecated in favor of the $_SERVER superglobal array.

 

$SERVER_NAME is probably undefined

 

$PHP_SELF is probably undefined

 

You should be developing with error reporting set up to display all possible errors. Make sure the following directives are set in your php.ini file, then restart Apache. Post any errors that are generated.

 

error_reporting = -1

display_errors = On

 

The way you're currently validating form fields will drive most users off in no time flat. For the sake of user-friendliness, you should return the user to the form, specifying which fields are incorrect, and pre-filling the values previously entered in the fields. Otherwise, the user may have to start over and fill in even the fields that were correct again, and most won't bother.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.