在 if 语句后回显消息并将其显示在下拉列表附近

Echo a message after an if statement and display it near the dropdown list

我有一个 if 语句来检查我拥有的开始年份下拉列表中的第一年是否小于结束年份下拉列表中的年份。

出于这个原因,每当开始年份大于结束年份时,我都希望在它们旁边显示一条错误消息。

我该怎么做?

我的代码如下...

    <?php

if(isset($_POST['submit_next']))
{

    $username = $_SESSION['username'];
    $school = $_POST['school'];
    $degree = $_POST['degree'];
    $website = $_POST['website'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $start_year = $_POST['start_year'];
    $end_year = $_POST['end_year'];
    $degree_description = $_POST['degree_description'];

    if($start_year > $end_year)
            {
                echo '<script>ErrorMessage()</script>';
                $good = false;
            }
            else
            {
                $good = true;
            }
            if($good == true){

    $sqlinsert = "INSERT INTO education (username,school,degree,website,start_date,end_date,start_year,end_year,degree_description) VALUES ('$username','$school', '$degree', '$website', '$start_date','$end_date', '$start_year', '$end_year', '$degree_description')";

    if(!mysqli_query($dbcon, $sqlinsert))
    {
        die('error inserting new record');
    }
    header('Location: work_history.php');
}
}
    else if(isset($_POST['submit_new']))
{

    $username = $_SESSION['username'];
    $school = $_POST['school'];
    $degree = $_POST['degree'];
    $website = $_POST['website'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $start_year = $_POST['start_year'];
    $end_year = $_POST['end_year'];
    $degree_description = $_POST['degree_description'];

    if($start_year > $end_year)
            {
                echo 'Error Message';
                $good = false;
            }
            else
            {
                $good = true;
            }
            if($good == true){

    $sqlinsert = "INSERT INTO education (username,school,degree,website,start_date,end_date,start_year,end_year,degree_description) VALUES ('$username','$school', '$degree', '$website', '$start_date','$end_date', '$start_year', '$end_year', '$degree_description')";

    if(!mysqli_query($dbcon, $sqlinsert))
    {
        die('error inserting new record');
    }
}
}



?>
<title>CV Education Form</title>
<form method="post" action="education.php">
<input type="hidden" name="submitted" value="true" />

    <legend style="color: #F87F25; font: bold 18px Tahoma;">Education</legend>
    <br/>
    <label style="color: #01ACEE; font: bold 14px Tahoma;">School <input placeholder="University of Sheffield, Department Of Computer Science" type="text" name="school" size="40" required/> </label>
    <br /><br />
    <label style="color: #01ACEE; font: bold 14px Tahoma;">Degree <input placeholder="70" type="text" name="degree" size="20" required/> </label>
    <br /><br />
    <label style="color: #01ACEE; font: bold 14px Tahoma;">Website <input placeholder="www.sheffield.ac.uk/" type="text" size="25" name="website"/> </label>
    <br /><br />
    <label style="color: #01ACEE; font: bold 14px Tahoma;">Start Date</label>
        <select name="start_date">
        <option value="January">January</option>
        <option value="February">February</option>
        <option value="March">March</option>
        <option value="April">April</option>
        <option value="May">May</option>
        <option value="June">June</option>
        <option value="July">July</option>
        <option value="August">August</option>
        <option value="September">September</option>
        <option value="October">October</option>
        <option value="November">Noember</option>
        <option value="December">December</option>
        </select>
    <br /><br />
    <label style="color: #01ACEE; font: bold 14px Tahoma;">End Date</label>
        <select name="end_date">
        <option value="January">January</option>
        <option value="February">February</option>
        <option value="March">March</option>
        <option value="April">April</option>
        <option value="May">May</option>
        <option value="June">June</option>
        <option value="July">July</option>
        <option value="August">August</option>
        <option value="September">September</option>
        <option value="October">October</option>
        <option value="November">Noember</option>
        <option value="December">December</option>
        </select>
    <br /><br />
        <label style="color: #01ACEE; font: bold 14px Tahoma;"> Start Year</label>
        <select name="start_year" >
        <option value="1979">1979</option>
        .
        .
        .
        .
        <option value="2021">2021</option>
        <option value="2022">2022</option>

        </select>
        <label style="color: #01ACEE; font: bold 14px Tahoma;">End Year</label>
        <select name="end_year">
        <option value="1979">1979</option>
        .
        .
        .
        .
        <option value="2021">2021</option>
        <option value="2022">2022</option>
        </select>      
    <br /><br />
<label style="color: #01ACEE; font: bold 14px Tahoma;">Degree Description</label>
    <br />
    <textarea rows="4" cols="40" name="degree_description"></textarea> </label>

<br />
<input type="submit" value="Next" name="submit_next" style="border: 1px solid #006; color:#F87F25; font: bold 16px Tahoma; border-radius:7px; padding:4px;"/>

<input type="submit" value="Add New & Save" name="submit_new" style="border: 1px solid #006; color:#F87F25; font: bold 16px Tahoma; border-radius:7px; padding:4px;"/>

<input type=button onClick="location.href='personal_information.php'" value='Previous' style="border: 1px solid #006; color:#F87F25; font: bold 16px Tahoma; border-radius:7px; padding:4px;">

<input type=button onClick="location.href='work_history.php'" value='Skip' style="border: 1px solid #006; color:#F87F25; font: bold 16px Tahoma; border-radius:7px; padding:4px;">

</form>     

我个人只会使用 javascript 检查字段并显示错误,然后使用 PHP 仅在处理数据之前仔细检查。

查看 javascript 表单验证。

试试这个 - 它有以下优点:

  • 它每年为您节省大量代码循环而不是硬编码选项
  • 表格保留之前选择的任何值
  • 它会在 select

    之后放入您想要的任何错误消息
    <label style="color: #01ACEE; font: bold 14px Tahoma;"> Start Year</label>
    <select name="start_year" >
    <?php 
    for ($i = 1979; $i < 2022; $i++) {
        if ($i == $start_year) 
            $sel = 'selected="selected"';
        else 
            $sel = '';
        echo "<option value=\"$i\" $sel >$i</option>\n";
    }
    ?>
    </select>
    <?php
    if (!$good) echo "<span>yada, yada error message</span>\n";
    ?>
    <label style="color: #01ACEE; font: bold 14px Tahoma;">End Year</label>
    <select name="end_year">
    <?php 
    for ($i = 1979; $i < 2022; $i++) {
        if ($i == $end_year) 
            $sel = 'selected="selected"';
        else 
            $sel = '';
        echo "<option value=\"$i\" $sel >$i</option>\n";
    }
    ?>
    </select>      
    <?php
    if (!$good) echo "<span>yada, yada error message</span>\n";
    ?>