So i am trying to make the server create a new HTML file every time i create a blog on my page on the make page of my website.
So what i tried is getting all the details with $_POST
with this:
HTML:
<form name="bloginput" action="blogpost.php" method="POST" >
<input type="text" name="bloginput" placeholder="Type your blog here..." id="bloginput" >
<div >
<input type="button" placeholder="Submit" id="popupbtn" onclick="toggleHideShow()">
<span id="myPopup">
<input type="name" name="author" placeholder="Name:" id="nameinput" >
<br>
<input type="email" name="authoremail" id="emailinput" placeholder="E-mail..." >
<br>
<label id="filelabel">
<input type="file" name="post-image" accept="image/*" id="fileinput" >
<p id="fileinputtext">
Upload Image:
</p>
</label>
<p><button onclick="toggleHideShow()" id="crossbtn">✕</button><input type="submit" value="Post" placeholder="Post" id="submitbtn"></p>
</form>
PHP:
<?php
$blogtext = $_POST["bloginput"];
$author = $_POST["author"];
$email = $_POST["authoremail"];
$image = $_POST["post-image"];
$Title = "My blog."
$file = "blogposttest.html"
$code = '
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
<link rel="stylesheet" href="CSS/Blogs.css">
</head>
<body>
<div id="wrapperdiv">
<h1 id="Title">$Title<br>By: $author</h1>
<br>
<br>
<br>
<center>
<img src="$image" name="Blogimage" id="blog-img">
</center>
<br>
<br>
<br>
<p id="blogtext">$blogtext</p>
</div>
</body>
</html>
';
file_put_contents($file, $code)
header("Location: http://blogmedia.nl")
?>
I would really like some help on this.
CodePudding user response:
You have some syntax errors in your code. you can use the below code and when I test it will create an HTML file but consider that if you want to create multiple files you should give a dynamic name and also it's better to validate your variables in PHP.
<form name="bloginput" action="#" method="POST" >
<input type="text" name="bloginput" placeholder="Type your blog here..." id="bloginput" >
<div >
<input type="button" placeholder="Submit" id="popupbtn" onclick="toggleHideShow()">
<span id="myPopup">
<input type="name" name="author" placeholder="Name:" id="nameinput" >
<br>
<input type="email" name="authoremail" id="emailinput" placeholder="E-mail..." >
<br>
<label id="filelabel">
<input type="file" name="post-image" accept="image/*" id="fileinput" >
<p id="fileinputtext">
Upload Image:
</p>
</label>
<p><button onclick="toggleHideShow()" id="crossbtn">✕</button><input type="submit" value="Post" placeholder="Post" id="submitbtn"></p>
</form>
<?php
$blogtext = $_POST["bloginput"];
$author = $_POST["author"];
$email = $_POST["authoremail"];
$image = $_POST["post-image"];
$Title = "My blog.";
$file = "blogposttest.html";
$code = <<<EOD
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
<link rel="stylesheet" href="CSS/Blogs.css">
</head>
<body>
<div id="wrapperdiv">
<h1 id="Title">$Title<br>By: $author</h1>
<br>
<br>
<br>
<center>
<img src="$image" name="Blogimage" id="blog-img">
</center>
<br>
<br>
<br>
<p id="blogtext">$blogtext</p>
</div>
</body>
</html>
EOD;
file_put_contents($file, $code)
?>
Use the below condition to check $_POST is empty or not. This resolves as true even if all $_POST values are empty string
if (!empty($_POST))
{
// Your code
}
Check specific key is available in post data. You can use this condition:
if (isset($_POST['author']) )
{
}
Using this code you can check before creating a new file to avoid rewriting an old HTML file. If the file with the same name exists:
if (file_exists( $file )) {
// todo acction. for example create a random name for your new file.
}