0

When I click on Submit button nothing happens neither it redirects nor the data in html form is posted to database.

Here is my code for date and time and location:

<?php
session_start();
$date1="";
$time1="";
$location="";
$db=mysqli_connect("localhost","root","","registration");
if(isset($_POST['submit'])) {
    $date1 = mysqli_real_escape_string($db, $_POST['date1']);
    $time1 = mysqli_real_escape_string($db, $_POST['time1']);
    $location = mysqli_real_escape_string($db, $_POST['location']);


    if (!empty($date)) {
        $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
        mysqli_query($db, $query);
        header('location:tariff.php');
    }
}


?>

In this HTML sample the button name is Submit, when it is clicked the PHP is set to True and the code should be executed, but there are some errors.

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


    <style>
        .form-inline{
            text-align: center;
            position: absolute;
            top: 100px;
            left: 500px;

        }
        .btn{
            position: absolute;
            top: auto;
            left: 500px;
        }
    </style>
</head>
<body>
<div class="container"
     <div class="panel panel-default"
<form class="form-inline" method="post" action="bookcar.php">
    <div class="panel-body">
    <p><label> Pick a date:</label><input class="form-control" type="date" name="date1" placeholder="Date"></p>
    </div>
    <div class="panel-body">
    <p><label> Pick a Time:</label><input class="form-control" type="time" name="time1" placeholder="Time"></p>
    </div>
    <div class="panel-body">
        <p><label> Pick a Location:</label><input class="form-control" type="text" name="location" placeholder="Location"></p>
    </div>
    <input type="submit" name="submit" value="submit" class="btn btn-lg btn-primary">

</form>
</div>
</div>
</body>
</html>
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • 1
    Possible duplicate of [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Progman Jul 14 '18 at 16:00
  • yes because you have not defined $date variable if (!empty($date)) { $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')"; mysqli_query($db, $query); header('location:tariff.php'); } it should be if(!empty($date1)) instead of if(!empty($date)) – Salman Zafar Jul 15 '18 at 08:46

3 Answers3

1
 session_start();

 $db=mysqli_connect("localhost","root","","registration");

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

     $date1 = mysqli_real_escape_string($db, $_POST['date1']);

 }

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

     $time1 = mysqli_real_escape_string($db, $_POST['time1']);

 }

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

     $location = mysqli_real_escape_string($db, $_POST['location']);

 }

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

     if (!empty($date1)){

         $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
         mysqli_query($db, $query);
         header('location:tariff.php');
         exit(); // Quit current page and go to tariff.php

     }
 } 
hans-könig
  • 553
  • 8
  • 10
0

There is minor error in your code. Replace your code with the below code.

if (!empty($date1)) {
        $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
        mysqli_query($db, $query);
        header('location:tariff.php');
    }

You have not define $date variable in your code and in if condition you are using $date variable to check if it is empty or not.

Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
0

First make sure that the bookcar.php is in the same location as the html file
NOTE: They should be inside the same folder.
Change your bookcar.php file to this

<?php
session_start();
$date1="";
$time1="";
$location="";
$db=mysqli_connect("localhost","root","","registration");
if(isset($_POST['submit'])) {
    $date1 = mysqli_real_escape_string($db, $_POST['date1']);
    $time1 = mysqli_real_escape_string($db, $_POST['time1']);
    $location = mysqli_real_escape_string($db, $_POST['location']);


    if (!empty($date1)) {
        $query = "INSERT INTO cars(date1,time1,location) VALUES('$date1', '$time1', '$location')";
        mysqli_query($db, $query);
        header('location:tariff.php');
    }
}
?>
j.edgar
  • 13
  • 7