0

There are total fourteen field in my code. All are text and numeric fields.Below code is the form validation code using php. I have checked where each field is empty, or wrongly filled(invalid data). But it is not validating the form. After clicking on submit button an empty page is appearing but the url i.e.,http://localhost/index.php remains same. I am unable to find any error. Please help me. I am new to coding.My code is a bit longer.

<?php

/*$pname = $tname = $oponname = $gameid = $ename = $facility = $e_c_state = "";
$pnum  = $pos = $height = $division =$per = 0;*/

if (isset($_POST['btnsubmit']))
{
    $pname = test_input($_POST['pname']);
    $pnum = test_input($_POST['pnum']);
    $tname = test_input($_POST['tname']);
    $pos = test_input($_POST['pos']);
    $height = test_input($_POST['height']);
    $oponname = test_input($_POST['opponname']);
    $gameid = test_input($_POST['gid']);
    $ename = test_input($_POST['ename']);
    $division = test_input($_POST['division']);
    $facility = test_input($_POST['facility']);
    $e_c_state = test_input($_POST['event_city_state']);

    $date = date('Y-m-d', strtotime($_POST['gdate']));
    $time = $_POST['tip_off_time'];
    $per = test_input($_POST['per']);


    function test_input($data) {
        $data = trim($data);
      //  $data = stripslashes($data);
     //   $data = htmlspecialchars($data);
        return $data;
    }

    if($pname == "") {
        $errorMsg =  "error : Please enter player name.";
        $code = "1" ;
    }
    
    elseif (!preg_match("/^[a-zA-Z]+(?:(?:\. |[' ])[a-zA-Z]+)*$/", $pname)) {
        $errorMsg =  "error : Please enter valid player name."; 
        $code = "1" ;
    }
    elseif ( strlen ( $pname ) <3 && strlen ( $pname ) > 30 ){
        $errorMsg =  "error : Player name must be between 3 to 30 characters.";  
        $code = "1" ; 
    } 

    elseif ( empty($pnum) ) {
        $errorMsg = "error : Player number can not be empty.";
        $code = "2" ;
    }

    elseif ( $tname == "") {
        $errorMsg = "error : Team name can not be empty.";
        $code = "3" ;
    }

    elseif ( !preg_match("/^[a-zA-Z]*$/",$tname)) {
        $errorMsg = "error : Plaese enter valid team name.";
        $code = "3" ;
    }

    elseif ( strlen ( $tname ) <3 && strlen ( $tname ) > 30 ) {
        $errorMsg = "error : Team name must be between 3 to 30 characters.";
        $code = "3" ;
    }

    elseif ( empty($pos) ) {
        $errorMsg = "error :  Position can not be empty.";
        $code = "4" ;
    }

    elseif ( empty($height) ) {
        $errorMsg = "error : Height can not be empty.";
        $code = "5" ;
    } 

    elseif ( $oponname == "" ) {
        $errorMsg = "error : Opponent name can not be empty.";
        $code = "6" ;
    }

    elseif ( !preg_match("/^[a-zA-Z]+(?:(?:\. |[' ])[a-zA-Z]+)*$/", $oponname) ) {
        $errorMsg = "error : Please enter valid opponent name.";
        $code = "6" ;
    }

    elseif (strlen ( $oponname ) <3 && strlen ( $oponname ) > 30 ) {
        $errorMsg = "error : Opponent name must be 3 to 30 characters.";
        $code = "6" ;
    }

    elseif ( empty($gameid) ) {
        $errorMsg = "error : Game id can not be empty.";
        $code = "7" ;
    }

    elseif ( !preg_match("/^[a-zA-Z0-9]+$/", $gameid) ) {
        $errorMsg = "error : Please enter valid game id.";
        $code = "7" ;
    }

    elseif ( $ename == "") {
        $errorMsg = "error : Event name can not be empty.";
        $code = "8" ;
    }

    elseif ( !preg_match("/^[a-zA-Z]*$/",$ename)) {
        $errorMsg = "error : Please enter valid event name.";
        $code = "8" ;
    }
    elseif ( strlen ( $ename ) <3 && strlen ( $ename ) > 20 ) {
        $errorMsg = "error : Event name must be between 3 to 20 characters.";
        $code = "8" ;
    }

    elseif ( empty($division) ) { 
        $errorMsg = "error : Division can not be empty.";
        $code = "9" ;
    }

    elseif ( $facility == "") {
        $errorMsg = "error : Facility can not be empty.";
        $code = "10" ;
    }

    elseif ( !preg_match("/^[a-zA-Z]*$/",$facility) ) {
        $errorMsg = "error : Please enter valid facility.";
        $code = "10" ;
    }

    elseif ( strlen ( $facility ) <3 && strlen ( $facility ) > 20 ) {
        $errorMsg = "error : Facility must be between 3 to 20 characters.";
        $code = "10" ;
    }

    elseif ( $e_c_state == " ") {
        $errorMsg = "error : Event city state can not be empty.";
        $code = "11" ;
    }

    elseif ( !preg_match("/^[a-zA-Z]*$/",$e_c_state)) {
        $errorMsg = "error : Please enter valid event city state.";
        $code = "11" ;
    }

    elseif ( strlen ( $e_c_state ) <3 && strlen ( $e_c_state ) > 30) {
        $errorMsg = "error : Event city state must be between 3 to 30 characters.";
        $code = "11" ;
    }

    elseif ( empty($per)) {
        $errorMsg = "error : PER can not be empty.";
        $code = "12" ;
    }
    else{
        echo "success";
        // Mysql insert statement
        // After successful form submission, it will be redirect to thankyou.php file.
       // header("Location: thankyou.php");
    }
}    

    


