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" class="bloginputform">
<input type="text" name="bloginput" placeholder="Type your blog here..." id="bloginput" class="bloginput">
<div class="popup">
<input type="button" placeholder="Submit" id="popupbtn" onclick="toggleHideShow()">
<span class="popuptext hide" id="myPopup">
<input type="name" name="author" placeholder="Name:" id="nameinput" class="nameinput">
<br>
<input type="email" name="authoremail" id="emailinput" placeholder="E-mail..." class="emailinput">
<br>
<label class="filelabel" id="filelabel">
<input type="file" name="post-image" accept="image/*" id="fileinput" class="fileinput">
<p class="fileinputtext" id="fileinputtext">
Upload Image:
</p>
</label>
<p><button class="crossbtn" onclick="toggleHideShow()" id="crossbtn">✕</button><input type="submit" value="Post" placeholder="Post" class="submitbtn" 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 class="wrapperdiv" id="wrapperdiv">
<h1 class="Title" id="Title">$Title<br>By: $author</h1>
<br>
<br>
<br>
<center>
<img src="$image" name="Blogimage" class="blog-img" id="blog-img">
</center>
<br>
<br>
<br>
<p class="blogtext" 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.
Advertisement
Answer
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" class="bloginputform">
<input type="text" name="bloginput" placeholder="Type your blog here..." id="bloginput" class="bloginput">
<div class="popup">
<input type="button" placeholder="Submit" id="popupbtn" onclick="toggleHideShow()">
<span class="popuptext hide" id="myPopup">
<input type="name" name="author" placeholder="Name:" id="nameinput" class="nameinput">
<br>
<input type="email" name="authoremail" id="emailinput" placeholder="E-mail..." class="emailinput">
<br>
<label class="filelabel" id="filelabel">
<input type="file" name="post-image" accept="image/*" id="fileinput" class="fileinput">
<p class="fileinputtext" id="fileinputtext">
Upload Image:
</p>
</label>
<p><button class="crossbtn" onclick="toggleHideShow()" id="crossbtn">✕</button><input type="submit" value="Post" placeholder="Post" class="submitbtn" 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 class="wrapperdiv" id="wrapperdiv">
<h1 class="Title" id="Title">$Title<br>By: $author</h1>
<br>
<br>
<br>
<center>
<img src="$image" name="Blogimage" class="blog-img" id="blog-img">
</center>
<br>
<br>
<br>
<p class="blogtext" 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.
}