Jump to content

PHP form script, send only if required fields are filled in?


aafordy

Recommended Posts

Hi, any help with this really clear things up...

 

I have the following php in use an it works OK but want it to have required fields like 'name', 'number' and 'email address' does anyone have any tips to amend it so it only sends the form if those fields are completed?

 

Thanks, script below:

 

$to = "[email protected]";

$subject = "contact enquiry";

$email = $_REQUEST['emailaddress'] ;

$message = "First name: ".$_REQUEST['firstname'] ;

$message .= "Surname: ".$_REQUEST['secondname'] ;

$message .= "Phone number: ".$_REQUEST['phonenumber'] ;

$message .= "Address: ".$_REQUEST['address'] ;

$message .= "Address2: ".$_REQUEST['address2'] ;

$message .= "Address3: ".$_REQUEST['address3'] ;

$message .= "Address4: ".$_REQUEST['address4'] ;

$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

if($sent)

{print ""; }

else

{print "We encountered an error sending your mail"; }

 

 

 

 

The strlen function gives you the length of the given string, you can then just compare it to any value, so if you want to make sure it's not an empty string just use some code like this :

 

if( strlen($_REQUEST['firstname']) > 0) {
    $message = "First name: ".$_REQUEST['firstname'] ; 
} else {
    echo "Please fill in your first name";
}

 

I hope this helps you along  ;)

<?php
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$number = trim($_POST['number']);

if((strlen($name) <= 0)||(strlen($email)<=0)||(strlen($number<=0)){

   // error

}


//.... validate the name email and number for format..etc
?>

Thanks for the help, I'm a bit of a beginner at this so I've put the script in again as follows and it now gives an error? Can anyone help use out as to where I should make the changes?

 

Thanks  :)

 

<?

 

$to = "[email protected]";

$subject = "emailheading";

$email = $_REQUEST['emailaddress'] ;

$message = "First name: ".$_REQUEST['firstname'] ;

$message .= "Surname: ".$_REQUEST['secondname'] ;

$message .= "Phone number: ".$_REQUEST['phonenumber'] ;

$message .= "Address: ".$_REQUEST['address'] ;

$message .= "Address2: ".$_REQUEST['address2'] ;

$message .= "Address3: ".$_REQUEST['address3'] ;

$message .= "Address4: ".$_REQUEST['address4'] ;

$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

 

 

