Jump to content

Display Everything on the same page


rich_web_development

Recommended Posts

Hi,

 

I'm trying to get a form where you submit information about a home to a database and below the form it displays the homes with the information and a photo.

 

Below the home information and photo I have a button to DELETE RECORD and a button to upload a photo.

 

All of the php and forms are in one file so submitting the home info and deleting record are just have action="samefile.php". This is so that it stays on the same page and doesn't have to jump to another page.

 

I cannot get the upload photo to stay on the same page. The only way I can get the upload photo to work is to send it to another file. This of course takes me to another page which I don't want. I want everything to stay on the same page.

 

The code I have (in a file called "PicTest03.php) is as follows:

 

 

<!DOCTYPE html>
<head>
</head>
<html>
<body>
<form action="PicTest03.php" method="POST"><pre>
Name <input type="" name="name">
Bedrooms <input type="text" name="bedrooms">
Length <input type="text" name="length">
Width <input type="text" name="width">
Serial Number <input type="text" name="serialno">
<input type="submit" value="ADD RECORD">
</pre>
</form>
<br><br><br>
<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
if (isset($_POST['delete'])&&
isset($_POST['serialno']))
{
$serialno = get_post($conn, 'serialno');
$query = "DELETE FROM holidayhomes WHERE serialno='$serialno'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query<br>" . $conn->error . "<br><br>";

}
if (isset ($_POST['name']) &&
isset ($_POST['bedrooms']) &&
isset ($_POST['length']) &&
isset ($_POST['width']) &&
isset ($_POST['serialno']))

{
$name = get_post ($conn, 'name');
$bedrooms = get_post ($conn, 'bedrooms');
$length = get_post ($conn, 'length');
$width = get_post ($conn, 'width');
$serialno = get_post ($conn, 'serialno');
$query = "INSERT INTO holidayhomes (name, bedrooms, length, width, serialno) VALUES ('$name','$bedrooms', '$length', '$width', '$serialno')";

$result = $conn->query($query);
if (!$result) echo "INSERT failed: $query<br>" . $conn->error . "<br><br>";
}
$query = "SELECT * FROM holidayhomes";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo <<<_END
<pre>
name $row[0]
Bedrooms $row[1]
Length $row[2]
Width $row[3]
Serial No $row[10]
MainPic $row[4]
<img src="uploads/$row[4]" width=200 height=200>
</pre>
<pre>
<form action="PicTest03.php" method="POST">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="serialno" value="$row[10]">
<input type="submit" value="DELETE RECORD">
</form>
</pre>
<pre>
<form action="mainphoto01.php" method="post" enctype="multipart/form-data">
Select main photo:
<input type="hidden" name="serialno" value="$row[10]">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</pre>
<br><br>
_END;
}


$result->close();
$conn->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}
?>
</body>
</html>
The code for "mainphoto01.php is:
<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false){
echo "FIle is an image - " . $check["mime"] . "." ;
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0){
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
echo "The file " . basename( $_FILES["fileToUpload"]["name"]) . " has been uploaded.";
} else {
echo "Hello!";
}
}
$mainpic = basename( $_FILES["fileToUpload"]["name"]);
if(isset($_POST["serialno"])) $serialno = $_POST['serialno'];
$query = "UPDATE holidayhomes SET mainpic='$mainpic' WHERE serialno='$serialno'";
$result = $conn->query($query);
if(!$result) echo "INSERT failed: $query<br>" . $conn->error . "<br><br>";

?>

No matter what I try I cannot get the above code to work if I keep it in the "PicTest03.php" file.

 

Can someone please show what the code should look like so I can get everything to show on the same page?

 

I have had a look as the PHP and AJAX and DATABASE http://www.w3schools.com/php/php_ajax_database.asp which I got the example working but it uses a drop down list.

Is there a way to convert the code used for the example on http://www.w3schools.com/php/php_ajax_database.asp and instead of drop down list just have the information from the database shown under the form for submitting the home information?

Link to comment
Share on other sites

It's hard to read your code when you just paste it (and, for that matter, changing the font size a bunch doesn't help readability any either). You should use code tags to post code here so that it is highlighted in a monospace font. The text editor has a button to wrap something in code tags, or you can manually type them.

 

You should add hidden inputs to each form so that you can tell which one was submitted. There's nothing about file uploads that requires a separate file, you're probably just not detecting which form was submitted. Use hidden inputs or querystring variables for that.

Link to comment
Share on other sites

It's hard to read your code when you just paste it (and, for that matter, changing the font size a bunch doesn't help readability any either). You should use code tags to post code here so that it is highlighted in a monospace font. The text editor has a button to wrap something in code tags, or you can manually type them.

 

