-1

Validate function

function validate(add_app_form){
    var valid = true;
    var userTxt = document.getElementById("patient_name").value;
    var dateTxt = document.getElementById("app_date").value;
    var timeTxt = document.getElementById("app_time").value;
    var oldName = document.getElementById("select_old").value;
    if(userTxt == "" && dateTxt == "" && timeTxt == "" && oldName == "choose")
    {
      //$("#lblTxt").text("Username and Password are required!");
      $('#patient_name').css('border-color', 'red');
      $('#app_date').css('border-color', 'red');
      $('#app_time').css('border-color', 'red');
      $('#select_old').css('border-color', 'red');
      $("#add_app_lbl").text("Please Fill all the form");
      valid = false;
    }
    if(userTxt == "" && oldName == "choose")
    {
      $('#patient_name').css('border-color', 'red');
      $("#add_app_lbl").text("Please Add Patient Name Or select an old patient");
      valid = false;
    }
    if(dateTxt == "")
    {
      $('#app_date').css('border-color', 'red');
      $("#add_app_lbl").text("Please Add a Date");
      valid = false;
    }
    return valid;
}

EDITED CODE

<?php
    //Set error reporting on
    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    //Include connection file
    require_once('../include/global.php');


    $user = $_SESSION['username'];
    $id_logged = $_SESSION['login_id'];

    if(isset($_POST['add_app_btn'])){
    //Values From AJAX
        $patient_name = $_POST['patient_name'];
        $date_app = $_POST['app_date'];
        $time_app = $_POST['app_time'];
        $reason = $_POST['app_reason'];
        $old_patient_id = $_POST['select_old'];

        //If new patient
        if($patient_name == "" && $old_patient_id != "choose")
        {
            try{    
        //See if date and time exist
                $appExist = "SELECT * FROM appointment WHERE id_logged = :id_logged AND date_app = :date_app and time_app = : time_app";
                $appExistStmt = $conn->prepare($appExist);
                $appExistStmt->bindValue(":id_logged", $id_logged);
                $appExistStmt->bindValue(":date_app", $date_app);
                $appExistStmt->bindValue(":time_app", $time_app);
                $appExistStmt->execute();
                $appExistStmtCount = $appExistStmt->rowCount();

                if($appExistStmtCount == 0)
                    {
                        //Add to appointment table
                        $appAdd = "INSERT INTO appointment(id_logged, patient_id, date_app, time_app, reason)
                                   VALUES(:id_logged, :patient_id, :date_app, :time_app, :reason)";
                        $appAddStmt = $conn->prepare($appAdd);
                        $appAddStmt->bindValue(":id_logged", $id_logged);
                        $appAddStmt->bindValue(":patient_id", $old_patient_id);
                        $appAddStmt->bindValue(":date_app", $date_app);
                        $appAddStmt->bindValue(":time_app", $time_app);
                        $appAddStmt->bindValue(":reason", $reason);

                        $appAddStmt->execute();

                        echo "added";
                    }
                    else
                    {
                        echo "not added";
                        header("Location: add_appoint.php");
                    }
                }

            catch(PDOException $m)
            {
                $m->getMessage();
                echo "error";
                header("Location: add_app_btnoint.php");
            }
    }
    }
    ?>

EDITED CODE 2

<form class="form-horizontal" id="add_app_form" method="post" action="add_appoint.php" onSubmit="return validate(this);">
                  <div class="box-body">
                    <div class="form-group">
                      <label for="patient_name" class="col-sm-3 control-label">Old Patient</label>
                      <div class="col-sm-4">
                        <select id="select_old" name="select_old">
                          <option value="choose">Choose Name</option>
                          <?php foreach($name_array as $na) { ?>
                          <option value="<?php echo $na['id'] ?>"><?php echo $na['patient_name'] ?></option>
                          <?php } ?>
                        </select>
                      </div>
                      <label for="patient_name" class="col-sm-1 control-label">New</label>
                      <div class="col-sm-4">
                        <input type="text" class="form-control" id="patient_name" name="patient_name" placeholder="New Patient Name">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_date" class="col-sm-2 control-label">Date</label>
                      <div class="col-sm-4">
                        <input type="date" class="form-control" id="app_date" name="app_date">
                      </div>
                      <label for="app_time" class="col-sm-2 control-label">Time</label>
                      <div class="col-sm-4">
                        <input type="time" class="form-control" id="app_time" name="app_time">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_reason" class="col-sm-2 control-label">Reason</label>
                      <div class="col-sm-10">
                        <textarea class="form-control" id="app_reason" name="app_reason" placeholder="Reason"></textarea>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="box-footer">
                    <button type="submit" id="add_app_btn" name="add_app_btn" class="btn btn-success pull-right">Add Appointment</button>
                  </div><!-- /.box-footer -->
                </form>

