The first thing that caught my eye was not related to your actual issue, but does play a part in debugging. What caught my eye was
if (isset($_POST['search_button'])){
after HTML output was already started. Rule of thumb, POST variables should be worked with at the top, and always redirected (except ajax). Check out the Post-Redirect-Get pattern.
However, in this case you should be using GET because you aren’t changing data, just reading specific data.
Which leads me to the debugging step
Separate Logic and Presentation
Perform all your logic first, and then when that is all done, close out php and write your html (presentation), using php only for templating (loops, filling in variables, minor presentation conditionals)
This is extremely helpful when debugging, because you don’t have to dig through extraneous html code.
So your code could be reordered to be like this:
<?php
if (isset($_GET['search_button'])){
require_once "/home/users/web/b1240/dom.heather93124/public_html/resources/config.php";
$fName = $_GET['fName'];
$lName = $_GET['lName'];
// more code here
}
// any other code here
?>
<html>
<!— and so forth —>
Prepared Statements
The time to learn better security is now. This query is wide open to sql injection. Never, never, never put a raw variable into a query. You should be using prepared statements, where the user data is treated separately from the query. This example assumes you have a PDO connection $pdo.
$stmt = $pdo->prepare("SELECT * FROM Customers Where FirstName LIKE ? AND LastName LIKE ?");
$stmt->execute(
[
$_GET['fName'],
$_GET['lName']
]
);
// iterate through results with $stmt->fetch()
Incorporating this change will fix the error in your code. ($search->fetch... is an error)
And then the scripts that are called are all pretty similar to below just adapted to that part of the code where it is called. This one is the addrPicker.php
<?php
//login to database
require_once "/home/users/web/b1240/dom.heather93124/public_html/resources/configPDO.php";
if (isset($_POST['propID'])){
//Get value of custID variable
$propID = $_POST['propID'];
//Declare data variable as array
$data = array();
try{
//SQL query PDO
$sql = "SELECT * FROM `custAdr` WHERE `propID` = :propID";
//Set cust ID variable as wildcard
//$custID = "%$custID%";
$stmt = $connect->prepare($sql);
//bind wildcard to sql statements
$stmt->bindValue(':propID', $propID);
//Execute sql statements
$stmt->execute();
//create associative array with results from query
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$data[] = array(
'propID' => $row["propID"],
'street' => $row["streetAdr"],
'city' => $row["city"],
'state' => $row["state"],
'zip' => $row["zip"],
);
}
//echo results back to ajax success function
echo json_encode($data);
//catch errors from try statement
}catch(Exception $e){
echo $e-getMessage();
}
}
?>
Problem :
I am trying to make a form where the user can fill in the last name of a customer, the program then needs to use the entered name to search the database for any customers with the same last name. Then the select form element needs to be populated with the results form the query allowing the user to select the correct customers info. After the correct customer is selected another query needs to be done to search a separate table to get all addresses associated with the customer ID of the selected customer.
Flow
*User enters Customer last name
*query to get all customers with same last name
**select box populated with customer data
*User selects correct customer
*query to find all addresses with same Customer ID
**separate select box populated with 2nd query results
I would like to accomplish this with ajax
Comments
Comment posted by Tim Morton
There are several issues to deal with, which I will address in an answer once we narrow down the exact cause. Could you edit your post to show the form tag
Comment posted by primo4e
Tim Morton–I have included the whole form now
Comment posted by Serghei Leonenco
You have to consider using
Comment posted by primo4e
Serghei Leonenco– I would love to switch to AJAX just know next to nothing about it at the moment. I will work on improving security as I go.
Comment posted by primo4e
I have found a section in one of my books on AJAX so I will be learning up on it and will try somethings out.
Comment posted by mickmackusa
Review of your review: do not use
Comment posted by Serghei Leonenco
@mickmackusa I don’t agree with you about not using
Comment posted by mickmackusa
That is certainly your prerogative @Serg I find proper indentation to be sufficient …especially when the loop body only contains one line of code.
Comment posted by stackoverflow.com/a/41426713/2943403
Also, the iterated calls of
Comment posted by mickmackusa
To clarify about the appropriate request type to use…