Jump to content

Problem Validation


relecule

Recommended Posts

Hi

 

Hope somebody can help here. I am kinda new to php.

 

I have been writing a form script which will save information into a database. The problem is that every time I refresh my page it inserts the data into the tables. I only want it to insert the data once the validated form data has been added. I am busy writing the component for joomla..

 

Please can somebody shed some light here. thanks in advance.

 

defined( '_JEXEC' ) or die( 'Restricted access' );

$db =& JFactory::getDBO();

 

$required = array("name" => "Your Name",

                  "emailaddr" => "Email Address",

                  "telephone" => "Telephone");

 

foreach($required as $field => $label)  {

  if (!$_POST[$field]) {

      $warnings[$field] = "<font color='red'>Required</font>";

      }

  }

 

if ($_POST["emailaddr"] &&

  !ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$",

  $_POST["emailaddr"]))

  $warnings["emailaddr"] = "<font color='red'>Invalid email</font>";

 

if ($_POST["telephone"] &&

  !ereg("[[:digit:]]{10}$",

  $_POST["telephone"]))

  $warnings["telephone"] = "Must be 0215555555";

 

 

 

$db->query();

// Grab and format all of the variable entries from the form.

$fldName = "'" . $db->getEscaped(JRequest::getVar('name')) . "'";

$fldSurname = "'" . $db->getEscaped(JRequest::getVar( 'surname')) . "'";

$fldTitle = "'" . $db->getEscaped(JRequest::getVar( 'title' )) . "'";

$fldGender = "'" . $db->getEscaped(JRequest::getVar( 'gender' )) . "'";

$fldRace = "'" . $db->getEscaped(JRequest::getVar( 'race' )) . "'";

$fldAge = "'" . $db->getEscaped(JRequest::getVar( 'age' )) . "'";

$fldIdnumber = "'" . $db->getEscaped(JRequest::getVar( 'idnumber' )) . "'";

$fldHtel = "'" . $db->getEscaped(JRequest::getVar( 'htel' )) . "'";

$fldWtel = "'" . $db->getEscaped(JRequest::getVar( 'wtel' )) . "'";

$fldFax = "'" . $db->getEscaped(JRequest::getVar( 'fax' )) . "'";

$fldEmailaddr = "'" . $db->getEscaped(JRequest::getVar( 'emailaddr' )) . "'";

$fldPostaladdr = "'" . $db->getEscaped(JRequest::getVar( 'postaladdr' )) . "'";

$fldPostalcode = "'" . $db->getEscaped(JRequest::getVar( 'postalcode' )) . "'";

$fldReference = "'" . $db->getEscaped(JRequest::getVar( 'reference' )) . "'";

$fldProgram = "'" . $db->getEscaped(JRequest::getVar( 'program' )) . "'";

$fldQualification = "'" . $db->getEscaped(JRequest::getVar( 'qualification' )) . "'";

$fldReason = "'" . $db->getEscaped(JRequest::getVar( 'reason' )) . "'";

$fldEmployedstatus = "'" . $db->getEscaped(JRequest::getVar( 'employedstatus' )) . "'";

$fldEmploymenttype = "'" . $db->getEscaped(JRequest::getVar( 'employmenttype' )) . "'";

$fldJobdesc = "'" . $db->getEscaped(JRequest::getVar( 'jobdesc' )) . "'";

$fldEnquire = "'" . $db->getEscaped(JRequest::getVar( 'enquire' )) . "'";

$fldOfficesc = "'" . $db->getEscaped(JRequest::getVar( 'officesc' )) . "'";

$fldOfficeaddkey = "'" . $db->getEscaped(JRequest::getVar( 'officeaddkey' )) . "'";

// Store the IP of the user submitting the enquire

$userIp = "'" . $_SERVER['REMOTE_ADDR'] . "'";

 

