Jump to content

Form won't appear on page load


digitaldragoon

Recommended Posts

I'm fairly new to php and I can very well have syntax mistakes so please don't pull any punches.

 

I'm trying to set up a page that shows a form then once it is submitted, pulls database info.  Unfortunately, as soon as I load the page, it comes up blank.  I can see the form in Dreamweaver, so I have no idea.  Here is the script:

 

<?php

if(!isset($_POST['submit'])) {
// form not submitted
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Therapy Summary</title>
</head>
<body>
<form action="therapy_summary.php" method="post">
    First Name: <input type="text" name="firstname">
    Last Name: <input type="text" name="lastname">
    <input type="submit" name="submit">
    </form> 

<?php 
}
else{
// form submitted, set server access variables
$host = "+++";
$user = "+++";
$pass = "+++";
$db_name = "+++";
$table_name = "total2";
// open connection
$mysqli = new mysqli($host, $user, $pass, $db);

// check for connection errors
if (mysqli_connect_errno()){
die("Unable to connect!");

// get form input, check that it's all there, escape input values for safety
$firstN = empty($_POST['firstname']) ? die ("Error: Enter a  first name") : mysqli_escape_string($_POST['firstname']);
$lastN = empty($_POST['lastname']) ? die ("Error: Enter a  first name") : mysqli_escape_string($_POST['lastname']);

// query to get records
$query = "SELECT * FROM `$table_name` WHERE `first_name` = '$firstN' and `last_name` = 'lastN' ";

// execute query
if ($result = $mysqli->query($query)){
// see if any rows were returned
	if($result->num_rows > 0){
//yes, then print them one after another
		echo "<table cellpadding=10 border=1>";
		while($row = $result->fetch_array()){
			echo "<tr>";
            	echo "<td>".$row[0]."</td>";
            	echo "<td>".$row[1]."</td>";
            	echo "<td>".$row[3]."</td>";
            	echo "<td>".$row[4]."</td>";
			echo "<td>".$row[5]."</td>";
			echo "</tr>";
		}
	}
// free result set memory
	$result->close();
}// close connection
mysqli_close($dbh);
} 

?>
</body>
</html>

 

Any assistance would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/190075-form-wont-appear-on-page-load/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.