I have a php code that take values from a form and add them into MySQL database. First part of the PHP code, see if the admin choose an already exist patient from drop list, then add a date and time of an appointment with a reason.

Then values are posted into PHP code where we see if we have already an appointment in those date and time. If not ($appExistStmtCount == 0) then go and insert an appointment.

The problem is that nothing added to database and can't see any PHP errors echoed.

Here is the PHP code:

<?php
    //Set error reporting on
    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    //Include connection file
    require_once('../include/global.php');


    $user = $_SESSION['username'];
    $id_logged = $_SESSION['login_id'];

    if(isset($_POST['add_app_btn'])){
    //Values From AJAX
        $patient_name = $_POST['patient_name'];
        $date_app = $_POST['app_date'];
        $time_app = $_POST['app_time'];
        $reason = $_POST['app_reason'];
        $old_patient_id = $_POST['select_old'];

        //If new patient
        if($patient_name == "" && $old_patient_id != "choose")
        {
            try{    
        //See if date and time exist
                $appExist = "SELECT * FROM appointment WHERE id_logged = :id_logged AND date_app = :date_app and time_app = : time_app";
                $appExistStmt = $conn->prepare($appExist);
                $appExistStmt->bindValue(":id_logged", $id_logged);
                $appExistStmt->bindValue(":date_app", $date_app);
                $appExistStmt->bindValue(":time_app", $time_app);
                $appExistStmt->execute();
                $appExistStmtCount = $appExistStmt->rowCount();

                if($appExistStmtCount == 0)
                    {
                        //Add to appointment table
                        $appAdd = "INSERT INTO appointment(id_logged, patient_id, date_app, time_app, reason)
                                   VALUES(:id_logged, :patient_id, :date_app, :time_app, :reason)";
                        $appAddStmt = $conn->prepare($appAdd);
                        $appAddStmt->bindValue(":id_logged", $id_logged);
                        $appAddStmt->bindValue(":patient_id", $old_patient_id);
                        $appAddStmt->bindValue(":date_app", $date_app);
                        $appAddStmt->bindValue(":time_app", $time_app);
                        $appAddStmt->bindValue(":reason", $reason);

                        $appAddStmt->execute();

                        echo "added";
                    }
                    else
                    {
                        echo "not added";
                        header("Location: add_appoint.php");
                    }
                }

            catch(PDOException $m)
            {
                $m->getMessage();
                echo "error";
                header("Location: add_app_btnoint.php");
            }
    }
    }
    ?>

And here the HTML form:

<form class="form-horizontal" id="add_app_form" onSubmit="return validate(this);">
                  <div class="box-body">
                    <div class="form-group">
                      <label for="patient_name" class="col-sm-3 control-label">Old Patient</label>
                      <div class="col-sm-4">
                        <select id="select_old" name="select_old">
                          <option value="choose">Choose Name</option>
                          <?php foreach($name_array as $na) { ?>
                          <option value="<?php echo $na['id'] ?>"><?php echo $na['patient_name'] ?></option>
                          <?php } ?>
                        </select>
                      </div>
                      <label for="patient_name" class="col-sm-1 control-label">New</label>
                      <div class="col-sm-4">
                        <input type="text" class="form-control" id="patient_name" name="patient_name" placeholder="New Patient Name">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_date" class="col-sm-2 control-label">Date</label>
                      <div class="col-sm-4">
                        <input type="date" class="form-control" id="app_date" name="app_date">
                      </div>
                      <label for="app_time" class="col-sm-2 control-label">Time</label>
                      <div class="col-sm-4">
                        <input type="time" class="form-control" id="app_time" name="app_time">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_reason" class="col-sm-2 control-label">Reason</label>
                      <div class="col-sm-10">
                        <textarea class="form-control" id="app_reason" name="app_reason" placeholder="Reason"></textarea>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="box-footer">
                    <button type="submi;" id="add_app_btn" class="btn btn-success pull-right">Add Appointment</button>
                  </div><!-- /.box-footer -->
                </form>

PS

Values can be seen in the URL but the page just refresh and nothing added

androidnation
  • 556
  • 1
  • 4
  • 17

1 Answers1

1

Your form has no method, so it's passing data through get. You need to add method="post" to your form.

Edit. As @u_mulder mentioned, you need to add name attribute to your button for the check in your php if the button is clicked.

Vasil Shaddix
  • 1,778
  • 12
  • 27