0

Having trouble pulling variables from one PHP to another script.

I have three different files, adminPage.html, reportScript.php, and report.php.

adminPage.html takes variables from the user and uses AJAX post function to post the variables to reportScript.php.

report.php is supposed to pull those posted variables from reportScript.php and use the variables in a SQL function, however, I am receiving an error stating that I have an "undefined index: startDate" and "undefined index: endDate" where I am instantiating the variables in PHP.

adminPage.html:

<center><h2> Choose the dates below that you need an order list from: </h2>
</br>
  <form>
    <h2>Start:</h2>
    <input type="date" id ="reportStartDate" name = "startDate">
      </br>
    <h2>End:</h2>
    <input type="date" id ="reportEndDate" name = "endDate">
  </form>
</center>

</br></br>
    <button id="runReportButton" onclick = "runReport()"> Run Report </button>

<script>



function runReport()
{
  var jStartDate;
  var jEndDate;

  jStartDate = document.getElementById("reportStartDate").value;
  jEndDate = document.getElementById("reportEndDate").value;

  /*console.log(jStartDate);
  console.log(jEndDate); */

  $.ajax
  ({
    type: "POST",
    url: "phpScripts/reportScript.php",
    data: {startDate: jStartDate, endDate: jEndDate},
    success: function(response)
      {
        console.log("posted");
       window.open("report.php", "_self");
      }
  });

}

</script>

reportScript.php:

    <?php
    require 'connect.php';

    //posts data to db
    $startDate = $_POST["startDate"];
    $endDate = $_POST["endDate"];

    $sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < startDate OR 
    dateOrdered > endDate)";

    $result = $conn->query($sql);

    if($result){
    echo "true";
    }

    else{
    echo "false";
    }
    ?>

report.php:

<?php
require 'phpScripts/connect.php';

require 'phpScripts/reportScript.php';

//posts data to db

/*$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];*/

/*$startDate = '2018-01-01';
$endDate = '2018-08-08'; */

$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < '$startDate' OR dateOrdered > '$endDate');";

$result = $conn->query($sql);
//above is reportScript.php, below is pulling list method from order.php
//below works, just needs variables from the reportScript
echo "<ul>";
if($result->num_rows >0)
{
  $i = 0;
  while($row = $result->fetch_assoc()) // this loads database into list, also 
creates array of pricing which someone can pull from later to get total
  {
    echo  "<li style='font-size:15px'>".$row["drinkName"]. ", Date Ordered: " 
.$row["dateOrdered"] . ",Cost: " .$row["drinkCost"] . "</li>";
    echo "</br>";

  $i = $i+1;
  }
}else {
  echo "<p> you're a dummy and you did this wrong </p>";
}
echo "</ol>";


?>

2 Answers 2

1

You forgot the dollar sign ($) in your variables in reportScript.php.

 $sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < $startDate OR 
    dateOrdered > $endDate)";

This statement is also vulnerable to sql injection.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help! The dollar sign was definitely something I missed, however it has not solved the issue, I am still coming up with the same error.
I think the issue is with window.open() in your ajax. You're starting another thread and when you require the reportScript.php script the $_POST array isn't assigned anymore. Maybe you should do everything you have to do in your ajax request and work output in php.
thanks so much for all of your help. I combined both report.php and reportScripts.php, and essentially the AJAX function posts directly to report.php. However, I am unsure on how to open the report.php window without starting a therefore giving me the same error.
0

With some of the advice taken from @Ralf, I combined both reportScript.php and report.php, and used a $_GET statement to put the date variables into the URL upon opening. This way, the query isn't placed twice and the variables are still saved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.