?>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title> This is leaderboard data entry page </title>
        <link rel="shortcut icon" href="#">
        <link rel="stylesheet" href="leaderboard.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
        <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js" defer></script>
        <style>
        .errorMsg{border:1px solid red; }
        .message{color: red; font-weight:bold; }
        </style>
    </head>
    
<body bgcolor="#E0FFFF">  
    <h2> Please Enter Game Details</h2>  
    <?php if (isset($errorMsg)) { echo "<p class='message'>" .$errorMsg. "</p>" ;} ?>
<form name = "playInfo" id = "playInfo" action = "" method = "POST">

    <div class = "formField">
        <label for="pname"> Player Name: </label>
        <input type="text" id="pname" name="pname" placeholder = "First name Last name" value = "<?php if(isset($pname)){echo $pname;} ?>"
        <?php if(isset($code) && $code == 1){echo "class = errorMsg" ;} ?> >
        <small></small>
        
        <br>
    </div>
        
    <div class = "formField">
        <label for="pnum"> Player Number: </label>
        <input type="number" id="pnum" name="pnum" value=""
        <?php if(isset($code) && $code == 2){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="tname"> Team Name: </label>
        <input type="text" id="tname" name="tname" value="" placeholder = "Eg. Knight Riders"
        <?php if(isset($code) && $code == 3){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="position"> Position: </label>
        <input type="number" id="posn" name="pos" value=""
        <?php if(isset($code) && $code == 4){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="height"> Height: </label>
        <input type="number" id="height" name="height" value=""
        <?php if(isset($code) && $code == 5){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="oponname"> Opponent Name: </label>
        <input type="text" id="oponname" name="opponname" value="" placeholder ="First name Last name "
        <?php if(isset($code) && $code == 6){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>
    
    <div class = "formField">
        <label for="gameid"> Game Id: </label>
        <input type="text" id="gid" name="gid" value="" placeholder = "Eg. G5"
        <?php if(isset($code) && $code == 7){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="ename"> Event Name: </label>
        <input type="text" id="ename" name="ename" value="" placeholder = "Eg. Badminton"
        <?php if(isset($code) && $code == 8){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="division"> Division: </label>
        <input type="number" id="division" name="division" value=""
        <?php if(isset($code) && $code == 9){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="facility"> Facility: </label>
        <input type="text" id="facility" name="facility" valur="" placeholder = "Eg. Eden Garden"
        <?php if(isset($code) && $code == 10){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="event-city-state"> Event City State: </label>
        <input type="text" id="e-c-state" name="event_city_state" value="" placeholder = "Eg. West Bengal"
        <?php if(isset($code) && $code == 11){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="date"> Date: </label>
        <input type="date" id="date" name="gdate" value=""
        <?php if(isset($code) && $code == 12){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

    <div class = "formField">
        <label for="tip-time"> Tipoff Time: </label>
        <input type="time" id="time" name="tip_off_time" value=""
        <?php if(isset($code) && $code == 13){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>   
    
    <div class = "formField">
        <label for="per"> PER: </label>
        <input type="number" id="per" name="per" value=""
        <?php if(isset($code) && $code == 14){echo "class = errorMsg" ;} ?> >
        <small></small>
        <br>
    </div>

   <!-- <input type="button" value="Back" onclicl="hostory.back()"> -->

    <!--!<button type = "reset" value = "Reset"> Back </button> -->
    
  <!--!  <div class = "formField"> -->
        <input type="submit" name = "btnsubmit" id="sub" value="Next"> <br><br>
  <!--      <small></small>
    </div> -->
</form>

<!--<script src = "validateupdate.js" defer></script> --> <!--client side validation -->

</body>
</html>
tahira
  • 1
  • 1
  • Have you tried showing errors? [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) Is there anything in your servers error log? "_but the url i.e.,http://localhost/index.php remains same_" Why should it not, you haven't set an action on your form? – brombeer Oct 26 '21 at 18:41
  • Thank you dear for helping me. I have rectified my mistake. Your specified thread was helpful for me. I am very happy. – tahira Oct 27 '21 at 05:45

0 Answers0