Php form Validation Help Please

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
avario
Forum Newbie
Posts: 3
Joined: Sun Oct 11, 2009 9:56 pm

Php form Validation Help Please

Post by avario »

I am making a form that needs to validated to make sure that all of the fields have been filled in. I would like messages to appear next to any fields that have not been filled in when the user tries to submit the form. Once the form has been filled in correctly it is meant to email the information to me. But my code does not work, the page just shows up as blank white...

Here is my code. Don't really do much php and am very new at it (not surprised it doesn't work). Any help to get this working would be greatly appreciated. Thanks

Code: Select all

<?php
 
//Create variables for form data
$submit = $_POST['submit'];
$name = strip_tags($_POST['name']);
$lname = strip_tags($_POST['lname']);
$email = strip_tags($_POST['email']);
$city = strip_tags($_POST['city']);
$postcode = strip_tags($_POST['postcode']);
$type = strip_tags($_POST['type']);
$workname = strip_tags($_POST['workname']);
$agree = strip_tags($_POST['agree']);
$errors= 0;
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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="Arun Books is a print to demand publisher which encourages new writers to publish their work." />
<meta name="keywords" content="arun, books, publishing, authors, new writers, new zealand" />
<title>Arun Books</title>
<style type="text/css">
<!--
body {
    background-image: url(Pictures/Graphics/Main-Background.jpg);
    background-repeat: repeat-x;
    background-color: #0e66c8;
}
-->
</style>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
    font-family: Georgia, Times New Roman, Times, serif;
}
a:link {
    color: #009;
    text-decoration: none;
}
a:visited {
    color: #009;
    text-decoration: none;
}
a:hover {
    color: #009;
    text-decoration: underline;
}
a:active {
    text-decoration: none;
    color: #009;
}
-->
</style>
<script src="Scripts/swfobject_modified.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
  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];}}
}
//-->
</script>
</head>
 
