Auto fill form

I am trying to create a table where a user selects a last name from an sql query drop-down-box (Select tag with options) that is also an sql query. I can get the combo box to work fine, but I cant get the other results to populate at all.

My code:

Customer Information "; } ?>
Last Name <?php $mysqli = new mysqli("mysql", "USER", "PASSWORD", "DBASE"); $query= $mysqli->query("SELECT DISTINCT * FROM Customer");
  while ($row = mysqli_fetch_array($query)) {
  echo "<option value='" . $row['LastName'] . "'>" .$row['LastName'] . "</option>";
}   

?>

City
First Name <?php $mysqli = new mysqli("mysql", "USER", "PASSWORD", "DBASE"); $query = $mysquli->query("SELECT * FROM Customer WHERE LastName = 'Customer'"); while ($result = mysqli_fetch_array($query)) { echo " '" . $result['FirstName'] . " ' " .$result['FirstName'] . " State
Company Name Zip
Address Phone
Email

This will also cut off everything after the First Name

Welcome to the forum!

Well it’s kind of hard to tell what is wrong as the code is cutoff and not formatted.

John

Just so I am clear.

A user comes to your page…

Makes a selection from a dropdown…and?..

Then what?

From this point… does the page submit… and the ‘new’ page has a table pre-populated with specific data?

Or… are you saying you want to NOT submit (reload) the page… and the table you want to populate is on the same page as the dropdown the user is interacting with?

Lets start there before anyone offers some advice

Your code contains a spelling mistake in a variable name, used in an object context, resulting in a fatal php run-time error, which is the reason for the cut off output after that point.

Please, everyone trying to use php, find the php.ini on your system that php is using, ALWAYS set error_reporting to E_ALL, and when learning, developing, and debugging code/queries, set display_errors to ON. When on a live/public server, set display_errors to OFF and set log_errors to ON. Stop and start your web server to get any changes made the php.ini to take effect.

Also, why are you repeating the database connection code? Each web page should only make one database connection.

@Strider64 I did a copy paste on the code, it automatically did that.

@whispers I would like to stay on the same page, and not submit and $_POST to a new page

I am very new to php. I’m pretty good at HTML, but never dabbled with php or mysql until recently. I wil double check the spelling

If you want to ‘stay on the same page’ without posting/submitting the page…\

Then you will need to use AJAX to handle the dropdown change/selection.

Call a PHP script to send the data too… and have the AJAX callback update the page with the returned data.

Is there an example of your table layout?

Or an example on how you want your table to look/display?

I’ve never dealt with AJAX at all. Yes, I have an example(ish). To give you a better understanding of what I’m trying to do, I am attempting to use this setup to create a work order. The section I am having issues with is the callback information for the customer. There is a second table, which is lower on the page, that contains the service information, such as service address, contact name, work needed, etc…

I would like to be able to select the customer from the dropdown, and have the information about the customer auto-fill with the selection.

A form and its form processing code can be put on the same page. If you want the page to not refresh when data is submitted, you would use AJAX. However, if you are not at the point where you can design, write, test, and debug the php code needed to dynamically produce html elements and process the submitted form data, you are not at the point of needing to using AJAX, as it adds a layer that makes debugging harder. Learn to program using php first, then learn how to add extra things like AJAX to the process.

Well you have to pick your poison here I guess.

If you want to go the AJAX route, you will need a SEPARATE .php file that does the DQ querying and handing of the returned data… that will be passed back to the AJAX call to this external .php script.

Does that make sense to you?

Or you can select from the drop down and hit submit, and then have the page reload with that users credentials/work information.

Up to you.

The later is just a simple HTML/PHP submission form using $_POST data and a database query.

The former is a bit more involved because you need a separate/external .php file that does the work for you.

Here is a quick -n- dirty way to do it with a normal HTML/PHP form type of approach:

<?
//PDO should parameterize and sanitize good enough, but in case any screen output
function cleanInput($dirtyData){   
	$cleanedData = htmlspecialchars(strip_tags($dirtyData)); 
	return $cleanedData;
}
$employee_id = isset($_POST["employee_id"]) ? cleanInput($_POST["employee_id"]) : '' ;


//check from 'mode' and set state
$mode = isset($_GET['mode']) ? $_GET['mode'] : '';

?>

<!-- Scripts -->
<script type="text/javaScript"> 

	//jQuery (event actions)
	document.addEventListener("DOMContentLoaded", function(event) { 		
		
		//enter key
		$(document).bind('keypress', function(e) {
			//enter key (code)
			if(e.keyCode==13){
				 $('#submitBtn').trigger('click');
			 }
		});			
			
		//submit button listener
		$("#submitBtn").click(function() {
			//console.log("submit button clicked");			
			
			//validation message holder
			var errormessage = '';	
			
			//any potential field validation goes here
			
			//employee
			if ($("#employee_id").val() === "") {
				errormessage += "\n Please select employee first";		
			}			
						
			//name - example
			/*			
			if($('#name').val().trim() == ''){		
				//update error message for output
				errormessage += "\n Name";						
			}	
			*/	
			
			//check if any errors were found
			if(errormessage != ''){
				event.preventDefault(); //do not submit form
				//output missing data
				alert("The following items need attention: \n" + errormessage);
			}else{
				//submit form
				$('#appForm').submit();
			}			
			
			
		});
	});
					
</script>



<!-- example form -->

<div id='appContainer'>                                
	<form id="appForm" name="appForm" method="post" action="<?=$_SERVER['PHP_SELF']?>?mode=submit">

		<select id="employee_id" name="employee_id">
		  <option value="">Select Employee</option>
		  <option value="1">Name1</option>
		  <option value="2">Name2</option>
		  <option value="3">Name3</option>
		  <option value="4">Name4</option>
		</select> 		
		
		<div id="buttonContainer" name="buttonContainer">
			<br><button id="submitBtn" type="button">Get Work Order Details</button><br>							
		</div>
	</form>
</div>
	
<?
if($mode == 'submit') {	

	echo 'ID CHECKING FOR: ' . $employee_id .'<br>';
	
	$employeeDetails_sql = "SELECT * FROM php_help WHERE employee_id = :employee_id";
	$employeeDetails_stmt = $conn->prepare($employeeDetails_sql);
	$employeeDetails_stmt->bindValue(':employee_id', $employee_id);
	$employeeDetails_stmt->execute();
	$employeeDetails_stmt->setFetchMode(PDO::FETCH_ASSOC);	
	$_employeeDetails = $employeeDetails_stmt->fetch(); //single row
	//$_employeeDetails = $employeeDetails_stmt->fetchAll(); //returns multi-dimensional array (changes way you access object below)
		
	$colcount = $employeeDetails_stmt->columnCount();
	$rowcount = $employeeDetails_stmt->rowCount();	
	//var_dump($_employeeDetails);
	
	if($rowcount <= 0){
		//no member record found
		return 'There was no username/password match<br>Please try again..<br>' . print_r($_employeeDetails);
	}else{		
		$target_id = $_employeeDetails['employee_id'];
		$target_first = $_employeeDetails['employee_first'];
		$target_last = $_employeeDetails['employee_last'];
		$target_work_order = $_employeeDetails['work_order_id'];
		
		echo 'EMPLOYEE ID: ' . $target_id . '<br>';
		echo 'FIRST NAME: ' . $target_first . '<br>';
		echo 'LAST NAME: ' . $target_last . '<br>';
		echo 'WORK ORDER#: ' . $target_work_order . '<br>';
		
	}	

}

I have no clue what your table looks like… or what data is in it either…

I just output some random stuff… and DID NOT make the results into a table. (no clue what it does or should look like)

Sponsor our Newsletter | Privacy Policy | Terms of Service