Submit my form and title to another success page

I am trying to validate my form fields and redirect user page to success page

so this is 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 and '_' allowed";
        }
    }

    $courses = check_input($_POST['courses']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $courses)) {
        $error['coursesErr'] = "Only letters, numbers and '_' allowed";
    }

    $careerObjective = check_input($_POST['careerObjective']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $careerObjective)) {
        $error['careerObjectiveErr'] = "Only letters, numbers and '_' 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 and '_' 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 INTO 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 these are the 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 and 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 this is the submit button code

<input type="submit" name="subTs" id="subTs" value="Save Changes" class="submitBtn4">

      

The problem is now when I submit the form it returns without inserting anything into the db and without getting an error

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 and '_' allowed";
        }
    }

    $courses = check_input($_POST['courses']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $courses)) {
        $error['coursesErr'] = "Only letters, numbers and '_' allowed";
    }

    $careerObjective = check_input($_POST['careerObjective']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $careerObjective)) {
        $error['careerObjectiveErr'] = "Only letters, numbers and '_' 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 and '_' 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 INTO 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 it didn't solve problem 1- now the code is sent correctly and gos to my DB. 2- if the fields are empty or not allowed, the message is no longer displayed below the fields any ideas please

+3


source to share


1 answer


Cause your script doesn't show any errors in this

so that you set the value again for your error in the else statement, which is empty; in this line

else {
        $error = array(
            "coursesErr" => "",
            "careerObjectiveErr" => "",
            "otherJobTitleErr" => "",
            "experiencesErr" => "",
            "availabilityErr" => "",
            "typeOfJobErr" => "",
            "rankErr" => "",
            "jTitleErr" => "",
            "salaryRangeErr" => "",
            "currencyErr" => "",
        );
    }

      

in this line you will set the value for your $ error Arrray and set them to empty. The point is, even you have given the array value before, but when php reaches that line, it changes that value to the empty value that you define.

For example, if you have code like this

$x=4; 
$x=5;

      



although you have the same variable, but if you do echo $x;

, it will always give you 5, because that is the last value for $ x;

to understand it more clearly, what you should do is give some value in any $ error argument in else, it will show that $ error

like this

$error = array(
            "coursesErr" => "my name is spider man",
            "careerObjectiveErr" => "",
            "otherJobTitleErr" => "",
            "experiencesErr" => "",
            "availabilityErr" => "",
            "typeOfJobErr" => "",
            "rankErr" => "",
            "jTitleErr" => "",
            "salaryRangeErr" => "",
            "currencyErr" => "",
        );
    }

      

and than running the code, it will show you this specific error and not another one because you set its value,

what would you do now, a simple option is to completely remove the else statement,

0


source







All Articles