繁体   English   中英

验证我的表单和标题到另一个成功页面

[英]validate my form and header to another success page

我正在尝试验证我的表单字段并将用户重定向到成功页面

所以这是PHP代码

<?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";
        }
    }
}
?>

这是HTML代码的第一行

<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>

这是提交按钮代码

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

问题现在当我提交表单时它返回而没有向数据库插入任何内容并且没有收到错误消息

更新

<?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" => "",
        );
    }
}
?>

仍然没有解决问题1-现在代码正确提交和gos到我的数据库。 2-如果字段为空或不允许输入,则消息不再出现在任何Ideas pleasee的字段下

你的脚本背后的原因是没有显示任何错误

你在else语句中为你的错误再次设置值为空; 在这些方面

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

在这些行中,您设置$ error Arrray的值,并将它们设置为空。 事情就是这样,即使您之前设置了数组值,但是当php到达这些行时,它会将这些值更改为您定义的空值,

例如,如果您有这样的代码

$x=4; 
$x=5;

即使你有相同的变量,但如果你echo $x; 它会给你总是5因为这是$ x的最后一个值;

为了更清楚地理解你应该做什么在else语句中的任何$ error数组中给出一些值,它将显示$ error

像这样

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

而且运行代码,它会显示特定错误,而不是任何其他错误,因为你设置它的值,

所以你现在应该怎么做,简单的选择是完全删除else语句,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM