Click here to Skip to main content
15,216,855 members
Rate this:
Please Sign up or sign in to vote.
See more:
Basically what happens is anytime I try to submit the form nothing gets posted into my DB. I'm not sure what is wrong with my code.
<?php
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  ini_set("error_log",dirname(__FILE__).'/errorlog.txt');
  session_start();

  if(isset($_POST['submit'])){
    $conn = new mysqli('localhost','root','','employeeTS');

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


    $hours= $_POST['hourPHP'];
    $rate= $_POST['ratePHP'];
    $date= $_POST['datePHP'];
    $user= $_SESSION['email'];
    //prepared statement
    $stmt =$conn->prepare("INSERT INTO Paysheet(`Hours`,`Rate`,`Date`,`Employee_email`) VALUES(?,?,?,?)");
    $stmt->bind_param("idss",$hours,$rate,$date,$user);
    $stmt->execute();

    echo "New record created succesfully";

    $stmt->close();
    $conn->close();
  }
?>


And here's my HTML with jQuery/AJAX:
<html>
  <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
     integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>

    <nav class="navbar navbar-expand-sm bg-dark navbar-dark justify-content-center">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="">Income Calculator</a>
          <a class="nav-link justify-content-right" href="logout.php">Logout</a>
      </ul>
    </nav>
    <div class="jumbotron justify-content-center">
      <h1 class="text-info justify-content-center">Get Your Income (Tax Feature Coming Soon)!</h1>
    </div>
    <div class="container">
      <div  class="row">
        <div class="col"></div>
        <div class="col-4 justify-content-center">
          <div class="card bg-light">
            <div class="card-header">Input Your Rate and Hours</div>
            <div class="card-body">
              <form method="post" action="hourspage.php">
                <div class="form-group row">
                  <label class="col-sm-4 col-form-label"for="hours">Hours</label>
                  <div class ="col-sm-5">
                    <input type="number" min="1" max="24"class="form-control"id="hours" class="form-control " placeholder="Enter Hours"required>
                  </div>

                </div>
                <div class="form-group row">
                  <label class="col-sm-4 col-form-label"for="rate">Rate</label>
                  <div class="col-sm-5">
                    <input type="number" step=".01"class="form-control" id="rate"  placeholder="Enter Rate" required >
                  </div>
                </div>
                <div class="form-group row">
                  <label class="col-sm-4 col-form-label"for="date">Date</label>
                  <div class="col-sm-5">
                    <input type="date"class="form-control" id="date" required >
                  </div>
                </div>
                <div class="form-group row">
                  <div class="dropdown">
                    <div class="col-sm-4">
                      <select class="form-control-4">
                        <option value="Wisconsin">Wisconsin</option>
                        <option value="Illinois">Illinois</option>
                      </select>
                    </div>
                  </div>

                </div>
                <div class="form-group row">
                  <div class="col-sm-6">
                    <button type="submit" class="btn btn-primary" id="submit">Submit Hours</button>
                  </div>
                </div>
              </form>
              <p class="income_field">Your income is $<span id="incomef"></span>.</p>
              <p id="response"></p>
              </div>
            </div>

        </div>
      <script
      src="https://code.jquery.com/jquery-3.4.1.min.js"
      integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
      crossorigin="anonymous"></script>
      <script type="text/javascript">
        $(document).ready(function(){
          //function to determine total pay-out
          $('#submit_pay').on('click',function(){
            var hoursf=$("#hours").val();
            var ratef=$("#rate").val();
            var datef=$("#date").val();
            var statef = $('#dropdown-item').val();
            //validation check to see if all fields have been filled
            if(hoursf==""&&ratef==""){
              alert("Please fill out all fields");
            }else{
            if(hoursf==""||ratef==""){
                alert("Please fill out required field");
              }}




            //send data off to server
            $.ajax(
              {
                url:"hourspage.php",
                type:"POST",
                data:{
                  hourPHP:hoursf,
                  ratePHP:ratef,
                  datePHP:datef
                },
                success: function(response){

                  $('#response').html(response);
                  console.log(data);

                    },
                datatype:"text"
              }
            );
            //calc income so user knows how much they are getting
            var incomef = hoursf*ratef;
            $('span#incomef').text(incomef);
            $('p#income_field').show();


          })
        });
      </script>
    <div class="col"></div>
    </div>
  </div>
  </body>
</html>


What I have tried:

I thought it might have something to do with the AJAX request not sending thus affecting the ability of PHP's $_POST to in the condition to fail but I don't necessarily understand what exactly is wrong with the response. Please advise.
Posted
Updated 2-Apr-20 11:56am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I am nowhere near being an expert on PHP, but it appears that your script is looking for something that is not being sent

Your PHP is checking to see if submit is included in the request
if(isset($_POST['submit'])){
But the AJAX making the request does not appear to have submit in the data being sent
$.ajax({
	url:"hourspage.php",
	type:"POST",
	data:{
		hourPHP:hoursf,
		ratePHP:ratef,
		datePHP:datef
	},
	success: function(response){
		$('#response').html(response);
		console.log(data);
	},
	datatype:"text"
});
   
Comments
Member 14790760 2-Apr-20 18:15pm
   
Oh I get what you mean. I put in the submit for the data being sent and still getting the same issue :(
$.ajax(
              {
                url:"hourspage.php",
                type:"POST",
                data:{
                  submit:1,
                  hourPHP:hoursf,
                  ratePHP:ratef,
                  datePHP:datef
                },
                success: function(response){

                  $('#response').html(response);
                  console.log(data);

                    },
                datatype:"text"
              }
            );

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100