Home > Enterprise >  error to displaying name with messages in php
error to displaying name with messages in php

Time:09-28

**i am writing a script for my project, i am facing a little bit error.

this code display only 'eligible messages', i want to showing user name also in output. pls solved it.**

**<?php
    $name = $_POST['name'];
    $dateOfBirth1 = $_POST['dob'];
    $today1 = date("Y-m-d");
    $diff1 = date_diff(date_create($dateOfBirth1), date_create($today1),);
    if($diff1->format('%y')>= 21){
        header('Content-Type: application/json');
        echo ($name);   
        echo json_encode(array('flag'=>1, 'msg'=> ' You are eligible. Your Age is: '.$diff1->format('%y').' Years, '.$diff1->format('%m').' months ,'.$diff1->format('%d').' days'));
        exit;
    }else{
        header('Content-Type: application/json');
        echo ($name);
        echo json_encode(array('flag'=>1, 'msg'=>' You are not eligible. Your Age is: '.$diff1->format('%y').' Years, '.$diff1->format('%m').' months ,'.$diff1->format('%d').' days'));
        exit;
    }
?>**

CodePudding user response:

 **<!-- begin snippet: js hide: false console: true babel: false -->
    <!-- language: lang-js -->
        $("#loading1").hide();
                // When the browser is ready...
                $(document).ready(function(){
                // Setup form validation on the #signup element
                $("#submit_form1").validate({
                
                 // Specify the validation rules
                 rules: {
                  dob: {
                   required: true,
                   date: true
                  }
                    
                 },
                
                 // Specify the validation error messages
                 messages: {
                
                  dob: {
                   required: "Please select your date of birth",
                   date: "Invalid date"
                  }
                    
                 },
                
                 submitHandler: function (form) {   
                  $("#loading1").show(); 
                $.ajax({
                   type: 'POST',
                   dataType:'JSON',    
                   url: 'calculate-age1.php',    
                   data: $("#submit_form1").serialize(),
                   success: function (json_data) {          
                    var msg = json_data.msg;
                    var flag = json_data.flag;     
                    if (flag == 1) {
                     $("#loading1").hide();
                     $("#message1").html(msg).show();
                     $("#submit_form1");
                    } else {
                     $("#message1").html(msg).show();
                     $("#submit_form1");
                
                    }
                   }
                  });
                 }
                });
                
                });** 
    
   
    

CodePudding user response:

 **<!-- language: lang-html -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Check Eligibility</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.min.js"></script>
      <script src="ajax.js" type="text/javascript"></script>
       <script src="ajax1.js" type="text/javascript"></script>
         <style type="text/css">
            .label {
            width: 100px;
            text-align: right;
            float: left;
            padding-right: 10px;
            }
            #submit_form, #submit_form1, label.error, .output {
            color: #FB3A3A;
            font-family:"Segoe UI";
            font-size:13px;
            margin-top:-10px;
            
            }
         </style>
    </head>
    <body>
    <div >
    <hr>
    <div >
        <div >
      <h5> Eligibility Test (To check your are 21 year old or not)</h5><br>
      <form action="#" id="submit_form1" method="POST" enctype="application/x-www-form-urlencoded">
        <div >
          <label for="name">Enter your Name:</label>
          <input type="text"  id="name" placeholder="Enter your name" name="name" size="25">
          <label for="dob">Enter your DOB:</label>
          <input type="date"  id="dob" placeholder="Enter Date of birth" name="dob" size="10">
        </div>
        <input type="submit"  name="submit_btn" value="Submit">
        <input type="reset"  value="Reset" name="B4">
      </form>
      <div id="loading1" style="display: none;">Calculating...</div>
        <br>
      <div  role="alert" id="message1" style="display: none;">
      </div>
    </div>
    </div>
    </div>
    </body>
    </html>**
  • Related