if((strlen($emailaddress) <= 0)||(strlen($firstname)<=0)||(strlen($phonenumber<=0)){

 

  // error

 

}

 

//.... validate the name email and number for format..etc

 

?>

 

little mistake by me sorry

 

try

<?

$to = "[email protected]"; 
$subject = "emailheading"; 
$email = $_REQUEST['emailaddress'] ; 
$message = "First name: ".$_REQUEST['firstname'] ; 
$message .= "Surname: ".$_REQUEST['secondname'] ; 
$message .= "Phone number: ".$_REQUEST['phonenumber'] ; 
$message .= "Address: ".$_REQUEST['address'] ;
$message .= "Address2: ".$_REQUEST['address2'] ;
$message .= "Address3: ".$_REQUEST['address3'] ;
$message .= "Address4: ".$_REQUEST['address4'] ;
$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;
$headers = "From: $email"; 
$sent = mail($to, $subject, $message, $headers) ; 


if((strlen($email) <= 0)||(strlen($firstname)<=0)||(strlen($phonenumber)<=0){

   // error

}

//.... validate the name email and number for format..etc

?>

Hi Sensei,

 

I've now got the following script in there and its giving me an error on the second if line the if((strlen...

so it looks like the following:

 

<?

 

$to = "[email protected]";

$subject = "emailheading";

$email = $_REQUEST['emailaddress'] ;

$message = "First name: ".$_REQUEST['firstname'] ;

$message .= "Surname: ".$_REQUEST['secondname'] ;

$message .= "Phone number: ".$_REQUEST['phonenumber'] ;

$message .= "Address: ".$_REQUEST['address'] ;

$message .= "Address2: ".$_REQUEST['address2'] ;

$message .= "Address3: ".$_REQUEST['address3'] ;

$message .= "Address4: ".$_REQUEST['address4'] ;

$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

if($sent)

{print ""; }

else

{print "We encountered an error sending your mail"; }

 

 

if((strlen($email) <= 0)||(strlen($firstname)<=0)||(strlen($phonenumber)<=0){

 

  // error

 

}

 

//.... validate the name email and number for format..etc

 

?>

 

Any ideas where I'm going wrong?

 

Thanks,

I suggest using JS.

Here is what I use:

<script type='text/javascript'>
function checkFields()
{
    if (document.getElementById('fname').value == '')
    {
        alert('Please enter your First name!');
        return false;
    }<s/cript>

Thanks, heres the error:

 

Parse error: syntax error, unexpected '{' in /websites/123reg/LinuxPackage21/fo/rd/ys/fordysfolio.co.uk/public_html/contactpage.php on line 150

 

would i put the javascript in the header of my html file or within the div where I have my php?

 

 

Hi Sensei,

 

I've gone back to your original advice and amending the PHP, please see my form at www.fordysfolio.co.uk/contact.html the following php I have sitting in my contactpage.php

 

<?

 

$to = "[email protected]";

$subject = "one2one enquiry";

$email = $_REQUEST['emailaddress'] ;

$message = "First name: ".$_REQUEST['firstname'] ;

$message .= "Surname: ".$_REQUEST['secondname'] ;

$message .= "Phone number: ".$_REQUEST['phonenumber'] ;

$message .= "Address: ".$_REQUEST['address'] ;

$message .= "Address2: ".$_REQUEST['address2'] ;

$message .= "Address3: ".$_REQUEST['address3'] ;

$message .= "Address4: ".$_REQUEST['address4'] ;

$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

if($sent)

{print ""; }

else

{print "We encountered an error sending your mail"; }

 

$name = trim($_POST['name']);

$email = trim($_POST['email']);

$number = trim($_POST['number']);

 

if((strlen($email) <= 0)||(strlen($firstname)<=0)||(strlen($phonenumber)<=0){

 

  // error

 

}

 

//.... validate the name email and number for format..etc

 

?>

 

when going through now it gives the following error:

 

Parse error: syntax error, unexpected '{' in /websites/123reg/LinuxPackage21/fo/rd/ys/fordysfolio.co.uk/public_html/contactpage.php on line 155

 

Line 155 is the bold line in the PHP, do you know what's going wrong?

 

Thanks

Hi, here's the full html for the form contactpage.php my php sits in the same div as where my form would of:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

  <head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <META name="description" content="One 2 One Business Solutions Tel: 0140 028 3259 Mob: 0772 599 6645 email: [email protected]"/>

<META name="keywords" content="business solutions, profit, help, bussiness, help with my business, business help, thelma yates, derbyshire chamber, designer, anthony ford, manchester."/>

 

<!--[if lte IE 6]>

        <link rel="stylesheet" type="text/css" href="/css/ie6fixes.css" />

<![endif]-->

<!--[if IE 7]>

        <link rel="stylesheet" type="text/css" href="/css/ie7fixes.css" />

<![endif]-->

<title>thank you</title>

  <script type="text/javascript" src="file:///Mac HD/Users/macbookpro/Desktop/sdmenu-2/sdmenu/sdmenu.js">

 

  <script type='text/javascript'>

function checkFields()

{

    if (document.getElementById('fname').value == '')

    {

        alert('Please enter your First name!');

        return false;

    };

// ]]>

</script>

  <link href="sdmenu/sdmenu.css" rel="stylesheet" type="text/css" />

  <style type="text/css">

<!--

@import url("../one2one.css/one2one.css");

a:link {

text-decoration: none;

color: #333;

}

a:visited {

text-decoration: none;

color: #333;

}

a:hover {

text-decoration: none;

color: #999;

}

a:active {

text-decoration: none;

}

body {

background-image: url(../Picture-3.gif);

}

-->

  </style>

  </head>

  <body>

 

  <link href="file:///Mac HD/Users/macbookpro/Desktop/one2one.css/one2one.css" rel="stylesheet" type="text/css" />

<style type="text/css">

<!--

a:link {

text-decoration: none;

color: #333;

}

a:visited {

text-decoration: none;

color: #666;

}

a:hover {

text-decoration: none;

color: #FFF;

}

a:active {

text-decoration: none;

}

body {

background-image: url(../Picture-3.gif);

}

-->

</style>

<script src="file:///Mac HD/Users/macbookpro/Desktop/Scripts/swfobject_modified.js" type="text/javascript"></script>

</head>

 

<body>

<div class="headercont">

 

<div class="logo">

  <object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="340" height="125">

    <param name="movie" value="../one2onelogo.swf" />

    <param name="quality" value="high" />

    <param name="wmode" value="opaque" />

    <param name="swfversion" value="6.0.65.0" />

    <!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. -->

    <param name="expressinstall" value="file:///Mac HD/Users/macbookpro/Desktop/Scripts/expressInstall.swf" />

    <!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->

    <!--[if !IE]>-->

    <object type="application/x-shockwave-flash" data="../one2onelogo.swf" width="340" height="125">

      <!--<![endif]-->

      <param name="quality" value="high" />

      <param name="wmode" value="opaque" />

      <param name="swfversion" value="6.0.65.0" />

      <param name="expressinstall" value="file:///Mac HD/Users/macbookpro/Desktop/Scripts/expressInstall.swf" />

      <!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->

      <div>

        <h4>Content on this page requires a newer version of Adobe Flash Player.</h4>

        <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p>

      </div>

      <!--[if !IE]>-->

    </object>

    <!--<![endif]-->

  </object>

</div>

<div class="otherlogos"><img src="../logos.gif" width="234" height="98"/></div>

</div>

 

 

 

 

<div class="nav-menu">

  <ul>

<li><a href="index.html">Home</a></li>

<li><a href="mission.html">Mission Statement</a></li>

<li><a href="services.html">Services</a></li>

<li><a href="testimonials.html">Testimonials</a></li>

<li><a href="about.html">About us</a></li>

<li><a href="contact.html">Contact</a></li>

</ul>

</div>

 

<div class="container"><img src="../thanksheader.jpg" width="873" height="150" />

  <div class="copyleft">

 

 

 

<?php

 

$to = "[email protected]";

$subject = "one2one enquiry";

$email = $_REQUEST['emailaddress'] ;

$message = "First name: ".$_REQUEST['firstname'] ;

$message .= "Surname: ".$_REQUEST['secondname'] ;

$message .= "Phone number: ".$_REQUEST['phonenumber'] ;

$message .= "Address: ".$_REQUEST['address'] ;

$message .= "Address2: ".$_REQUEST['address2'] ;

$message .= "Address3: ".$_REQUEST['address3'] ;

$message .= "Address4: ".$_REQUEST['address4'] ;

$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

if($sent)

{print ""; }

else

{print "We encountered an error sending your mail"; }

 

$name = trim($_POST['name']);

$email = trim($_POST['email']);

$number = trim($_POST['number']);

 

if((strlen($email) <= 0)||(strlen($firstname)<=0)||(strlen($phonenumber)<=0){

 

  // error

 

}

 

//.... validate the name email and number for format..etc

 

?>

 

  </div>

  </div>

 

<div class="footer">

  <div class="footernav"> <a href="index.html">Home</a> | <a href="mission.html">Mission Statement</a> | <a href="services.html">Services</a> | <a href="testimonials.html">Testimonials</a> | <a href="about.html">About us</a> | <a href="contact.html"> Contact</a></div>

  <div class="footercopy">

    <h3>Fully Flexible Service: no NI, no Sick Pay, no Holiday, no Agency Fee</h3>

    <p>  </p>

    <h4>Copyright © 2009 One 2 One Business Solutions. All rights reserved.</h4>

  </div>

</div>

<script type="text/javascript">

<!--

swfobject.registerObject("FlashID");

//-->

</script>

 

 

 

 

 

  </body>

</html>

 

As you can see on the contact page when submitting Its giving an error?

 

Thanks,

Here try this

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <META name="description" content="One 2 One Business Solutions Tel: 0140 028 3259 Mob: 0772 599 6645 email: [email protected]"/>
<META name="keywords" content="business solutions, profit, help, bussiness, help with my business, business help, thelma yates, derbyshire chamber, designer, anthony ford, manchester."/>

<!--[if lte IE 6]>
        <link rel="stylesheet" type="text/css" href="/css/ie6fixes.css" />
<![endif]-->
<!--[if IE 7]>
        <link rel="stylesheet" type="text/css" href="/css/ie7fixes.css" />
<![endif]-->
   <title>thank you</title>
  <script type="text/javascript" src="file:///Mac HD/Users/macbookpro/Desktop/sdmenu-2/sdmenu/sdmenu.js">
   
     <script type='text/javascript'>
function checkFields()
{
    if (document.getElementById('fname').value == '')
    {
        alert('Please enter your First name!');
        return false;
    };
   // ]]>
   </script>
  <link href="sdmenu/sdmenu.css" rel="stylesheet" type="text/css" />
  <style type="text/css">
<!--
@import url("../one2one.css/one2one.css");
a:link {
   text-decoration: none;
   color: #333;
}
a:visited {
   text-decoration: none;
   color: #333;
}
a:hover {
   text-decoration: none;
   color: #999;
}
a:active {
   text-decoration: none;
}
body {
   background-image: url(../Picture-3.gif);
}
-->
  </style>
  </head>
  <body>

  <link href="file:///Mac HD/Users/macbookpro/Desktop/one2one.css/one2one.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
   text-decoration: none;
   color: #333;
}
a:visited {
   text-decoration: none;
   color: #666;
}
a:hover {
   text-decoration: none;
   color: #FFF;
}
a:active {
   text-decoration: none;
}
body {
   background-image: url(../Picture-3.gif);
}
-->
</style>
<script src="file:///Mac HD/Users/macbookpro/Desktop/Scripts/swfobject_modified.js" type="text/javascript"></script>
</head>

<body>
<div class="headercont">

<div class="logo">
  <object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="340" height="125">
    <param name="movie" value="../one2onelogo.swf" />
    <param name="quality" value="high" />
    <param name="wmode" value="opaque" />
    <param name="swfversion" value="6.0.65.0" />
    <!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. -->
    <param name="expressinstall" value="file:///Mac HD/Users/macbookpro/Desktop/Scripts/expressInstall.swf" />
    <!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->
    <!--[if !IE]>-->
    <object type="application/x-shockwave-flash" data="../one2onelogo.swf" width="340" height="125">
      <!--<![endif]-->
      <param name="quality" value="high" />
      <param name="wmode" value="opaque" />
      <param name="swfversion" value="6.0.65.0" />
      <param name="expressinstall" value="file:///Mac HD/Users/macbookpro/Desktop/Scripts/expressInstall.swf" />
      <!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->
      <div>
        <h4>Content on this page requires a newer version of Adobe Flash Player.</h4>
        <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p>
      </div>
      <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
  </object>
</div>
<div class="otherlogos"><img src="../logos.gif" width="234" height="98"/></div>
</div>




<div class="nav-menu">
  <ul>
<li><a href="index.html">Home</a></li>
<li><a href="mission.html">Mission Statement</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="testimonials.html">Testimonials</a></li>
<li><a href="about.html">About us</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>

<div class="container"><img src="../thanksheader.jpg" width="873" height="150" />
  <div class="copyleft">


   
<?php

$to = "[email protected]";
$subject = "one2one enquiry";
$email = $_REQUEST['emailaddress'] ;
$message = "First name: ".$_REQUEST['firstname'] ;
$message .= "Surname: ".$_REQUEST['secondname'] ;
$message .= "Phone number: ".$_REQUEST['phonenumber'] ;
$message .= "Address: ".$_REQUEST['address'] ;
$message .= "Address2: ".$_REQUEST['address2'] ;
$message .= "Address3: ".$_REQUEST['address3'] ;
$message .= "Address4: ".$_REQUEST['address4'] ;
$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print ""; }
else
{print "We encountered an error sending your mail"; }

$name = trim($_POST['name']);
$email = trim($_POST['email']);
$number = trim($_POST['number']);

if((strlen($name) <= 0)||(strlen($email)<=0)||(strlen($number)<0)){

   // error

}

//.... validate the name email and number for format..etc

?>
   
  </div>
  </div>

<div class="footer">
  <div class="footernav"> <a href="index.html">Home</a> | <a href="mission.html">Mission Statement</a> | <a href="services.html">Services</a> | <a href="testimonials.html">Testimonials</a> | <a href="about.html">About us</a> | <a href="contact.html"> Contact</a></div>
  <div class="footercopy">
    <h3>Fully Flexible Service: no NI, no Sick Pay, no Holiday, no Agency Fee</h3>
    <p>  </p>
    <h4>Copyright © 2009 One 2 One Business Solutions. All rights reserved.</h4>
  </div>
</div>
<script type="text/javascript">
<!--
swfobject.registerObject("FlashID");
//-->
</script>




   
  </body>
</html>

Any chance you could shed some light on where I would do that? I'm new to php?

 

We have given you a good start. Trust me do some research my friend, it is far better you try first, fail, then try and fix your mistake. This way you will learn better...

 

Plan it out in your head, on a piece of paper, on anything on what to do next. Then take action, do research and put it to action...

 

then do even more research on why its not working, not by asking us to do it. :)

 

To help you start...

 

http://www.php-mysql-tutorial.com/wikis/php-tutorial/form-validation-using-php.aspx

http://www.w3schools.com/PHP/php_mail.asp

 

and mostly

 

http://www.phpfreaks.com/tutorial/php-security

:) Thanks for your help, I guess I'll have to try and figure it out somehow, for record I've changed a few things and it still works as norm?

 

<?php

 

$to = "[email protected]";

$subject = "one2one enquiry";

$email = $_REQUEST['emailaddress'] ;

$message = "First name: ".$_REQUEST['firstname'] ;

$message .= "Surname: ".$_REQUEST['secondname'] ;

$message .= "Phone number: ".$_REQUEST['phonenumber'] ;

$message .= "Address: ".$_REQUEST['address'] ;

$message .= "Address2: ".$_REQUEST['address2'] ;

$message .= "Address3: ".$_REQUEST['address3'] ;

$message .= "Address4: ".$_REQUEST['address4'] ;

$message .= "Nature of enquiry: ".$_REQUEST['enquiry'] ;

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

if($sent)

{print ""; }

else

{print "We encountered an error sending your mail"; }

 

$name = trim($_POST['firstname']);

$email = trim($_POST['emailaddress']);

$number = trim($_POST['phonenumber']);

 

if((strlen($name) <= 0)||(strlen($email)<=0)||(strlen($number)<0)){

 

  // error

 

}

 

//.... validate the name email and number for format..etc

 

?>

 

You might want to run this part of the script after validation

 

$sent = mail($to, $subject, $message, $headers) ;

 

So after

 

 if((strlen($name) <= 0)||(strlen($email)<=0)||(strlen($number)<0)){

   // error

}

 

 

Do some validation individually on each variable, refer to the links above given to you. After everything is done, send the email, test if it works, and give the user the message.

Thanks for the help, :D found this site while trying to figure out how it works http://myphpform.com/php-form-tutorial.php there is a good tutorial on there that gives step by step instructions to get the basic function working, with the php...

 

Just have to put it all together along with the existing html file and customize the thanks page 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.