Jump to content

Form validation alwats showing as invalid


Merdok

Recommended Posts

Hi Guys,

 

I have built a contact form recently and with help from you guys I managed to add a file upload facility to it, however I am now stuck on something else. The form validation that I have written is always showing the form as not valid when it should be. I'm not sure if it is related to the javascript I have attached to the fields, I have never combined the two things this way before and I am not sure how to fix it. I would be most appreciative of any help you can give me. Here is my code:

 

<?php 
// BEGIN SOCKET IMPLEMENTATION 
require_once($_SERVER['DOCUMENT_ROOT'] . '/socket/globals.php'); //Sets the static pageID 

if ($_GET['article']) {
if ($_GET['revision']) { // If viewing a revision from SOCKET
$dblookup = "SELECT articleID, datePosted, articleTitle, permaLink, articleBody, sideBody, parent FROM core_pages_revisions WHERE(revisionID ='".$_GET['revision']."') LIMIT 1";		
} else {
$dblookup = "SELECT articleID, datePosted, articleTitle, permaLink, articleBody, sideBody, parent FROM core_pages WHERE(articleID ='".$_GET['article']."') OR (permaLink ='".$_GET['article']."'";
if($_GET['parent']) {																																											$dblookup .= " AND parent ='".$_GET['parent']."'";
}
$dblookup .= ") LIMIT 1";
}
$data = mysql_query($dblookup) or die('Failed to return data: ' . mysql_error());	
if (mysql_num_rows($data) == 0) { redirect_to('/404');exit;}
/* sorts the data into variables and puts them in an array ready to be called when needed */
while(list($articleID, $datePosted, $articleTitle, $permaLink, $articleBody, $sideBody, $parent) = mysql_fetch_array($data, MYSQL_BOTH))
{
$meta_title = html_entity_decode(stripslashes("$articleTitle"));
$module_ID = 2;
$theH1 = '<h1>' . stripslashes(html_entity_decode($articleTitle)) . '</h1>';
$theParent = $parent;
$parentUrl = $_SERVER['HTTP_REFERER'];
require_once('' . $serverroot . '/style/standard/head.php');
require_once('' . $serverroot . '/style/standard/head2.php');
require_once('style/standard/header.php');

if(isset($_POST['submit'])) {

// form validation

if($_POST['first_name'] == 'First Name'||$_POST['last_name'] == 'Last Name'||$_POST['your_company'] == 'Company'||$_POST['your_email'] == 'Email'||$_POST['your_telephone'] = 'Telephone') { 		$errors = "please ensure all mandatory fields are completed";
} else {

/* Deals with file uploading */
if (($_FILES["ttt"]["size"] < 500000))
  {
  if ($_FILES["ttt"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["ttt"]["error"] . "<br />";
    }
  else
    {
      move_uploaded_file($_FILES["ttt"]["tmp_name"],
      "upload/" . $_FILES["ttt"]["name"]);
      $fileLink = $siteroot. "/upload/" . $_FILES["ttt"]["name"];
      }
  }
else
  {
  echo "Invalid file";
  }
$to = str_replace('info', 'sales', $sc_email);   
$subject = "Quotation request from website";
$body = "From: ".$_POST['first_name']." ".$_POST['last_name']."\n";
if ($_POST['job_title']) {
$body .= "(".$_POST['job_title']." at ".$_POST['your_company'].")\n";
}
$body .= "E-Mail: ".$_POST['your_email']."\n";
$body .= "Telephone: ".$_POST['your_telephone']."\n";
$body .= "Address:\n ".$_POST['your_address']." ,".$_POST['your_country']."\n\n";
$body .= "Project: ".$_POST['project_details']."\n";
$body .= "Translate to: ".$_POST['lang_target']." from ".$_POST['lang_source']."\n";
$body .= "by: ".$_POST['project_deadline'];
$body .= "The text: ".$_POST['text_to_translate'];
$body .= "Link to attachment: <a href=".$fileLink."> Link </a>";
}
if (!$errors) {
	echo '<h1> Email Sent! </h1>';
	echo '<p>Thank you for your enquiry.</p> <p>If it is required a representative will contact you as soon as possible</p><p><strong>Please note:</strong></p>  <p>We endeavour to respond to all requests within ' . $sc_response_time . ' however during busy or holiday periods this may increase. </p>';
	mail($to, $subject, $body);
} else { echo $errors; } 
}

//Main content starts here
/****************************/
/*    Page starts here 		*/
/****************************/
//echo '<img class="hidden" src="'.$siteroot.'/Scripts/phpThumb/phpThumb.php?src='.$siteroot.'/Scripts/phpThumb/phpThumb.php?w=100&h=100&zc='.$articleImagePos.'&src='.$articleImage.'" />';
if ($_SESSION['access_lvl'] <= 1) {
// if a user with appropriate access levels is logged in, allow them to edit this page
// to add
}
echo stripslashes($articleBody);
?>
<form id="quote_form" name="quote_form" enctype="multipart/form-data" method="post" action="<?php $_SERVER['SCRIPT_NAME']?>">
<fieldset>
	<legend> Your Details </legend>
      	<table border="0" cellpadding="0" cellspacing="5">
            <tr>
            <td class="colOne"> Name </td>
              <td class="colTwo"><label for="first_name">First Name</label>
              <input type="text" name="first_name" id="first_name" value="<?php if ($_POST['submit']) { echo $_POST['first_name']; } else { echo 'First Name'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='First Name'}" /></td>
              <td><label for="last_name">Last Name</label>
              <input type="text" name="last_name" id="last_name" value="<?php if ($_POST['submit']) { echo $_POST['last_name']; } else { echo 'Last Name'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Last Name'}" /></td>
            </tr>
            <tr>
            	<td class="colOne"></td>
              <td class="colTwo"><label for="job_title">Job Title</label>
              <input type="text" name="job_title" id="job_title" value="<?php if ($_POST['submit']) { echo $_POST['job_title']; } else { echo 'Job Title'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Job Title'}"/></td>
              <td><label for="your_company">Company</label>
              <input type="text" name="your_company" id="your_company" value="<?php if ($_POST['submit']) { echo $_POST['your_company']; } else { echo 'Company'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Company'}"/></td>
            </tr>
            <tr>
		  <td class="colOne"> Contact </td>
              <td class="colTwo"><label for="your_telephone">Telephone</label>
              <input type="text" name="your_telephone" id="your_telephone"  value="<?php if ($_POST['submit']) { echo $_POST['your_telephone']; } else { echo 'Telephone'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Telephone'}" /></td>
              <td><label for="your_email">Email Address</label>
              <input type="text" name="your_email" id="your_email"  value="<?php if ($_POST['submit']) { echo $_POST['your_email']; } else { echo 'Email'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Email'}"/></td>
            </tr>
            <tr>
		  <td class="colOne"> Address </td>
              <td class="colTwo" valign="top"><label for="your_address">Address</label>
              <textarea name="your_address" id="your_address"  cols="45" rows="5" ><?php echo $_POST['your_address']; ?></textarea></td>
              <td></td>
            </tr>
             <tr>
			<td class="colOne"> Country </td>
              	<td class="colTwo"><label for="your_country">Country</label>
                <select name="your_country" id="your_country">
                <option value="" <?php if (!$_POST['submit']) { echo 'selected="selected'; } ?>>Select your country</option>
                <?php $countries = mysql_query("SELECT * FROM custom_countries") or die ("Could not get countries list".mysql_error());			
	          while($option = mysql_fetch_array($countries)) {
			  if ($_POST['your_country'] == $option['country_name']) { $selected = 'selected="selected'; }
        		  echo '<option '.$selected.' value="' . $option['country_name'].'">' .$option['country_name'].'</option>';
        		  }
	         ?>
              </select></td>
              <td></td>
            </tr>
   		</table>
    </fieldset>
    <fieldset>
        <legend> Your Quote </legend>
        <table border="0" cellpadding="0" cellspacing="5">
		<tr>
			<td class="colOne"> Languages </td>
              	<td class="colTwo"><label for="lang_source">Source Language</label>
                <select name="lang_source" id="lang_source">
                <option value="" selected="selected">Translate from</option>
                <?php $languages = mysql_query("SELECT * FROM custom_languages") or die ("Could not get languages list".mysql_error());			
	          while($option = mysql_fetch_array($languages)) {
				  if ($_POST['lang_source'] == $option['language_name']) { $selected = 'selected="selected'; }
				  echo '<option '.$selected.' value="' . $option['language_name'].'">' .$option['language_name'].'</option>';
        		  }
	         ?>
              </select>
              <label for="lang_target[]">Target Language</label></td>
              <td>(hold CTRL to select multiple languages)<select name="lang_target[]" size="6" multiple="multiple" id="lang_target[]">
                <option value="" selected="selected">Translate to</option>
                <?php $languages = mysql_query("SELECT * FROM custom_languages") or die ("Could not get languages list".mysql_error());			
	          while($option = mysql_fetch_array($languages)) {
  				  if ($_POST['lang_target'] == $option['language_name']) { $selected = 'selected="selected'; }
				  echo '<option '.$selected.' value="' . $option['language_name'].'">' .$option['language_name'].'</option>';
        		  }
	         ?>
              </select></td>
            </tr>
            <tr>
		  <td class="colOne"> Project</td>
              <td class="colTwo"><label for="project_details">Project Details</label>
              <input type="text" name="project_details" id="project_details"  value="<?php if ($_POST['submit']) { echo $_POST['project_details']; } else { echo 'Reference'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Reference'}" /></td>
              <td><label for="project_deadline">Project Deadline</label>
              <input type="text" name="project_deadline" id="project_deadline"  value="<?php if ($_POST['submit']) { echo $_POST['project_deadline']; } else { echo 'Deadline'; } ?>" onfocus="this.value=''" onblur="if (this.value = "") {this.value='Deadline'}" /></td>
            </tr>
            <tr>
		  <td class="colOne"> Text to translate</td>
              <td colspan="2"><label for="ttt">Text to translate</label>
              <input type="file" name="ttt" id="ttt" value="" />
            </tr>

            <tr>
                <td> </td>
                <td><input class="form_button" type="submit" name="submit" id="submit" value="Submit" /><p class="red size10font"><?php echo $error ?></p></td>
            </tr>
         </table>
    </fieldset>
</form>

</div>
<?php  
//Main content ends here
require_once('style/standard/footer.php');
}
}
?>

Link to comment
Share on other sites

Also, the formatting of your code could use some improvement.  Consistent indenting would make the logic clearer, and seperating out the validation for each form field onto a different line, or even a different if condition altogether, would make the code easier to read and make bugs easier to find.

Link to comment
Share on other sites

Also, the formatting of your code could use some improvement.  Consistent indenting would make the logic clearer, and seperating out the validation for each form field onto a different line, or even a different if condition altogether, would make the code easier to read and make bugs easier to find.

 

Oh God I can't believe it was that! I knew that too, I MUST have been tired, yeah the indenting is terrible, I wrote the CMS the form comes from a long time ago, it's horrible code now, I need to rewrite it really to be object oriented :)

 

Thanks for noticing my idiocy, works like a charm now :)

Link to comment
Share on other sites

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.