By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

validate my form and header to another success page

P: 22
I am trying to validate my form fields and redirect the user to success page

so this is the PHP code

Expand|Select|Wrap|Line Numbers
  1. if (empty($_POST['experiences'])) {
  2.         $error['experiencesErr'] = "Experiences Required";
  3.     } else {
  4.         $experiences = check_input($_POST['experiences']);
  5.         if (!preg_match("/^[0-9_a-zA-Z ]*$/", $experiences)) {
  6.             $error['experiencesErr'] = "Only letters, numbers and '_' allowed";
  7.         }
  8.     }
  9.  
  10.     $courses = check_input($_POST['courses']);
  11.     if (!preg_match("/^[0-9_a-zA-Z ]*$/", $courses)) {
  12.         $error['coursesErr'] = "Only letters, numbers and '_' allowed";
  13.     }
  14.  
  15.     $careerObjective = check_input($_POST['careerObjective']);
  16.     if (!preg_match("/^[0-9_a-zA-Z ]*$/", $careerObjective)) {
  17.         $error['careerObjectiveErr'] = "Only letters, numbers and '_' allowed";
  18.     }
  19.  
  20.     if (empty($_POST['availability'])) {
  21.         $error['availabilityErr'] = "Availability Required";
  22.     } else {
  23.         $availability = check_input($_POST['availability']);
  24.     }
  25.  
  26.     if (empty($_POST['typeOfJob'])) {
  27.         $error['typeOfJobErr'] = "Full/Part Time Required";
  28.     } else {
  29.         $typeOfJob = check_input($_POST['typeOfJob']);
  30.     }
  31.  
  32.     if (empty($_POST['typeOfJob'])) {
  33.         $error['typeOfJobErr'] = "Full/Part Time Required";
  34.     } else {
  35.         $typeOfJob = check_input($_POST['typeOfJob']);
  36.     }
  37.  
  38.     if (empty($_POST['rank'])) {
  39.         $error['rankErr'] = "Self-assessment Required";
  40.     } else {
  41.         $rank = check_input($_POST['rank']);
  42.     }
  43.  
  44.     if (empty($_POST['jTitle'])) {
  45.         $error['jTitleErr'] = "Job Field Required";
  46.     } else {
  47.         $jTitle = check_input($_POST['jTitle']);
  48.     }
  49.  
  50.     $otherJobTitle = check_input($_POST['otherJobTitle']);
  51.     if (!preg_match("/^[0-9_a-zA-Z ]*$/", $otherJobTitle)) {
  52.         $error['otherJobTitleErr'] = "Only letters, numbers and '_' allowed";
  53.     }
  54.  
  55.     if (empty($_POST['salaryRange'])) {
  56.         $error['salaryRangeErr'] = "Salary Range Required";
  57.     } else {
  58.         $salaryRange = check_input($_POST['salaryRange']);
  59.     }
  60.  
  61.     if (empty($_POST['currency'])) {
  62.         $error['currencyErr'] = "Currency Required";
  63.     } else {
  64.         $currency = check_input($_POST['currency']);
  65.     }
  66.  
  67.     $workIn = check_input($_POST['workIn']);
  68.  
  69.     if(!$error){
  70.         $putData = $db->prepare("INSERT INTO hired_ts_info (id, uid, experiences, courses, career_objective,
  71.         availability, type_of_job, rank, job_title, other_job_title, salary_range, currency, workIn)
  72.         VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  73.  
  74.         $putData->bind_param('iisssssssssss', $id, $uid, $experiences, $courses, $careerObjective, $availability,
  75.         $typeOfJob, $rank, $jTitle, $otherJobTitle, $salaryRange, $currency, $workIn);
  76.  
  77.         if($putData->execute()){
  78.             header("Location:?pid=4&pp=2&pps=technicalSummary&m=g");
  79.         }else{
  80.             echo "Error on executing";
  81.         }
  82.     }
  83. }
  84. ?>
  85.  
and this is the first lines of the HTML code

Expand|Select|Wrap|Line Numbers
  1. <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="personRegestrationPage4">
  2.  
  3.  
  4. <div class="f_left width100percent">
  5.     <div class="TwoLine">
  6.         <label for="experiences" class="requiredFields">experiences and qualifications</label>
  7.         <textarea name="experiences" id="experiences"></textarea>
  8.         <span class="notAllowed"><?php if (isset($error)) {
  9.                 echo $error['experiencesErr'];
  10.             }?></span>
  11.     </div>
  12.  
  13.     <div class="TwoLine">
  14.         <label for="courses">Previous Courses</label>
  15.         <textarea name="courses" id="courses"></textarea>
  16.         <span class="notAllowed"><?php if (isset($error)) {
  17.                 echo $error['coursesErr'];
  18.             } ?></span>
  19.     </div>
  20. </div>
  21.  
and this is the submit button code

Expand|Select|Wrap|Line Numbers
  1. <input type="submit" name="subTs" id="subTs" value="Save Changes" class="submitBtn4">
