How to merge two php page into one php - javascript

I have index.php and upload.php. index.php gives option of selecting an image and once clicked on upload image button, upload.php is called which then creates a directory and saves the image.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Face Recognition</title>
<link href="main.css" rel="stylesheet" type="text/css" href="" />
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Select file: <input name="userfile" type="file" />
<?php $file = isset($filename) ? $filename : ''; ?>
<input type="text" name="filename" value="<?php echo $file; ?>" />
<input type="submit" value="Upload Images" />
</form>
</body>
</html>
upload.php
<?php
$uploaddir = 'G:/dataset/' . $_POST['filename'] . "/";
// check if directory exists
if(!is_dir($uploaddir)){
mkdir($uploaddir);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']) ;
echo $uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile);
?>
When upload.php is called, the page redirects to upload.php page and shows the upload file path. I dont want to redirect to upload.php. I want to keep everything in single index.php file. So that after uploading the images, it shows the upload file path on the same index.php. How can we achieve it.?

Put both in one PHP page and set the action of your form to the page itself. it would do the trick :)
<!DOCTYPE html>
<html>
<head>
<title>Face Recognition</title>
<link href="main.css" rel="stylesheet" type="text/css" href="" />
</head>
<body>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Select file: <input name="userfile" type="file" />
<?php $file = isset($filename) ? $filename : ''; ?>
<input type="text" name="filename" value="<?php echo $file; ?>" />
<input type="submit" value="Upload Images" />
</form>
</body>
</html>
<?php
if(!empty($_FILES['userfile']))
{
$uploaddir = 'G:/dataset/' . $_POST['filename'] . "/";
// check if directory exists
if(!is_dir($uploaddir)){
mkdir($uploaddir);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']) ;
echo $uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}
?>

You can achieve that by using the functionality of PHP_SELF environment variable.
You have to make the action attribute as form action = <?php echo $_SERVER['PHP_SELF']; ?> and then add the code of upload.php into the same index.php.
For reference : Refer this link

Related

Converting HTML form input to PDF - How to print more than 2 outputs

I'm new to coding and trying to accomplish passing input data from a form to a PHP response page and displaying it in the browser. The user can then click on a button to save the HTML element to PDF. The code works when displaying 2 form outputs eg. but as soon as I add more outputs from the form the javascript doesn't seem to run. I cant understand what is going wrong.
HTML form input page:
<!DOCTYPE html>
<html>
<body>
<img src ="ff.png">
<form action="response.php" method="post">
Invoice Number/Reference:<br>
<input type="text" name="ref"><br>
Customer Name:<br>
<input type="text" name="cname"><br>
<br>
Item:<br>
<input type="text" name="i1">
Cost
<input type="text" name="c1">
<br>
Item:<br>
<input type="text" name="i2">
Cost
<input type="text" name="c2">
<br>
Item:<br>
<input type="text" name="i3">
Cost
<input type="text" name="c3">
<br><br>
Total Cost:<br>
<input type="text" name="t1"><br><br>
<input type="submit">
</form>
</body>
</html>
PHP response page with button to download to PDF
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js" ></script>
<script>
function generatePDF() {
var doc = new jsPDF(); //create jsPDF object
doc.fromHTML(document.getElementById("input_data"), // page element which you want to print as PDF
15,
15,
{
'width': 170 //set width
},
function(a)
{
doc.save("ClientInvoice.pdf"); // save file name as HTML2PDF.pdf
});
}
</script>
</head>
<body>
<div id="input_data">
<img src = "ff.png" width="150"><br>
<h2>Invoice Reference: <?php echo $_POST["ref"]; ?></h2><br>
Customer name: <?php echo $_POST["cname"]; ?><br><br>
Item: <?php echo $_POST["i1"]; ?><br>
£ <?php echo $_POST["c1"]; ?><br>
Item: <?php echo $_POST["i2"]; ?><br>
£ <?php echo $_POST["c2"]; ?><br>
Item: <?php echo $_POST["i3"]; ?><br>
£ <?php echo $_POST["c3"]; ?><br><br>
Total: <?php echo $_POST["t1"]; ?><br><br>
Thank you for choosing My Company.
</div>
Dowload PDF
</body>
</html>
For your case applying jspdf.debug.js
Please enclose your string data by <div> (so use </div>TEXT</div>)
Please use £ for the pound sign (to avoid seeing weird characters when you view this character on page)
So please change your code (response.php) to:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js" ></script>
<script>
function generatePDF() {
var doc = new jsPDF(); //create jsPDF object
doc.fromHTML(document.getElementById("input_data"), // page element which you want to print as PDF
15,
15,
{
'width': 170 //set width
},
function(a)
{
doc.save("ClientInvoice.pdf"); // save file name as HTML2PDF.pdf
});
}
</script>
</head>
<body>
<div id="input_data">
<img src = "ff.png" width="150"><br>
<h2>Invoice Reference: <?php echo $_POST["ref"]; ?></h2><br>
<div>Customer name: <?php echo $_POST["cname"]; ?></div><br><br>
<div>Item: <?php echo $_POST["i1"]; ?></div><br>
<div>£ <?php echo $_POST["c1"]; ?></div><br>
<div>Item: <?php echo $_POST["i2"]; ?></div><br>
<div>£ <?php echo $_POST["c2"]; ?></div><br>
<div>Item: <?php echo $_POST["i3"]; ?></div><br>
<div>£ <?php echo $_POST["c3"]; ?></div><br><br>
<div>Total: <?php echo $_POST["t1"]; ?></div><br><br>
<div>Thank you for choosing My Company.</div>
</div>
Dowload PDF
</body>
</html>
See a working version here:
http://www.createchhk.com/SOanswers/testSO17apr2022.html

How to pass PHP variable value to html input box from external PHP file?

HTML FILE
<body>
<input type="text" id="name" class="form-control" name="name">
<label for="name">name</label>
<script type="text/javascript" src="PHPfile.php"></script>
</body>
Separate PHP FILE
I have done MySQL query, and I have query value in $row['name'].
How can I put this value in the HTML input box?
<?PHP
**some code here**
//value in $row['name']
//confirmed by echo $row['name'];
// now I want to pass this value to my HTML input box
echo "<script>
var name = <?php echo $row['name'] ?>;
document.getElementById(name).value='name';
window.location.href='htmlpage.html';
</script>";
?>
I tried this but this didn't work. I got the following error
Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in...
I have tried to provide custom variable to var name and then document.getElementByID line. Still, it doesn't work.
Any Solution?
This should work.
<?php
/**some code here**/
//value in $row['name']
//confirmed by echo $row['name'];
// now I want to pass this value to my HTML input box
echo '<script>
document.getElementById("name").value = "' . $row['name'] . '";
</script>';
?>
or same thing if more complex HTML/script needed:
<?php
/**some code here**/
//value in $row['name']
//confirmed by echo $row['name'];
// now I want to pass this value to my HTML input box
?>
<script>
document.getElementById("name").value = "<?=$row['name'];?>";
</script>
<?php
/* more php code */
?>
If you still get the same error message, than the problem is somewhere else.
To echo a variable in a string ( in php ) we can use concatenation " . " $var['code'] ". " or template string { $var["code"] }
PHPfile.php
<?php
$row = ["name" => "something"];
echo "
let input = document.getElementById('name');
input.value = '{$row['name']}';
setTimeout(()=>{
window.location.href='htmlpage.html';
} ,2000)
"
?>
For echoing variables in string use {$var} instead of simple concatenation
Extended answer
index.php
<?php
// all querys here ... getting results from db
// assuming result
$row = [
"username" => "xxxxxxxxx",
"email" => "xxxxx#example.com"
];
$name = $row['username'];
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width , initial-scale=1.0">
<title>Damn Forms ! </title>
</head>
<body>
<form action="file.php" method="post" accept-charset="utf-8">
<label for="name">Name <span class="red">*</span></label>
<input type="text" id="name" class="form-control" name="name" placeholder="Enter your full name " value="<?= $name ?>">
<!-- sending values to another php file -->
<button type="submit">Submit</button>
</form>
</body>
</html>
file.php
<?php
if($_SERVER["REQUEST_METHOD"] === "POST"){
$name = $_POST['name'];
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width">
<title>Input Form </title>
</head>
<body>
<input type="text" name="name" id="" value="<?= $name ?>" />
</body>
</html>
I see 2 answers already, but my way of doing it would be....
HTML
<body>
<input type="text" id="name" class="form-control" name="name">
<label for="name">name</label>
<div id="externalPHP"></div>
<script type="text/javascript">
document.getElementById("externalPHP").innerHTML='<object type="type/php" data="PHPfile.php" ></object>';//javascript
$('#externalPHP').load('PHPfile.php');//jQuery
</script>
</body>
The php file
<?PHP
**some code here**
//value in $row['name']
//confirmed by echo $row['name'];
// now I want to pass this value to my HTML input box
echo '<script>document.getElementById("name").value='."$row['name']".';</script>';
?>

Changing site title server-side

How can I change a web page title for everyone on that site and not to be changed with refreshing, using javascript or PHP?
I've tried HTML DOM title but it gets back to its first title after refreshing.
I've tried giving variable in PHP and changing it by after a button pressed but the title didn't change!
These were my codes:
<?php
$title = "Chat";
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div id="chat_messages"></div>
<form enctype="multipart/form-data" method="post">
<input onClick="dokme()" class="button2" type="submit" name="send" value="Send">
</form>
</div>
<?php
if(isset($_POST['send'])){
$title = "new message";
}
?>
</body>
</html>
Any idea what should I do?
Glad to help.
Of course the title won't change, because php reads your code from 1st line to the end, and you have already added $title = "Chat";
what to do is to change the code like this:
<?php
if(isset($_POST['send'])) $title = "new message";
else $title = "Chat";
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div id="chat_messages"></div>
<form enctype="multipart/form-data" method="post">
<input onClick="dokme()" class="button2" type="submit" name="send" value="Send">
</form>
</div>
</body>
</html>
This will change the title.
Also, if you want to change the title without refreshing the page, you need JS.
Just use
document.title = '***'; //use your title to replace "***"!
Have a good day~
There are some mistakes in your code like you have used onClick instead you should use onclick
you have not putted a semicolon ; while you are typing
You have not assigned action to your form
Action will be echo htmlspecialchars($_SERVER['PHP_SELF'])
And we will use if else
This will work
<?php
if (isset($_POST['send'])) {
$title = "new message";
} else {
$title = "Chat";
}
?>
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<div id="chat_messages"></div>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" enctype="multipart/form-data" method="post">
<input onclick="dokme()" class="button2" type="submit" name="send" value="Send">
</form>
</div>
</body>
</html>
I hope this will Help you

Upload video Using PHP

I'm Trying this code to upload video.
but the code below is not working for me, can someone help?
Plese correct the code if required.
I dont know php well..
<?php
mysql_connect("localhost","root","123456789");
mysql_select_db("vid");
if(isset($_POST['submit']))
{
$name = $_FILES['file']['name'];
$name = $_FILES['file']['temp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url = "http://localhost/video/uploaded/$name";
mysql_query("INSERT INTO `videos` VALUE ('','$name','$url') ");
}
?>
<body>
Videos
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<br><br>
<input type="submit" name="submit" value="Upload" />
</form>
<?php
if(isset($_POST['submit']))
{
echo "<br/>".$name." has been Uploaded";
}
?>
</body>
Thanks

When I write page name in action attribute the file image does not upload

When I write page name in action attribute the file image does not upload. If i write "#" in action page the image upload to target folder.
Please check the coding and mention mistake. Thanks in advance.
<form method="post" enctype="multipart/form-data" action="#">
<input type="file" name="imageupload" align="center">
<input type="submit" name="frmsubmit" value="Submit">
</form>
<?php
include 'includes/dbconnection.php';
if(isset($_POST['frmsubmit'])){
$image_name = $_FILES['imageupload']['name'];
$image_type = $_FILES['imageupload']['type'];
$image_size = $_FILES['imageupload']['size'];
$image_tmp = $_FILES['imageupload']['tmp_name'];
$target_dir = "dp/";
$path = $target_dir . $image_name;
move_uploaded_file($image_tmp, $path);
echo "The file ". basename( $_FILES["imageupload"]["name"]). " has been uploaded."; }?>
In form's action you can use $_SERVER['PHP_SELF']. Then it will send the files to same script. If you are using same php script to upload the file then it is good. I have modified you code please have a look at that
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="file" name="imageupload" align="center">
<input type="submit" name="frmsubmit" value="Submit">
</form>
<?php
include 'includes/dbconnection.php';
if(isset($_POST['frmsubmit'])){
$image_name = $_FILES['imageupload']['name'];
$image_type = $_FILES['imageupload']['type'];
$image_size = $_FILES['imageupload']['size'];
$image_tmp = $_FILES['imageupload']['tmp_name'];
$target_dir = "dp/";
$path = $target_dir . $image_name;
move_uploaded_file($image_tmp, $path);
echo "The file ". basename( $_FILES["imageupload"]["name"]). " has been uploaded."; }?>
Then after successful file upload you can use header("Location") to transfer user to any page.

Categories