<body>
<a name="top" id="top"></a>
<div id="wrapper">
  <div id="banner"></div>
  <div id="contentFooterBG">
    <div id="content">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="23%" height="630" valign="middle"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="180" height="630" class="navBar" id="FlashID">
            <param name="movie" value="Flash/ArunBooks Menu Bar.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="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="Flash/ArunBooks Menu Bar.swf" width="180" height="630">
              <!--<![endif]-->
              <param name="quality" value="high" />
              <param name="wmode" value="opaque" />
              <param name="swfversion" value="6.0.65.0" />
              <param name="expressinstall" value="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></td>
          <td width="77%" rowspan="2" class="heading" style="padding-right:10px"><p>Get Your Work Published</p>
          <p class="bodyText">We are pleased you want to  get you're work published. This could be the beginning of a long relationship with literature and Arun Books. Please fill in the form below about your personal details and details about the work you want us to publish. Please fill in all fields.</p>
          <form id="form1" name="form1" method="post" action="publish.php">
            <table width="100%" border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td width="37%" align="right" class="bodyTextNoAlign"><label for="name" class="bodyTextNoAlign"> First Name:
                  
                </label></td>
                <td width="63%"><div align="left" class="bodyText">
                  <input name="name" type="text" id="name" size="40" maxlength="40" />
                
                *<?php
                
                if ($submit){
                        if (!$name){
                            echo '<style="color:#C00">Please enter a your name.</style>';
                            $errors = 1;
                
                ?>
                
                </div></td>
              </tr>
              <tr>
                <td align="right" class="bodyTextNoAlign"><label for="lname" class="bodyTextNoAlign">
                  Last Name:
                </label></td>
                <td class="bodyText"><div align="left">
                  <input name="lname" type="text" id="lname" size="40" maxlength="40" />
                  *
                
                <?php
                
                if ($submit){
                        if (!$lname){
                            echo '<style="color:#C00">Please enter a your last name.</style>';
                            $errors = 1;
                
                ?>
                
                </div></td>
              </tr>
              <tr>
                <td class="bodyTextNoAlign"><div align="right">
                  <label for="email">Email Address:</label>
                </div></td>
                <td class="bodyText"><div align="left">
                  <input name="email" type="text" id="email" size="40" maxlength="40" />
                
                *<?php
                
                if ($submit){
                        if (!$email){
                            echo '<style="color:#C00">Please enter a your e-mail.</style>';
                            $errors = 1;
                
                ?>
                
                </div></td>
              </tr>
              <tr>
                <td class="bodyTextNoAlign"><div align="right">
                  <label for="address">Address:</label>
                </div></td>
                <td class="bodyText"><div align="left">
                  <input name="address" type="text" id="address" size="40" maxlength="40" />
                
                *<?php
                
                if ($submit){
                        if (!$address){
                            echo '<style="color:#C00">Please enter a your address.</style>';
                            $errors = 1;
                
                ?>
                
                </div></td>
              </tr>
              <tr>
                <td class="bodyTextNoAlign"><div align="right">
                  <label for="city">City:</label>
                </div></td>
                <td class="bodyText"><div align="left">
                  <input name="city" type="text" id="city" size="40" maxlength="40" />
                
                *<?php
                
                if ($submit){
                        if (!$city){
                            echo '<style="color:#C00">Please enter a your city.</style>';
                            $errors = 1;
                
                ?>
                
                </div></td>
              </tr>
              <tr>
                <td class="bodyTextNoAlign"><div align="right">
                  <label for="postcode">Post Code:</label>
                </div></td>
                <td class="bodyText"><div align="left">
                  <input name="postcode" type="text" id="postcode" size="10" maxlength="10" />
                </div></td>
              </tr>
              <tr>
                <td class="bodyTextNoAlign"><div align="right">
                  <label for="type">Type of Work:</label>
                </div></td>
                <td class="bodyText"><div align="left">
                  <select name="type" id="type">
                    <option value="Novel">Novel</option>
                    <option value="Non-fiction">Non-fiction</option>
                    <option value="Children's Book">Children's Book</option>
                    <option value="Photographic">Photographic</option>
                    <option value="Poetry">Poetry</option>
                    <option value="Other">Other</option>
                  </select>
                  *
                
                <?php
                
                if ($submit){
                        if (!$type){
                            echo '<style="color:#C00">Please enter the type of work you want Arun Books to publish.</style>';
                            $errors = 1;
                
                ?>
                
                </div></td>
              </tr>
              <tr>
                <td align="right" class="bodyTextNoAlign"><label for="workname">
                  Name of Work:
                  </label></td>
                <td><div align="left"><span class="bodyTextNoAlign">
                  <input name="workname" type="text" id="workname" size="40" maxlength="40" />
                  *
                </span>
                
                <?php
                
                if ($submit){
                        if (!$workname){
                            echo '<style="color:#C00">Please enter the name of your work you wnat to publish.</style>';
                            $errors = 1;
                
                ?>
                
                </div></td>
              </tr>
              <tr>
                <td align="right" class="bodyTextNoAlign">
                
                  <div align="right">
                    <?php
                
                if ($submit){
                        if (!$agree){
                            echo '<style="color:#C00">You must agree to the terms and conditions if you want Arun Books to publish your work.</style>';
                            $errors = 1;
                
                ?>
                    
                    <input type="checkbox" name="agree" id="agree" />
                  </div></td>
                <td><div align="left"><span class="bodyTextNoAlign">I agree to the <a href="terms.html">terms and conditions</a> of publishing my work with Arun Books.</span></div></td>
              </tr>
              <tr>
                <td colspan="2" class="bodyTextNoAlign"><label for="submit"></label>
                  <input name="submit" type="image" class="imageCentre" id="submit" src="Pictures/Graphics/Publish.gif" /></td>
                </tr>
              <tr class="bodyTextNoAlign">
                <td colspan="2">&nbsp;</td>
                </tr>
            </table>
          </form>
          <p class="bodyText">&nbsp;</p>
<p class="bodyText">&nbsp;</p></td>
        </tr>
        <tr>
          <td height="100%">&nbsp;</td>
        </tr>
      </table>
    </div>
  <div id="footer">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="50%">© Arun Books | <a href="../contact.html">Contact Us</a> | <a href="#top">Top</a></td>
        <td width="50%"><div align="right">Site by Avario Babushka</div></td>
      </tr>
    </table>
  </div>
  </div>
</div>
<script type="text/javascript">
<!--
swfobject.registerObject("FlashID");
//-->
</script>
</body>
</html>
 
<?php
        
    $emailSubject = 'Arun Books Publication Request';
    $webMaster = 'avario@hotmail.com';
        
        if ($submit){
                if ($erorrs == 0){
    
    $body = <<<EOD
<br><hr><br>
<b>Arun Books - Publication Request</b><br>
<hr>
Stu, this is a publication request from someone who filled in the form on the Arun Books website.<br>
From $name $lname<br>
$email<br>
$Adress<br>
$city<br>
$postcode<br>
Type of Work: $type<br>
Name of Work: $workname<br>
Please email them to notify them if they have been accepted and with further details.
EOD;
 
    $headers = "From: $email\r\n";
    $headers = "Content-type: text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body, $headers);
    
/* results rendered as HTML */
 
echo header ("Location:../pubsuccess.html")         
                
?>
jegan.aaodis
Forum Newbie
Posts: 15
Joined: Fri Oct 09, 2009 1:56 am

Re: Php form Validation Help Please

Post by jegan.aaodis »

Hi,

In the code you mentioned there is no validation functionality.
First check the values that are you getting it from the post value.
Ie using

If(isset($_POST[submit]))
{
echo strip_tags($_POST['name']);
}


If you are getting the values then then we proceed with the next step.
Next is to store the values to a variable.

Then check using if condition which are mandatory.
If(isset($_POST[submit]))
{
$name= strip_tags($_POST['name']);
if(!$name)
{
echo "Enter your name";
}
elseif(!$email)
{
echo "enter you email id";
}
}


Use this code

Thanks
avario
Forum Newbie
Posts: 3
Joined: Sun Oct 11, 2009 9:56 pm

Re: Php form Validation Help Please

Post by avario »

Thank you for your help. I mainly just want to know why my script is not working. I forgot to close off the if statement at the end. That is how I am trying to verify the form. It has an error variable, if the variable is equal to 1 it will not send the email, the error messages make the error variable equal to one, so if there were no errors the variable will remain zero and will fore send the email... you get what I mean. But it does not work. Why does it not work? Can someone please tell me.

Help is much appreciated.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Php form Validation Help Please

Post by Weiry »

I have not looked at the entire code, but:
you are not closing your IF statements...
You have 8 IF statements which are never closed, causing your first problem of an unexpected end of file.
avario
Forum Newbie
Posts: 3
Joined: Sun Oct 11, 2009 9:56 pm

Re: Php form Validation Help Please

Post by avario »

Weiry wrote:I have not looked at the entire code, but:
you are not closing your IF statements...
You have 8 IF statements which are never closed, causing your first problem of an unexpected end of file.

Thank you so much, that is all it was. I haven't done coding in a while and I guess I just forgot.

Thanks a million.
Post Reply