Update
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 1);
  5.  
  6.  
  7. $experiences = $courses = $careerObjective = $availability = $typeOfJob = $rank = $jTitle = $otherJobTitle
  8.     = $salaryRange = $currency = $workIn = "";
  9.  
  10. $experiencesErr = $coursesErr = $careerObjectiveErr = $availabilityErr = $typeOfJobErr = $rankErr = $jTitleErr
  11.     = $otherJobTitleErr = $salaryRangeErr = $currencyErr = $workInErr = "";
  12.  
  13.  
  14. $id = "";
  15. $uid = "";
  16.  
  17. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  18.     $error = array();
  19.  
  20.     if (empty($_POST['experiences'])) {
  21.         $error['experiencesErr'] = "Experiences Required";
  22.     } else {
  23.         $experiences = check_input($_POST['experiences']);
  24.         if (!preg_match("/^[0-9_a-zA-Z ]*$/", $experiences)) {
  25.             $error['experiencesErr'] = "Only letters, numbers and '_' allowed";
  26.         }
  27.     }
  28.  
  29.     $courses = check_input($_POST['courses']);
  30.     if (!preg_match("/^[0-9_a-zA-Z ]*$/", $courses)) {
  31.         $error['coursesErr'] = "Only letters, numbers and '_' allowed";
  32.     }
  33.  
  34.     $careerObjective = check_input($_POST['careerObjective']);
  35.     if (!preg_match("/^[0-9_a-zA-Z ]*$/", $careerObjective)) {
  36.         $error['careerObjectiveErr'] = "Only letters, numbers and '_' allowed";
  37.     }
  38.  
  39.     if (empty($_POST['availability'])) {
  40.         $error['availabilityErr'] = "Availability Required";
  41.     } else {
  42.         $availability = check_input($_POST['availability']);
  43.     }
  44.  
  45.     if (empty($_POST['typeOfJob'])) {
  46.         $error['typeOfJobErr'] = "Full/Part Time Required";
  47.     } else {
  48.         $typeOfJob = check_input($_POST['typeOfJob']);
  49.     }
  50.  
  51.     if (empty($_POST['typeOfJob'])) {
  52.         $error['typeOfJobErr'] = "Full/Part Time Required";
  53.     } else {
  54.         $typeOfJob = check_input($_POST['typeOfJob']);
  55.     }
  56.  
  57.     if (empty($_POST['rank'])) {
  58.         $error['rankErr'] = "Self-assessment Required";
  59.     } else {
  60.         $rank = check_input($_POST['rank']);
  61.     }
  62.  
  63.     if (empty($_POST['jTitle'])) {
  64.         $error['jTitleErr'] = "Job Field Required";
  65.     } else {
  66.         $jTitle = check_input($_POST['jTitle']);
  67.     }
  68.  
  69.     $otherJobTitle = check_input($_POST['otherJobTitle']);
  70.     if (!preg_match("/^[0-9_a-zA-Z ]*$/", $otherJobTitle)) {
  71.         $error['otherJobTitleErr'] = "Only letters, numbers and '_' allowed";
  72.     }
  73.  
  74.     if (empty($_POST['salaryRange'])) {
  75.         $error['salaryRangeErr'] = "Salary Range Required";
  76.     } else {
  77.         $salaryRange = check_input($_POST['salaryRange']);
  78.     }
  79.  
  80.     if (empty($_POST['currency'])) {
  81.         $error['currencyErr'] = "Currency Required";
  82.     } else {
  83.         $currency = check_input($_POST['currency']);
  84.     }
  85.  
  86.     $workIn = check_input($_POST['workIn']);
  87.  
  88.     if (!$error) {
  89.         $putData = $db->prepare("INSERT INTO hired_ts_info (id, uid, experiences, courses, career_objective,
  90.         availability, type_of_job, rank, job_title, other_job_title, salary_range, currency, workIn)
  91.         VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  92.  
  93.         $putData->bind_param('iisssssssssss', $id, $uid, $experiences, $courses, $careerObjective, $availability,
  94.             $typeOfJob, $rank, $jTitle, $otherJobTitle, $salaryRange, $currency, $workIn);
  95.  
  96.         if ($putData->execute()) {
  97.             header("Location:?pid=4&pp=2&pps=technicalSummary&m=g");
  98.         } else {
  99.             echo "Error on executing";
  100.         }
  101.     } else {
  102.         $error = array(
  103.             "coursesErr" => "",
  104.             "careerObjectiveErr" => "",
  105.             "otherJobTitleErr" => "",
  106.             "experiencesErr" => "",
  107.             "availabilityErr" => "",
  108.             "typeOfJobErr" => "",
  109.             "rankErr" => "",
  110.             "jTitleErr" => "",
  111.             "salaryRangeErr" => "",
  112.             "currencyErr" => "",
  113.         );
  114.     }
  115. }
  116. ?>
  117.  

**still that didn't solve the issue **

1- now the code submit correctly and gos to my DB.
2- if the fields is empty or not allowed input the message don't appear any more under the fields
any Ideas please

Thanks
Aug 18 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.