单击提交按钮后,使文本框将其属性更改为只读

时间:2016-11-08 21:29:28

标签: javascript php validation textbox readonly

点击提交按钮后,我想将我的textbox student_number的属性更改为readonly(查找) 有任何建议。我是否需要在isset函数(搜索)之后立即在php代码中插入脚本?

     <style>
        .login{
        width: 500px;
        margin: 50px auto;
        font: Georgia, "Times New Roman", Times, serif;
        border-radius: 10px;
    border: 2px solid #ccc;
    padding: 15px 50px 25px;
    margin-top: 60px;
    margin-left: -370px;
    width: 1000px;
}

body {
    background-color:#0CF;

}
</style>



<link href="css/admin.css" rel="stylesheet" type="text/css" />


<?php
session_start();
if (!isset($_SESSION["sess_user"])) {
    header("Location: login.php");
} else {
    include 'dbconnection.php'; 

    $id             = "";
    $first_name     = "";
    $middle_name    = "";
    $last_name      = "";
    $student_number = "";
    $program        = "";
    $year_level     = "";
    $faculty_name   = "";
    $reported_by    = "";
    $violation_1    = "";
    $violation_2    = "";
    $violation_3    = "";

    // Search
    if (isset($_POST['search'])) {
        $data = mysqli_real_escape_string($con, $_POST['student_number']);

        $search_Query = "SELECT * FROM register WHERE student_number = '$data'";

        $search_Result = mysqli_query($con, $search_Query);

        if (mysqli_num_rows($search_Result) > 0) {
            while ($row = mysqli_fetch_array($search_Result)) {
                $id             = $row['id'];
                $first_name     = $row['first_name'];
                $middle_name    = $row['middle_name'];
                $last_name      = $row['last_name'];
                $student_number = $row['student_number'];
                $program        = $row['program'];
                $year_level     = $row['year_level'];
                $faculty_name   = $row['faculty_name'];
                $reported_by    = $row['reported_by'];
                $violation_1    = $row['violation_1'];
                $violation_2    = $row['violation_2'];
                $violation_3    = $row['violation_3'];
            }
        } else {
            echo 'No record found with this Student Number.';
        }
    }

    // Insert
    if (isset($_POST['insert'])) {
        $data     = array();
        $data[1]  = mysqli_real_escape_string($con, $_POST['id']);
        $data[2]  = mysqli_real_escape_string($con, $_POST['first_name']);
        $data[3]  = mysqli_real_escape_string($con, $_POST['middle_name']);
        $data[4]  = mysqli_real_escape_string($con, $_POST['last_name']);
        $data[5]  = mysqli_real_escape_string($con, $_POST['student_number']);
        $data[6]  = mysqli_real_escape_string($con, $_POST['program']);
        $data[7]  = mysqli_real_escape_string($con, $_POST['year_level']);
        $data[8]  = mysqli_real_escape_string($con, $_POST['faculty_name']);
        $data[9]  = mysqli_real_escape_string($con, $_POST['reported_by']);
        $data[10] = mysqli_real_escape_string($con, $_POST['violation_1']);
        $data[11] = mysqli_real_escape_string($con, $_POST['violation_2']);
        $data[12] = mysqli_real_escape_string($con, $_POST['violation_3']);

        $query         = "SELECT student_number FROM register WHERE student_number = '$data[5]'";
        $search_Result = mysqli_query($con, $query);
        if (mysqli_num_rows($search_Result) > 0) {
            echo 'student number taken';
        } else {
            $insert_Query = "INSERT INTO `register` (
                    `first_name`, 
                    `middle_name`, 
                    `last_name`, 
                    `student_number`, 
                    `program`, 
                    `year_level`, 
                    `faculty_name`, 
                    `reported_by`, 
                    `violation_1`, 
                    `violation_2`, 
                    `violation_3`
                ) VALUES (
                    '$data[2]', 
                    '$data[3]',
                    '$data[4]',
                    '$data[5]',
                    '$data[6]',
                    '$data[7]',
                    '$data[8]',
                    '$data[9]',
                    '$data[10]',
                    '$data[11]',
                    '$data[12]'
            )";

            if (mysqli_query($con, $insert_Query)) {
                echo 'Record was added successfully.';
            } else {
                echo 'Adding of record failed.';
            }
        }
    }

    // Edit
    if (isset($_POST['update'])) {
        $data     = array();
        $data[1]  = mysqli_real_escape_string($con, $_POST['id']);
        $data[2]  = mysqli_real_escape_string($con, $_POST['first_name']);
        $data[3]  = mysqli_real_escape_string($con, $_POST['middle_name']);
        $data[4]  = mysqli_real_escape_string($con, $_POST['last_name']);
        $data[5]  = mysqli_real_escape_string($con, $_POST['student_number']);
        $data[6]  = mysqli_real_escape_string($con, $_POST['program']);
        $data[7]  = mysqli_real_escape_string($con, $_POST['year_level']);
        $data[8]  = mysqli_real_escape_string($con, $_POST['faculty_name']);
        $data[9]  = mysqli_real_escape_string($con, $_POST['reported_by']);
        $data[10] = mysqli_real_escape_string($con, $_POST['violation_1']);
        $data[11] = mysqli_real_escape_string($con, $_POST['violation_2']);
        $data[12] = mysqli_real_escape_string($con, $_POST['violation_3']);

        $update_Query = "UPDATE `register` SET 
            `first_name`='$data[2]', 
            `middle_name`='$data[3]', 
            `last_name`='$data[4]', 
            `student_number`='$data[5]', 
            `program`='$data[6]', 
            `year_level`='$data[7]', 
            `faculty_name`='$data[8]', 
            `reported_by`='$data[9]', 
            `violation_1`='$data[10]', 
            `violation_2`='$data[11]', 
            `violation_3`='$data[12]' 
            WHERE `id` = '$data[1]'";

        if (mysqli_query($con, $update_Query)) {
            echo 'Record was updated successfully.';
        } else {
            echo 'Updating of record failed.';
        }
    }

    if(isset($_POST["homeBtn"])){
        header("Location:admin.php");

    }
    ?>

    <!DOCTYPE Html>
    <html>
        <head>

            <title>Cant decide a title</title>
        </head>
        <h3>WELCOME</h3><h3><?=$_SESSION['sess_user'];?>!</h3><a href="logout.php">logout</a>

        <body>

        <div id="form">
        <div class="login">
        <H1>SECOND VIOLATION</H1>
            <form action="violation_2.php" method="post">

                <input type="hidden" name="id" placeholder="Id" value="<?php echo $id; ?>"><br><br>
                <label>*Student Number:</label><br><input type="text" id="student_number" name="student_number" placeholder="Student Number" value="<?php echo $student_number; ?>"><br><br><br><br><br><br>

               <label>First Name:</label><br> <input readonly type="text" id="first_name" name="first_name" placeholder="First Name" value="<?php echo $first_name; ?>"><br><br>
               <label>Middle Name:</label><br><input readonly type="text" id="middle_name" name="middle_name" placeholder="Middle Name" value="<?php echo $middle_name; ?>"><br><br>
                 <label>Last Name:</label><br><input readonly type="text" id="last_name" name="last_name" placeholder="Last Name" value="<?php echo $last_name; ?>"><br><br>

                 <label>Program:</label><br><input readonly type="text" id="program" name="program" placeholder="Program" value="<?php echo $program; ?>"><br><br>
                 <label>Year Level:</label><br><input readonly type="text" id="year_level" name="year_level" placeholder="Year Level" value="<?php echo $year_level; ?>"><br><br>
                 <label>Faculty Name:</label><br><input readonly type="text" id="faculty_name" name="faculty_name" placeholder="Faculty Name" value="<?php echo $faculty_name; ?>"><br><br>
                 <label>Reported By:</label><br><input readonly type="text" id="reported_by" name="reported_by" placeholder="Reported By" value="<?php echo $reported_by; ?>"><br><br>
                 <label>First Violation:</label><br><input readonly type="text" id="violation_1" name="violation_1" placeholder ="First Violation" value="<?php echo $violation_1 ?>"><br><br>
                 <label>Second Violation:</label><br><input readonly type="text" id="violation_2" name="violation_2" placeholder ="Second Violation" value="<?php echo $violation_2 ?>"><br><br>
                 <label>Third Violation:</label><br><input readonly type="text" id="violation_3" name="violation_3" placeholder ="Third Violation" value="<?php echo $violation_3 ?>"><br><br>



                <p>
                    <label>
                    <input type="radio" name="violation_2" value="Not in uniform or improper wearing of uniform" id="RadioGroup1_0">
                    Not in uniform or improper wearing of uniform</label>
                    <br>
                    <label>
                    <input type="radio" name="violation_2" value="Wearing of earrings" id="RadioGroup1_1">
                    Wearing of earrings(Male)</label>
                    <br>
                    <input type="radio" name="violation_2" value="Loitering during class hours" id="RadioGroup1_1">
                    Loitering during class hours</label><br>

                    <input type="radio" name="violation_2" value="Playing of games inside the computer laboratory room" id="RadioGroup1_1">
                    Playing of games inside the computer laboratory room</label><br>

                    <input type="radio" name="violation_2" value="Un-authorized access to sites like Youtube, Facebook and other social media sites" id="RadioGroup1_1">
                    Un-authorized access to sites like Youtube, Facebook and other social media sites</label><br>

                    <input type="radio" name="violation_2" value="Downloading of files not duly authorized by the faculty concerned" id="RadioGroup1_1">
                    Downloading of files not duly authorized by the faculty concerned</label><br>

                    <input type="radio" name="violation_2" value="Tampering, theft or vandalizing the computer units" id="RadioGroup1_1">
                    Tampering, theft or vandalizing the computer units</label><br>

                    <input type="radio" name="violation_2" value="Conduct unbecoming of a CCS student" id="RadioGroup1_1">
                    Conduct unbecoming of a CCS student</label><br>

                    <input type="radio" name="violation_2" value="Not wearing of ID" id="RadioGroup1_1">
                    Not wearing of ID</label><br>

                    <input type="radio" name="violation_2" value="Sporting long hair" id="RadioGroup1_1">
                    Sporting long hair</label><br>

                    <input type="radio" name="violation_2" value="Littering" id="RadioGroup1_1">
                    Littering</label><br>

                    <input type="radio" name="violation_2" value="Un-authorized stay inside a lecture or laboratory room during vacant periods" id="RadioGroup1_1">
                    Un-authorized stay inside a lecture or laboratory room during vacant periods</label><br>
                </p>


                    <input type="submit" name="update" value="Add Violation ">
                      <script>
function changeText(){
   var text_box = document.getElementById('student_number');


        text_box.setAttribute('readonly','readonly');   

}
</script>
              <input type="submit" name="search" value="Find" onClick="changeText();">

                    <input type="submit" name="homeBtn" value="Home">


            </form>
          </div>
        </div>
        </body>
    </html>
<?php
}
?>

1 个答案:

答案 0 :(得分:0)

document.getElementById("student_number").disabled = true;