You should add hidden inputs to each form so that you can tell which one was submitted. There's nothing about file uploads that requires a separate file, you're probably just not detecting which form was submitted. Use hidden inputs or querystring variables for that.

 

Thanks for the reply.

Sorry about the messy code and not using the code tags. I'll use them in the future.

 

I've taken all the echo parts out of the mainphoto01.php. Changed the form for uploading photos so that the action points back to the same file. I put the code for uploading photos in the PicTest03.php file. It all works and everything stays on the same page now. The only thing is I keep getting the following

 

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\PicTest03.php on line 54

 

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\PicTest03.php on line 70

 

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\PicTest03.php on line 77

 

Notice: Undefined variable: serialno in C:\xampp\htdocs\PicTest03.php on line 79

<!DOCTYPE html>
<head>
</head>
<html>
<body>

<form action="PicTest03.php" method="POST"><pre>
Name          <input type="text" name="name">
Bedrooms      <input type="text" name="bedrooms">
Length        <input type="text" name="length">
Width         <input type="text" name="width">
Serial Number <input type="text" name="serialno">
<input type="submit" value="ADD RECORD">
</pre>
</form>
<br><br><br>

<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);

if (isset($_POST['delete'])&&
	isset($_POST['serialno']))
	{
		$serialno = get_post($conn, 'serialno');
		$query = "DELETE FROM holidayhomes WHERE serialno='$serialno'";
		$result = $conn->query($query);
		if (!$result) echo "DELETE failed: $query<br>" . $conn->error . "<br><br>";
		
	}

if (isset ($_POST['name']) &&
	isset ($_POST['bedrooms']) &&
	isset ($_POST['length']) &&
	isset ($_POST['width']) &&
	isset ($_POST['serialno']))


	{
		$name     = get_post ($conn, 'name');
		$bedrooms = get_post ($conn, 'bedrooms');
		$length   = get_post ($conn, 'length');
		$width    = get_post ($conn, 'width');
		$serialno = get_post ($conn, 'serialno');
		$query    = "INSERT INTO holidayhomes (name, bedrooms, length, width, serialno) VALUES ('$name','$bedrooms', '$length', '$width', '$serialno')";
		
		$result = $conn->query($query);
		if (!$result) echo "INSERT failed: $query<br>" . $conn->error . "<br><br>";

    }

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if(isset($_POST["submit"])){
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false){
// echo "FIle is an image - " . $check["mime"] . "." ;
$uploadOk = 1;
} else {
// echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0){
// echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
// echo "The file " . basename( $_FILES["fileToUpload"]["name"]) . " has been uploaded.";
} else {
// echo "Hello!";
}
}

$mainpic = basename( $_FILES["fileToUpload"]["name"]);
if(isset($_POST["serialno"])) $serialno = $_POST['serialno'];
$query = "UPDATE holidayhomes SET mainpic='$mainpic' WHERE serialno='$serialno'";
$result = $conn->query($query);

    $query = "SELECT * FROM holidayhomes";
    $result = $conn->query($query);
    if (!$result) die ("Database access failed: " . $conn->error);

    $rows = $result->num_rows;
    for ($j = 0 ; $j < $rows ; ++$j)
    {
    	$result->data_seek($j);
    	$row = $result->fetch_array(MYSQLI_NUM);
    	
echo <<<_END
<pre>
name       $row[0]
Bedrooms   $row[1]
Length     $row[2]
Width      $row[3]
Serial No  $row[10]
MainPic    $row[4]
<img src="uploads/$row[4]" width=200 height=200>
</pre>

<pre>
<form action="PicTest03.php" method="POST">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="serialno" value="$row[10]">
<input type="submit" value="DELETE RECORD">
</form>
</pre>


<pre>
<form action="PicTest03.php" method="post" enctype="multipart/form-data">
Select main photo:
<input type="hidden" name="serialno" value="$row[10]">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</pre>
<br><br>
_END;

    }

    
$result->close();
$conn->close();

function get_post($conn, $var)
{
	return $conn->real_escape_string($_POST[$var]);
}
?>
</body>
</html> 

Hope I used the code tags correctly

Link to comment
Share on other sites

The undefined indexes are because that code isn't inside an if statement where you check if the form was submitted before trying to process the uploaded file. The undefined variable is because you only define that variable inside an if statement, but you try to use it outside of the if statement.

Link to comment
Share on other sites

The undefined indexes are because that code isn't inside an if statement where you check if the form was submitted before trying to process the uploaded file. The undefined variable is because you only define that variable inside an if statement, but you try to use it outside of the if statement.

 

Oh right. I think I have it now :)

Thanks very much for the help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...