if (count($warnings) > 0) {

 

// Insert all variables into the jos_enquires table

$insertFields = "INSERT INTO #__enquire " .

"(name, surname, title, gender, race, age, idnumber, htel, wtel, fax, emailaddr, postaladdr, postalcode, reference, program, qualification, reason, employedstatus, employmenttype, jobdesc, enquire, officesc, officeaddkey, userip) " .

"VALUES (" . $fldName . "," . $fldSurname . "," . $fldTitle . "," . $fldGender . "," . $fldRace . "," . $fldAge . "," . $fldIdnumber . "," . $fldHtel . "," . $fldWtel . "," . $fldFax . "," . $fldEmailaddr . "," . $fldPostaladdr . "," . $fldPostalcode . ",". $fldReference . ",". $fldProgram . "," . $fldQualification . "," . $fldReason . "," . $fldEmployedstatus . "," . $fldEmploymenttype . "," . $fldJobdesc . "," . $fldEnquire ."," . $fldOfficesc . "," . $fldOfficeaddkey . "," . $userIp . ");";

 

 

 

$db->setQuery( $insertFields, 0);

$db->query();

 

?>

 

<h1 class="contentheading">Enquiry form</h1>

 

  <form method="post" action="index.php?option=com_enquire">

<table class="office" width="571" border="0" cellspacing="0" cellpadding="5">

 

  <tr>

    <td class="formhead" colspan="3"><strong>Personal Details    </strong></td>

    </tr>

  <tr>

    <td width="188">Name    </td>

    <td width="280"><input type="text" value="<?php echo $_POST[name];?>" name="name" id="name" /></td>

    <td width="100"><?php echo $warnings["name"];?></td>

  </tr>

  <tr>

    <td>Surname</td>

    <td><input type="text" name="surname" id="surname" />  <font color="red">*</font></td>

  </tr>

  <tr>

    <td>Title</td>

    <td><select name="title" id="title">

      <option>Mr</option>

      <option>Mrs</option>

      <option>Miss</option>

      <option>Ms</option>

    </select>  </td>

    <td></td>

  </tr>

  <tr>

    <td>Gender</td>

    <td><select name="gender" id="gender">

      <option>Male</option>

      <option>Female</option>

    </select>  <font color="red">*</font>    </td>

    <td></td>

  </tr>

  <tr>

    <td>Race</td>

    <td><select name="race" id="race">

      <option>Black</option>

      <option>Other</option>

    </select>    </td>

    <td></td>

  </tr>

  <tr>

    <td>Age</td>

    <td><input size="2" maxlength="2" type="text" name="age" id="age" /></td>

    <td></td>

  </tr>

  <tr>

    <td>ID number</td>

    <td><input size="13" maxlength="13" type="text" name="idnumber" id="idnumber" /></td>

    <td></td>

  </tr>

  <tr>

    <td class="formhead" colspan="3"><strong>Contact Details</strong></td>

    </tr>

  <tr>

    <td>H/ tel</td>

    <td><input type="text" name="htel" id="htel" />  <font color="red">*</font></td>

    <td></td>

  </tr>

  <tr>

    <td>W/ tel</td>

    <td><input type="text" name="wtel" id="wtel" />  <font color="red">*</font></td>

    <td></td>

  </tr>

  <tr>

    <td>Fax no</td>

    <td><input type="text" name="fax" id="fax" /></td>

    <td></td>

  </tr>

  <tr>

    <td>E-mail</td>

    <td><input type="text" name="emailaddr" id="emailaddr" /></td>

    <td><?php echo $warnings["emailaddr"];?></td>

  </tr>

  <tr>

    <td>Postal address</td>

    <td><textarea name="postaladdr" id="postaladdr" cols="35" rows="5"></textarea></td>

    <td></td>

  </tr>

  <tr>

    <td>Postal Code</td>

    <td><input type="text" name="postalcode" id="postalcode" /></td>

    <td></td>

  </tr>

  <tr>

    <td class="formhead" colspan="3"><b>Information</b></td>

    </tr>

  <tr>

    <td>Where did you hear about us:</td>

    <td><input type="text" name="reference" id="reference" /></td>

    <td></td>

  </tr>

  <tr>

    <td>Program of Interest</td>

    <td><input type="text" name="program" id="program" /></td>

    <td></td>

  </tr>

  <tr>

    <td>Highest qualification passed</td>

    <td><select name="qualification" id="qualification">

      <option>Still at school</option>

      <option>Grade 12</option>

      <option>Technikon</option>

      <option>University</option>

      <option>Other</option>

    </select>    </td>

    <td></td>

  </tr>

  <tr>

    <td>Reason for wanting to do this course</td>

    <td><textarea name="reason" id="reason" cols="35" rows="5"></textarea></td>

    <td></td>

  </tr>

  <tr>

    <td class="formhead" colspan="3"><strong>Employement Information</strong></td>

    </tr>

  <tr>

    <td>Employed</td>

    <td><select name="employedstatus" id="employed_status">

      <option>Yes</option>

      <option>No</option>

    </select>    </td>

    <td></td>

  </tr>

  <tr>

    <td>If yes what kind of employment:</td>

    <td><select name="employmenttype" id="employment_type">

      <option>Permanent</option>

      <option>Contract</option>

      <option>Temporary</option>

      <option>Self-Employed</option>

    </select>    </td>

    <td></td>

  </tr>

  <tr>

    <td>Job Description </td>

    <td><select name="jobdesc" id="jobdesc">

      <option>Admin</option>

      <option>Management</option>

      <option>Professional</option>

      <option>Skilled labour</option>

      <option>Unskilled labour</option>

    </select>    </td>

    <td></td>

  </tr>

  <tr>

    <td>Your Message</td>

    <td><textarea name="enquire" id="enquire" cols="35" rows="5"></textarea></td>

    <td></td>

  </tr>

</table>

<br>

<table class="office" width="571" border="0" cellspacing="0" cellpadding="5">

  <tr>

    <td colspan="3" class="officeformhead"><b>Office use only</b></td>

  </tr>

  <tr>

    <td>SC</td>

    <td><input type="text" name="officesc" id="office_sc" /></td>

  </tr>

  <tr>

    <td>ADKEY</td>

    <td><input type="text" name="officeaddkey" id="office_addkey" /></td>

  </tr>

    <tr>

    <td></td>

    <td></td>

  </tr>

  <tr>

    <td width="188">Email Address    </td>

    <td width="280"><input type="text" value="<?php echo $_POST;?>" name="email" id="name" /></td>

    <td></td>

  </tr>

  <tr>

    <td width="188">Telephone  </td>

    <td width="280"><input type="text" value="<?php echo $_POST[telephone];?>" name="telephone" id="telephone" /></td>

    <td><?php echo $warnings["telephone"];?></td>

  </tr>

 

 

 

 

</table>

<br>

<input type="submit" name="Submit" value="Submit" />

 

      </form>

 

Link to comment
https://forums.phpfreaks.com/topic/103268-problem-validation/
Share on other sites

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.