-1

The page shows all the index and variables as errors.

All the codes looks ok to me but is not working. I have looked around community and checked some solutions but nothing.

This is the code:

    <?php
    $result="";
    if($_POST['submit']){
        if(!$_POST['name']){
             $error="<h3>FORM NOT SUBMITTED (try again)</h3>Please enter your name.";
        };
        if(!$_POST['phone']){
            $error.="<br>Please enter your phone no.";
        };
        if(!$_POST['address']){
            $error.="<br>Please enter your address.";
        };   
        if(!$_POST['pin']){
            $error.="<br>Please enter your Pincode.";
        };
        if(!$_POST['date']){
            $error.="<br>Please enter the Date.";
        };
        if(!$_POST['time']){
            $error.="<br>Please enter the time.";
        };
        if($error){
            $result='<div class="alert alert-danger alert-dismissible">'.$error.'</div>';
        }
        else{

            if ( mail("rhinoaid@gmail.com", "Service Needed", 
                "Name: ".$_POST['name']."

                Phone no: ".$_POST['phone']."

                Address: ".$_POST['address']."

                Pincode: ".$_POST['pin']."

                Date: ".$_POST['date']."

                Time: ".$_POST['time']
            )) {
                $done="<h3>FORM SUBMITTED SUCCESSFULLY</h3>Thank you! RHINOaid will contact you shortly.";
                $result='<div class="alert alert-success">'.$done.'</div>';
            };
        };

    };
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>   

</head>

<body>
    <div class="container-fluid">
         <form method="post" action="">  
            <?php echo $result; ?>
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name" value="<?php echo $_POST['name']; ?>">
            </div>
            <div class="form-group">
                <label for="Phone">Phone:</label>
                <input type="number" class="form-control" id="phone" placeholder="Enter Phone no." name="phone" value="<?php echo $_POST['phone']; ?>">
            </div>
            <div class="form-group">
                <label for="address">Address:</label>
                <input type="text" class="form-control" id="address" placeholder="Enter Address/Venue" name="address" value="<?php echo $_POST['address']; ?>">
            </div>
            <div class="form-group">
                <label for="pin">Pincode:</label>
                <input type="number" class="form-control" id="pin" placeholder="Enter Pincode" name="pin" value="<?php echo $_POST['pin']; ?>">
            </div>
            <div class="form-group">
                <label for="date">Date:</label>
                <input type="date" class="form-control" id="date" placeholder="dd/mm/yy" name="date" value="<?php echo $_POST['date']; ?>">
            </div>
            <div class="radio">
                <label><input type="radio" name="time" value="From 9am to 12pm">From 9am to 12pm</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="time" value="From 12pm to 5pm">From 12pm to 5pm</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="time" value="From 5pm to 9pm">From 5pm to 9pm</label>
            </div> 
            <input type="submit" name="submit" class="btn btn-success btn-sm" value="submit">
            <small>By submitting you agree to our <a href="tc.html">terms</a> and <a href="policy.html">policy</a></small>
        </form>
    </div>
 </body>

</html>
Mr.Web
  • 6,992
  • 8
  • 51
  • 86
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Niklesh Raut Sep 29 '17 at 06:49

1 Answers1

0

check isset() on submit and all other Post

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

}

As per your code

 <?php
    $result="";
    $error='';
    if(isset($_POST['submit'])){
        if(!isset($_POST['name'])){
             $error="<h3>FORM NOT SUBMITTED (try again)</h3>Please enter your name.";
        };
        if(!isset($_POST['phone'])){
            $error.="<br>Please enter your phone no.";
        };
        if(!isset($_POST['address'])){
            $error.="<br>Please enter your address.";
        };   
        if(!isset($_POST['pin'])){
            $error.="<br>Please enter your Pincode.";
        };
        if(!isset($_POST['date'])){
            $error.="<br>Please enter the Date.";
        };
        if(!isset($_POST['time'])){
            $error.="<br>Please enter the time.";
        };
        if(isset($error)&!empty($error)){
            $result='<div class="alert alert-danger alert-dismissible">'.$error.'</div>';
        }
        else{

            if ( mail("rhinoaid@gmail.com", "Service Needed", 
                "Name: ".$_POST['name']."

                Phone no: ".$_POST['phone']."

                Address: ".$_POST['address']."

                Pincode: ".$_POST['pin']."

                Date: ".$_POST['date']."

                Time: ".$_POST['time']
            )) {
                $done="<h3>FORM SUBMITTED SUCCESSFULLY</h3>Thank you! RHINOaid will contact you shortly.";
                $result='<div class="alert alert-success">'.$done.'</div>';
            };
        };

    };
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>   

</head>

<body>
    <div class="container-fluid">
         <form method="post" action="">  
            <?php echo $result; ?>
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name" value="<?php echo isset($_POST['name'])?$_POST['name']:''; ?>">
            </div>
            <div class="form-group">
                <label for="Phone">Phone:</label>
                <input type="number" class="form-control" id="phone" placeholder="Enter Phone no." name="phone" value="<?php echo isset($_POST['phone'])?$_POST['phone']:''; ?>">
            </div>
            <div class="form-group">
                <label for="address">Address:</label>
                <input type="text" class="form-control" id="address" placeholder="Enter Address/Venue" name="address" value="<?php echo isset($_POST['address'])?$_POST['address']:''; ?>">
            </div>
            <div class="form-group">
                <label for="pin">Pincode:</label>
                <input type="number" class="form-control" id="pin" placeholder="Enter Pincode" name="pin" value="<?php echo isset($_POST['pin'])?$_POST['pin']:''; ?>">
            </div>
            <div class="form-group">
                <label for="date">Date:</label>
                <input type="date" class="form-control" id="date" placeholder="dd/mm/yy" name="date" value="<?php echo isset($_POST['date'])?$_POST['date']:''; ?>">
            </div>
            <div class="radio">
                <label><input type="radio" name="time" value="From 9am to 12pm">From 9am to 12pm</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="time" value="From 12pm to 5pm">From 12pm to 5pm</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="time" value="From 5pm to 9pm">From 5pm to 9pm</label>
            </div> 
            <input type="submit" name="submit" class="btn btn-success btn-sm" value="submit">
            <small>By submitting you agree to our <a href="tc.html">terms</a> and <a href="policy.html">policy</a></small>
        </form>
    </div>
 </body>

</html>

for mail error check this question [PHP Warning: mail(): " sendmail_from" not set in php.ini or custom "From:" header missing

Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
  • thanks. tried. but now all the validations for the inputs (except radio inputs) in the form are not functioning. even the mail function isn't working. again after submission an undefined index message shows up : 'Notice: Undefined variable: error in C:\xampp\htdocs\New folder\frm1.php on line 20' – user8663816 Sep 29 '17 at 07:07
  • use isset to check – Vision Coderz Sep 29 '17 at 07:16