0

I have a webpage that allows user to enter a minGPA in a textbox to query a database and display all records of students that meet that criteria. The html calls a separate php file and also includes ajax calling a function. The problem I'm having is no matter what my query is returning all students. This has plagued me for 2-days now trying to figure where my error is.

Table data:

CREATE DATABASE student_gpa_db;
USE student_gpa_db;

CREATE TABLE student (
    studentID   INT(11)     NOT NULL    AUTO_INCREMENT,
    name        VARCHAR(255)    NOT NULL,
    email       VARCHAR(255)    NOT NULL,
    GPA     DECIMAL(4,2)    NOT NULL,
    PRIMARY KEY(studentID)
);
  INSERT INTO student VALUES
    (1, "John Doe", "[email protected]", 3.56),
    (2, "Jim Smith", "[email protected]", 2.79),
    (3, "Jerry Gold", "[email protected]", 3.78),
    (4, "Jane Doe", "[email protected]", 2.99),
    (5, "Jill Hill", "[email protected]", 2.55);

PHP/HTML:

 <?php
//get user input from text box 
require_once('student_gpa_db.php');
//validate user enters a value

$minGPA = filter_input(INPUT_POST, 'minGPA');
//Code for query
$query = 'SELECT *
          FROM student
      WHERE GPA >= :minGPA
          ORDER BY studentID';

$statement = $db->prepare($query);
$statement->bindValue(':minGPA', $minGPA); 
$statement->execute();
$students = $statement->fetchAll();
?>
<!--table to hold output-->
<table>
<tr>
<th>Student ID</th>
<th>Name</th>
<th>Email</th>
<th>GPA</th>
</tr>

<?php foreach ($students as $student) : ?> 
<tr>
    <td><?php echo $student['studentID']; ?></td>
    <td><?php echo $student['name']; ?></td>
    <td><?php echo $student['email']; ?></td>
    <td><?php echo $student['GPA']; ?></td>
</tr>
<?php endforeach; ?>

</table>

    <script type="text/javascript">
    function queryStudent() {
        var ajaxRequest = new XMLHttpRequest;
        ajaxRequest.onreadystatechange = function() {
            if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
                document.getElementById("ajax_output").innerHTML = ajaxRequest.responseText;
            }
        };
        ajaxRequest.open("GET", "gpa_search.php", true);
        ajaxRequest.send(null);
    }
    </script>   
    <form action="gpa_search.php" method="post" id="form1">
    <label>Minimum GPA: </label>
    <input type="text" id="GPA" name="minGPA"><br><br>

    <input type="button"  onclick="queryStudent();" value="Search" id="button">
    </form><br>
    <p>Students with higher than minimum GPA will be displayed below.</p><br>

    <!--output section after search-->

    <section id="ajax_output">                       <!--Echo the user input variable with output-->
    <h3>Student list (Students with GPAs higher than <?php echo htmlspecialchars($minGPA); ?></h3>

    </section><br><br>

    <a href="search_split.htm">Search & Split</a>
3
  • 1
    You send no info about GPA to your script. Commented Apr 27, 2016 at 18:45
  • What do you see if you echo out $query Commented Apr 27, 2016 at 18:46
  • I can't test from work. Whatever I entered in textbox returned all rows not just the ones that meet minGPA or higher Commented Apr 27, 2016 at 18:47

1 Answer 1

1

You should pass the param with ajax request.

just replace below function with yours and check.

function queryStudent() {
        var ajaxRequest = new XMLHttpRequest;
        ajaxRequest.onreadystatechange = function() {
            if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
                document.getElementById("ajax_output").innerHTML = ajaxRequest.responseText;
            }
        };
        params = "minGPA=" + document.getElementById("GPA");
        ajaxRequest.open("POST", "gpa_search.php", true);
        ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajaxRequest.setRequestHeader("Content-length", params.length);
        ajaxRequest.setRequestHeader("Connection", "close");
        ajaxRequest.send(params);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

@allendks45, are you checked..?
Sorry, busy at work. I finally solved my problem late last evening. I added a variable like you and returned it with the request.send and now it is working. Thanks for the help!

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.