php - validate my form and header to another success page -
i trying validate form fields , redirect user success page
so php code
<?php error_reporting(e_all); ini_set('display_errors', 1); $experiences = $courses = $careerobjective = $availability = $typeofjob = $rank = $jtitle = $otherjobtitle = $salaryrange = $currency = $workin = ""; $experienceserr = $courseserr = $careerobjectiveerr = $availabilityerr = $typeofjoberr = $rankerr = $jtitleerr = $otherjobtitleerr = $salaryrangeerr = $currencyerr = $workinerr = ""; $id = ""; $uid = ""; if ($_server['request_method'] == "post") { $error = array( "courseserr"=>"", "careerobjectiveerr"=>"", "otherjobtitleerr"=>"", "experienceserr"=>"", "availabilityerr"=>"", "typeofjoberr"=>"", "rankerr"=>"", "jtitleerr"=>"", "salaryrangeerr"=>"", "currencyerr"=>"", ); if (empty($_post['experiences'])) { $error['experienceserr'] = "experiences required"; } else { $experiences = check_input($_post['experiences']); if (!preg_match("/^[0-9_a-za-z ]*$/", $experiences)) { $error['experienceserr'] = "only letters, numbers , '_' allowed"; } } $courses = check_input($_post['courses']); if (!preg_match("/^[0-9_a-za-z ]*$/", $courses)) { $error['courseserr'] = "only letters, numbers , '_' allowed"; } $careerobjective = check_input($_post['careerobjective']); if (!preg_match("/^[0-9_a-za-z ]*$/", $careerobjective)) { $error['careerobjectiveerr'] = "only letters, numbers , '_' allowed"; } if (empty($_post['availability'])) { $error['availabilityerr'] = "availability required"; } else { $availability = check_input($_post['availability']); } if (empty($_post['typeofjob'])) { $error['typeofjoberr'] = "full/part time required"; } else { $typeofjob = check_input($_post['typeofjob']); } if (empty($_post['typeofjob'])) { $error['typeofjoberr'] = "full/part time required"; } else { $typeofjob = check_input($_post['typeofjob']); } if (empty($_post['rank'])) { $error['rankerr'] = "self-assessment required"; } else { $rank = check_input($_post['rank']); } if (empty($_post['jtitle'])) { $error['jtitleerr'] = "job field required"; } else { $jtitle = check_input($_post['jtitle']); } $otherjobtitle = check_input($_post['otherjobtitle']); if (!preg_match("/^[0-9_a-za-z ]*$/", $otherjobtitle)) { $error['otherjobtitleerr'] = "only letters, numbers , '_' allowed"; } if (empty($_post['salaryrange'])) { $error['salaryrangeerr'] = "salary range required"; } else { $salaryrange = check_input($_post['salaryrange']); } if (empty($_post['currency'])) { $error['currencyerr'] = "currency required"; } else { $currency = check_input($_post['currency']); } $workin = check_input($_post['workin']); if(!$error){ $putdata = $db->prepare("insert hired_ts_info (id, uid, experiences, courses, career_objective, availability, type_of_job, rank, job_title, other_job_title, salary_range, currency, workin) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $putdata->bind_param('iisssssssssss', $id, $uid, $experiences, $courses, $careerobjective, $availability, $typeofjob, $rank, $jtitle, $otherjobtitle, $salaryrange, $currency, $workin); if($putdata->execute()){ header("location:?pid=4&pp=2&pps=technicalsummary&m=g"); }else{ echo "error on executing"; } } } ?>
and first lines of html code
<form action="<?php $_server['php_self']; ?>" method="post" id="personregestrationpage4"> <div class="f_left width100percent"> <div class="twoline"> <label for="experiences" class="requiredfields">experiences , qualifications</label> <textarea name="experiences" id="experiences"></textarea> <span class="notallowed"><?php if (isset($error)) { echo $error['experienceserr']; }?></span> </div> <div class="twoline"> <label for="courses">previous courses</label> <textarea name="courses" id="courses"></textarea> <span class="notallowed"><?php if (isset($error)) { echo $error['courseserr']; } ?></span> </div> </div>
and submit button code
<input type="submit" name="subts" id="subts" value="save changes" class="submitbtn4">
problem when submit form come without inserting db , no error message received
update
<?php error_reporting(e_all); ini_set('display_errors', 1); $experiences = $courses = $careerobjective = $availability = $typeofjob = $rank = $jtitle = $otherjobtitle = $salaryrange = $currency = $workin = ""; $experienceserr = $courseserr = $careerobjectiveerr = $availabilityerr = $typeofjoberr = $rankerr = $jtitleerr = $otherjobtitleerr = $salaryrangeerr = $currencyerr = $workinerr = ""; $id = ""; $uid = ""; if ($_server['request_method'] == "post") { $error = array(); if (empty($_post['experiences'])) { $error['experienceserr'] = "experiences required"; } else { $experiences = check_input($_post['experiences']); if (!preg_match("/^[0-9_a-za-z ]*$/", $experiences)) { $error['experienceserr'] = "only letters, numbers , '_' allowed"; } } $courses = check_input($_post['courses']); if (!preg_match("/^[0-9_a-za-z ]*$/", $courses)) { $error['courseserr'] = "only letters, numbers , '_' allowed"; } $careerobjective = check_input($_post['careerobjective']); if (!preg_match("/^[0-9_a-za-z ]*$/", $careerobjective)) { $error['careerobjectiveerr'] = "only letters, numbers , '_' allowed"; } if (empty($_post['availability'])) { $error['availabilityerr'] = "availability required"; } else { $availability = check_input($_post['availability']); } if (empty($_post['typeofjob'])) { $error['typeofjoberr'] = "full/part time required"; } else { $typeofjob = check_input($_post['typeofjob']); } if (empty($_post['typeofjob'])) { $error['typeofjoberr'] = "full/part time required"; } else { $typeofjob = check_input($_post['typeofjob']); } if (empty($_post['rank'])) { $error['rankerr'] = "self-assessment required"; } else { $rank = check_input($_post['rank']); } if (empty($_post['jtitle'])) { $error['jtitleerr'] = "job field required"; } else { $jtitle = check_input($_post['jtitle']); } $otherjobtitle = check_input($_post['otherjobtitle']); if (!preg_match("/^[0-9_a-za-z ]*$/", $otherjobtitle)) { $error['otherjobtitleerr'] = "only letters, numbers , '_' allowed"; } if (empty($_post['salaryrange'])) { $error['salaryrangeerr'] = "salary range required"; } else { $salaryrange = check_input($_post['salaryrange']); } if (empty($_post['currency'])) { $error['currencyerr'] = "currency required"; } else { $currency = check_input($_post['currency']); } $workin = check_input($_post['workin']); if (!$error) { $putdata = $db->prepare("insert hired_ts_info (id, uid, experiences, courses, career_objective, availability, type_of_job, rank, job_title, other_job_title, salary_range, currency, workin) value(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $putdata->bind_param('iisssssssssss', $id, $uid, $experiences, $courses, $careerobjective, $availability, $typeofjob, $rank, $jtitle, $otherjobtitle, $salaryrange, $currency, $workin); if ($putdata->execute()) { header("location:?pid=4&pp=2&pps=technicalsummary&m=g"); } else { echo "error on executing"; } } else { $error = array( "courseserr" => "", "careerobjectiveerr" => "", "otherjobtitleerr" => "", "experienceserr" => "", "availabilityerr" => "", "typeofjoberr" => "", "rankerr" => "", "jtitleerr" => "", "salaryrangeerr" => "", "currencyerr" => "", ); } } ?>
still didn't solve issue 1- code submit correctly , gos db. 2- if fields empty or not allowed input message don't appear more under fields ideas pleasee
the reason behind script not showing error this
that set value again error in else statement empty; in these line
else { $error = array( "courseserr" => "", "careerobjectiveerr" => "", "otherjobtitleerr" => "", "experienceserr" => "", "availabilityerr" => "", "typeofjoberr" => "", "rankerr" => "", "jtitleerr" => "", "salaryrangeerr" => "", "currencyerr" => "", ); }
in these line set value $error arrray, , set them empty. things set array value before, when php reaches these line, changes value empty value define,
for example if have code
$x=4; $x=5;
even though got same variable, if echo $x;
gonna give 5 cause last value $x;
to understand more should give value in $error array in else statement show $error
like
$error = array( "courseserr" => "my name spider man", "careerobjectiveerr" => "", "otherjobtitleerr" => "", "experienceserr" => "", "availabilityerr" => "", "typeofjoberr" => "", "rankerr" => "", "jtitleerr" => "", "salaryrangeerr" => "", "currencyerr" => "", ); }
and run code, show particular error not other, because set value,
so should now, easy option remove else statement completely,
Comments
Post a Comment