I have a title/entry form for submitting a blog post. On submit, it checks for the same title in the db (which you're not allowed to do), then returns an error to tell you to change your title. I want it to keep both fields filled after the POST, because duh, a user shouldn't have to rewrite the entire entry. However, it only keeps the TITLE. session_start exists in the header.php. Please ignore any SQL injection vulnerable sections.
<?php
require("connect.php");
require("header.php");
if(isset($_POST['post'])){
$title = $_POST['blogtitle'];
$entry = $_POST['blogentry'];
$author = $_SESSION['username'];
$newBlogPostQuery = "SELECT * FROM contents WHERE title='$title'";
$newBlogPostResult = mysqli_query($conn, $newBlogPostQuery) or die("Error:".mysqli_error($conn));
$newBlogPostRow = mysqli_fetch_array($newBlogPostResult, MYSQLI_ASSOC);
if(mysqli_num_rows($newBlogPostResult) ==1){
$blogPostExists = TRUE;
}
else{
$blogQuery = mysqli_query($conn, "INSERT INTO contents (timestamp, title, entry, authorName) VALUES (now(), '$title', '$entry', '$author')");
if($blogQuery){
header("Location: index.php");
}
}
}
?>
<div class="flex-enable flex-center blog-body">
<?php if(isset($blogPostExists)&&$blogPostExists){
echo '<div class="exception" style="margin: 0;"><span class="small-white-subtitle">Please use a different title.</div>';
}
?>
<form class="flex-enable flex-column" method="POST">
<label for="blogtitle"><span class="blog-insert-title">Title</span></label>
<input class="blog-input-text small-white-title" id="blogtitle" type="text" name="blogtitle" placeholder="Your title" value="<?php echo isset($_POST['blogtitle']) ? $_POST['blogtitle']:''; ?>">
<label for="blogentry"><span class="blog-insert-title">Blog post</span></label>
<textarea class="blog-input-text blog-insert-entry" id="blogentry" type="text" rows="20" name="blogentry" placeholder="Write something amazing here..." value="<?php echo isset($_POST['blogentry']) ? $_POST['blogentry']:''; ?>"></textarea>
<input class="form-button small-white-title" type="submit" name="post" value="Post">
</form>
</div>
Textarea has no value attribute, the content is between <textarea> and </textarea>.
<textarea class="blog-input-text blog-insert-entry" id="blogentry" type="text" rows="20" name="blogentry" placeholder="Write something amazing here..."><?php echo isset($_POST['blogentry']) ? htmlentities($_POST['blogentry']) : ''; ?></textarea>
You shoul put the value beetween textarea tags
<textarea class="blog-input-text blog-insert-entry" id="blogentry" type="text" rows="20" name="blogentry" placeholder="Write something amazing here..." ><?php echo isset($_POST['blogentry']) ? $_POST['blogentry']:''; ?></textarea>
You need to put value between textarea start and end tag Your Value
in your case, it will be
<textarea class="blog-input-text blog-insert-entry" id="blogentry" type="text" rows="20" name="blogentry" placeholder="Write something amazing here..."><?php echo isset($_POST['blogentry']) ? $_POST['blogentry']:''; ?></textarea>
Textarea has no value attribute
<form class="flex-enable flex-column" method="POST">
<label for="blogtitle"><span class="blog-insert-title">Title</span></label>
<input class="blog-input-text small-white-title" id="blogtitle" type="text" name="blogtitle" placeholder="Your title" value="<?php if(isset($_POST['post'])){ echo $_POST['blogtitle']; } ?>">
<label for="blogentry"><span class="blog-insert-title">Blog post</span></label>
<textarea class="blog-input-text blog-insert-entry" id="blogentry" type="text" rows="20" name="blogentry" placeholder="Write something amazing here..." value="<?php if(isset($_POST['post'])){ echo $_POST['blogentry']; } ?>"></textarea>
<input class="form-button small-white-title" type="submit" name="post" value="Post">
Related
The landing page has form but it is not submitting and not redirecting to the next page.After submitting the form, it stays on the same page.
It was alright and was working before but I cant figure out where is the problem.
Code in formPage.php is below:
<form action="insert.php" enctype="multipart/form-data" class="contact_form" method="post" name="htmlform" >
<input class="frm-input" name="name" type="text" size="30" maxlength="50" placeholder="Enter Name" required="required" />
<input class="frm-input" name="email" type="text" size="30" maxlength="80" placeholder="Enter Email" required="required"/>
<input class="frm-input" name="jobtype" type="text" size="30" maxlength="30" placeholder="Job Type" required="required"/>
<input class="frm-input" name="ent_type" type="text" size="30" maxlength="80" placeholder="Entity Type" required="required"/>
<input class="frm-input" name="tas_out" type="text" size="30" maxlength="80" placeholder="Task Outline" required="required"/>
<input class="frm-input" name="l_st" type="text" size="30" maxlength="80" placeholder="Logo style of interest (optional)" />
<textarea required="required" class="frm-input frm-txtarea" name="message" placeholder="Task Description!!" maxlength="1000" cols="25" rows="6" ></textarea>
<input style="float: left;" type="file" name="image" size="66"/>
<input type="submit" class="btn btn-success btn-lg" name="submitt" value="submit" style="float: right" />
</form>
In this file I am trying to get the form information and storing them in database.But this page is not loading after the form submission.
Code in insert.php is below:
<?php
/*
$name = "";
$text = "";
$post = "";
*/
//echo $name;
if (isset($_POST['submitt']))
{
$name = $_POST["name"];
$mail = $_POST["email"];
$j_type = $_POST["jobtype"];
$e_type = $_POST["ent_type"];
$task = $_POST["tas_out"];
$l_st = $_POST["l_st"];
$task_des = $_POST["message"];
$image_name=$_FILES['image']['name'];
$image_type=$_FILES['image']['type'];
$image_size=$_FILES['image']['size'];
$image_temp=$_FILES['image']['tmp_name'];
//$date = date(m-d-y);
echo $name;
echo $mail;
echo $j_type;
echo $e_type;
echo $task;
echo $l_st;
echo $task_des;
if ($image_type=='image/jpeg' || $image_type=='image/png' || $image_type=='image/gif') {
move_uploaded_file($image_temp, "img/$image_name");
}
$connection=mysqli_connect("localhost", "root", "","com");
$query="insert into details (name, mail, j_type, e_type, task_outline, l_style, task_desc, image) values('".$name."','".$mail."','".$j_type."','".$e_type."','".$task."','".$l_st."','".$task_des."','".$image_name."')";
if(mysqli_query($connection,$query)){
//include('test.php');
echo '<h2>Data submitted successfully!!</h2>';
header("refresh:1; url=login.php");
//echo 'Back';
}else{
echo "Data not Submitted!";
# code...
}
}
echo "Data not Submitted!";
?>
echo "Data not Submitted!"; // put this line inside the last bracket
Sorry it was my fault,there was a typo mistake in the form action.Everything else is fine.
I want to keep form values after submit. I found some methods something like that :
However; when I run the html file, first : the code doesn't work, second: php codes can be seen in text areas. What should I do. Thank you for reading.
<form method="post" action="mail.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" value="<?php if (isset($_POST['Name'])){echo htmlentities($_POST['Name']); }?>" />
<label for="Subject">Subject:</label>
<input type="text" name="Subject" id="Subject" value="<?php if (isset($_POST['Subject'])){echo htmlentities($_POST['Subject']); }?>"/>
<label for="Phone">Phone:</label>
<input type="text" name="Phone" id="Phone" value="<?php if (isset($_POST['Phone'])){echo htmlentities($_POST['Phone']); }?>"/>
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" value="<?php if (isset($_POST['Email'])){echo htmlentities($_POST['Email']); }?>"/>
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message">
<?php if (isset($_POST['Message'])){echo htmlentities($_POST['Message']); }?>
</textarea>
<input type="submit" name="submit" value="Send" class="submit-button" />
</form>
It's not a php page, it's html, change the extension to .php so the browser knows it's got to read some php code.
Your POSTed values will only exist once the form has been submitted, so will initially not exist. If you want 'default' values in your form fields, you need to change the code inside your value='' elements to something like:
<?php echo (isset($_POST['Name'])) ? htmlentities($_POST['Name']) : 'Default text' ?>
This is my code. I'm trying to print the comments on my site. The query and everything works since I tried it in an empty project but here it doesn't echo. The comments update in the database but they just don't show. What am I missing?
<h1>Leave a comment below!</h1>
<?php
$find_comments = mysql_query("SELECT * FROM comments");
if ($find_comments) {
while ($row = mysql_fetch_assoc($find_comments)) {
$comment_name = $row['name'];
$comment = $row['comment'];
echo "<p>'$comment_name' - '$comment'</p>";
}
}
if(isset($_GET['error'])) {
echo "<p>100 per limit";
}
?>
<form action="post_comments.php" method="post">
<p>Your Name: </p>
<input type="text" name="name" size="40" maxlength="30" placeholder="Enter name..." </input><br><p>
<p>Your Email: </p>
<input type="text" name="email" size="40" maxlength="30" placeholder="Enter email..." </input><br><p>
<p>Your comment: </p>
<textarea type="text" name="comment" cols="50" rows="10" placeholder="Enter comment..."></textarea><br><p>
<input type="submit" name="submit" value="Submit comment!" ></input>
</form>
Your variable name is in single quote, it must me concat or place in double quote..
For Ex
echo "<p>$comment_name-$comment</p>";
or
echo "<p>".$comment_name."-".$comment</p>";
Try this it will work :
Use
"<p>".$comment_name." - ".$comment."</p>";
instead of
"<p>'$comment_name' - '$comment'</p>";
echo "<p>".$comment_name."-".$comment."</p>";
I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.
Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.
Somebody there see something wrong?
<?php
if (isset($_POST['sendForm'])) {
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['date'] = $_POST['date'];
} ?>
<form name="form" action="" method="post">
<label class="line">
<span class="data">Name:</span>
<input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
</label>
<label class="line">
<span class="data">Content:</span>
<textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
</label>
<label class="line">
<span class="data">Date:</span>
<input type="text" name="date" value="<?php if (isset($f['date'])) {
echo $f['date'];
} else {
echo date('d/m/Y H:i:s');
} ?>"/>
</label>
<input type="submit" value="Create" name="sendForm" class="btn"/>
</form>
In short you can set by this way,
<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
You do not need to use $f['name']. you can get value directly by $_POST['name'] method.
The script for handling the action of my form redirects to the form page if the values are not in proper format. I want to fill the textfields and textarea with the faulty data the user entered on redirect to the form page. I have written the following script which redirects the page on wrong value submission, but does not fill the fields thereafter.
script on form page:
<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>
The html part of the form:
<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
</p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
<p>
<label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>
The redirecting script on addscript.php to which the form values are submitted:
<?php
// Get values from form
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];
$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Please telll me where is the problem and how can I solve the issue?
Thanks in advance.
session_start() must be called at the top of your PHP script to use the $_SESSION variable.