PHP form processing and validation is not working - php

I have the following code for my form, and it doesn't seem to work, the query does not insert after all the fields have been filled. I'll really appreciate it if someone can look at it and help me correct any mistake. The method for the form is post and the action is <?php echo $_SERVER['PHP_SELF'];?>. This is my first code in PHP so kind of new here Thanks.
<?php
$counter=1;
if (isset($_POST["post"])) {
$gender= $_POST['gender'];
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$email= $_POST['email'];
$fone= $_POST['fone'];
$school= $_POST['skuul'];
$other= $_POST['other'];
$output_form=false;
$is_error = false;
if (empty($email)) {
echo "Please enter your email or contact info. \n";
$output_form = $is_error = true;
}
if (empty($lname)) {
echo "Please enter your email or contact info. \n";
$output_form= $is_error = true;
}
if (empty($fone)) {
echo "Please enter your email or contact info. \n";
$output_form= $is_error = true;
}
if (empty($fname)) {
echo "Please enter your name. \n";
$output_form= $is_error = true;
}
if ($output_form) {
$query= "INSERT INTO 'elect_conference' ('gender', 'fname', 'lname', 'phone', 'email', 'School', 'course', 'other')".
"values ('$gender', '$fname', '$lname', '$fone', '$email', '$school', '$course', '$other');";
$result = execute($query);
echo '<article class="extra-wrap">'."Hello $name congratulations, you are the number $counter member registered for the ELECT Conference.
A confirmation will be sent to your email ($email) within five(5) hours.\n
Thank you. And have a nice day.</artice>";
$fname="";
$lname="";
$fone="";
$email="";
$fone="";
$school="";
$subject="";
$counter++;
} else {
echo "Please ensure that all the fields are approprietly filled";}
} else {
$output_form=true;
}
if ($output_form) {
?>
<body id="page2">
<!--==============================header=================================-->
<section id="content">
<div class="pad">
<div class="main">
<article class="grid_9 suffix_1">
<div class="form1">
<div class="padding">
<h4 class="border-bot2 img-indent-bot" align="center" style="color:#00C !important">
ELECT Conference <br/>Registration
</h4>
<form id="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"enctype="multipart/form-data">
<fieldset>
<div style="margin-left:90px !important;"><?php echo "$counter/500"; ?></div>
<div class="rowElem0">
<span class="radio">Personal Infomation:</span>
<span class="radio2">
<input type="radio" name="gender" value="Mr" checked>
<label class="ratio">Mr</label>
<input type="radio" name="gender" value="Mrs">
<label class="ratio">Ms</label>
<input type="radio" name="gender" value="Mrs">
<label class="ratio">Mrs</label>
</span>
</div>
<div class="rowElem">
<label><span class="input">First Name:</span></label>
<input type="text" name="fname" value="<?php echo $fname; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">Last Name:</span></label>
<input type="text" name="lname" value="<?php echo $lname; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">Phone:</span></label>
<input type="text" name="fone" value="<?php echo $fone; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">E-mail:</span></label>
<input type="text" name="email" value="<?php echo $email; ?>"/>
</div>
<div class="rowElem1">
<input type="radio" name="graduate" id="chbox4" checked>
<strong><label class="check2">Graduate?</label></strong>
</div>
<div class="rowElem">
<label><span class="input">School:</span></label>
<input type="text" name="skuul" value="<?php echo $school; ?>"/>
</div>
<div class="rowElem">
<label><span class="input">Course:</span></label>
<input type="text" name="course" value="<?php echo $school; ?>"/>
</div>
<div class="rowElem1">
<input type="radio" name="other" id="chbox5">
<strong><label class="check2">Other</label></strong>
<label><span class="input">Specify:</span></label>
<input type="text" name="other" value="<?php echo $other; ?>"/>
</div>
<div class="buttons">
<input class="button" type="submit" value="Submit" />
</div>
</fieldset>
</form>
</div>
</div>
</article>
</div>
</div>
</div>
</section>
<script type="text/javascript"> Cufon.now(); </script>
</body>
<?php
}
?>
</html>

Please show the form code, and be specific in the outcome of the form processing. What exactly didn't work?
In the mean time here are a couple of observations:
Instead of assigning each $_POST element at a time you can do this:
foreach ($_POST as $key=>$value) $$key = $value;
*$output_form* and *$is_error* always have the same value, why the duplication?

Related

I have two tables,from that am fetching data with left join

post id is the common field in both the tables.If I want to select the comments under same post_id,only 1 time the post should display and all its comments under it..
this is my code....
home.php
<?php
$sql="SELECT family_news.post_id,family_news.username,family_news.post,
family_news.fileToUpload,family_news.description,
family_comments.post_id,family_comments.comment,
family_comments.commenter
FROM family_news
LEFT JOIN family_comments ON family_news.post_id = family_comments.post_id
ORDER BY family_news.post_id DESC";
if($result = mysqli_query($con, $sql))
{
while($row = mysqli_fetch_array($result))
{
?>
<div class="form-group">
<b style="font-size:30px;color:brown;">
<?php
echo $row['username'];
?>
</b>
<br>
<?php
if($row['fileToUpload'] != NULL)
{
?>
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<br>
<img class="img-responsive" src="../admin_panel/uploads/<?php echo $row['fileToUpload']; ?>" alt="Image Loading" height='80' width='120'>
<div class="form-group" style="font-size:15px;color:purple;">
<?php
echo $row['description'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</div>
</form>
<?php
}
if($row['post'] != NULL)
{
?>
<br>
<div class="form-group" style="font-size:15px;color:purple;">
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<?php
echo $row['post'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</form>
<?php
}
?>
</div>
<?php
}
}
?>
</div>
</section>
post_comment.php
<section class="col-lg-6 connectedSortable">
<?php
$post_id=$_POST['post_id'];
$comment=$_POST['comment'];
$commenter=$_POST['commenter'];
if($comment != NULL) {
$sql="INSERT INTO family_comments
(post_id, comment, commenter)
VALUES ('$post_id', '$comment', '$commenter')";
if($con ->query($sql) == TRUE)
{
echo 'Comment saved Successfully...';
}
} else {
echo 'Please Write Something...';
}
?>
</section>

php script change when submitting a form data

I built a script named add_post.php that submit form data in database.
The script is here:
<?php include ("includes/header.php"); ?>
<div class="content" style="text-align: center;">
<header>
<h1 style="color: rgb(245, 106, 106);">ADMIN AREA</h1><br/>
<p>Movie Post Area</p>
</header>
</div>
</section>
<?php
include("includes/startsession.php");
if(isset($_SESSION['username'])){
?>
<Section>
<?php
if(isset($_POST['submit'])){
$name =mysqli_real_escape_string($con, trim($_POST['name']));
$description = mysqli_real_escape_string($con, trim($_POST['description']));
$year = mysqli_real_escape_string($con, trim($_POST['year']));
$popularity = mysqli_real_escape_string($con, trim($_POST['popularity']));
$download_link = mysqli_real_escape_string($con, trim($_POST['download']));
$tags = mysqli_real_escape_string($con, trim($_POST['tags']));
//upload the name to the temporary folder
$images = mysqli_real_escape_string($con, trim($_FILES['image']['name']));
//select the image from the temporary folder
$images_tmp = $_FILES['image']['tmp_name'];
//move the uploaded image to the `images` folder
move_uploaded_file($images_tmp, "../images/$images");
$output_form = false;
if( empty($name) || empty($description) || empty($year) || empty($popularity) || empty($download_link) || empty($tags) || empty($images)){
echo '<h2>Oops! You seem to forget writing something</h2>';
$output_form = true;
}
if( (!empty($name)) && (!empty($description)) && (!empty($year)) && (!empty($popularity)) && (!empty($download_link)) && (!empty($tags)) && (!empty($images))){
$insert = "INSERT INTO movies (name, description, year, image, popularity, download_link, tags)
VALUES ('$name', '$description', '$year', '$images', '$popularity', '$download_link', '$tags') " or die(mysqli_error);
$run = mysqli_query($con, $insert);
echo "<h2>The Movie has been added.<h2>";
echo 'RESET';
}
} else{
$output_form = true;
$name = '';
$description = '';
$year = '';
$popularity = '';
$download_link = '';
$tags = '';
$images = '';
}
?>
<?php if($output_form = true){ ?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="row uniform">
<div class="6u 12u$(xsmall)" style="float:none">
<label>Movie Name</label>
<input type="text" name="name" value="<?php echo $name; ?>" id="demo-name" value="" placeholder="Enter Movie Name" />
</div>
<br/>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Description</label>
<textarea name="description" id="demo-message" placeholder="Enter Description" rows="6"><?php echo $description; ?></textarea>
</div>
<br/>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Year</label>
<input type="text" name="year" value="<?php echo $year; ?>" id="demo-name" placeholder="Enter Year" />
</div>
<br/>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Image</label>
<input type="file" name="image" value="<?php echo $images; ?>" id="demo-name" class="special" />
</div>
<div class="12u$" style="float:none">
<div class="select-wrapper">
<label>Popularity</label>
<select name="popularity" id="demo-category">
<option <?php if ($popularity == 'Yes'){echo 'selected';} ?> >Yes</option>
<option <?php if ($popularity == 'No'){echo 'selected';} ?> >No</option>
</select>
</div>
</div>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Download Link</label>
<input type="text" name="download" value="<?php echo $download_link; ?>" id="demo-name" value="" placeholder="Enter Download Link" />
</div>
<div class="6u 12u$(xsmall)" style="float:none">
<label>Tags</label>
<input type="text" name="tags" value="<?php echo $tags; ?>" id="demo-name" value="" placeholder="Enter Tags seperated by commas(,)" />
</div>
<!-- Break -->
<div class="12u$" style="float:none">
<ul class="actions">
<li><input type="submit" name="submit" value="ADD MOVIE" class="special" /></li>
</ul>
</div>
</div>
</form>
<?php } ?>
<?php
} else{
header('Location: login.php');
}
?>
when I submit the form the page returns the script of index.php. When i refresh the page then the page shows the content of add_post.php and above all the data content of the form did not add to the database.
I don't know whats causing this problem and how to fix it.

PHP : Login in to the admin page

I am trying to login into the admin and lecturer page in the following code.But it is not working properly.When i login after entering loginid and password, click on submit , no error occured.
admin.php
<?php
session_start();
if(isset($_SESSION["userid"]))
{
if($_SESSION["type"]=="admin")
{
header("Location: dashboard.php");
}
else
{
header("Location: lectureaccount.php");
}
}
include("header.php");
include("conection.php");
if(isset($_POST["uid"]) && isset($_POST["pwd"]) )
{
// echo "sdfsd". $_POST[uid];
$result = mysql_query("SELECT * FROM administrator WHERE adminid='$_POST[uid]'");
while($row = mysql_fetch_array($result))
{
$pwdmd5 = $row["password"];
}
if(md5($_POST["pwd"])=='$pwdmd5')
{
$_SESSION["userid"] = $_POST["uid"];
$_SESSION["type"]="admin";
header("Location: dashboard.php");
}
else
{
$log = "Login failed.. Please try again..";
}
}
if(isset($_POST["luid"]) && isset($_POST["lpwd"]))
{
$result = mysql_query("SELECT * FROM lectures WHERE lecid='$_POST[luid]'");
while($row = mysql_fetch_array($result))
{
$pwdm= $row["password"];
$_SESSION["lecname"] = $row["lecname"];
$_SESSION["coid"] = $row["courseid"];
}
//echo"pwd". md5($_POST["lpwd"]);
if(md5($_POST["lpwd"])==$pwdm)
{
//echo $_POST["lpwd"];
$_SESSION["userid"] = $_POST["luid"];
$_SESSION["type"]=="lecturer";
header("Location: lectureaccount.php");
}
else
{
$log12 = "Login failed.. Please try again..";
}
}
?>
<section id="page">
<header id="pageheader" class="normalheader">
<h2 class="sitedescription">
</h2>
</header>
<section id="contents">
<article class="post">
<header class="postheader">
<h2><u>Admin Login</u></h2>
<?php $log = isset($_POST['log']) ?>
<h2><?php echo $log;?></h2>
</header>
<section class="entry">
<form action="admin.php" method="post" class="form">
<p class="textfield">
<label for="author">
<small>Admin Login ID (required)</small>
</label>
<input name="uid" id="uid" value="" size="22" tabindex="1" type="text">
</p>
<p class="textfield">
<label for="email">
<small>Password (required)</small>
</label>
<input name="pwd" id="pwd" value="" size="22" tabindex="2" type="password">
</p>
<p>
<input name="submit" id="submit" tabindex="5" type="image" src="images/submit.png">
<input name="comment_post_ID" value="1" type="hidden">
</p>
<div class="clear"></div>
</form>
<form action="admin.php" method="post" class="form">
<div class="clear">
<hr />
<header class="postheader">
<h2><u>Lectures Login</u></h2>
<?php $log12 = isset($_POST['log12']) ?>
<h2><?php echo $log12;?></h2>
</header>
<section class="entry">
<p class="textfield">
<label for="author2"> <small><br />
Lecture Login ID (required)</small> </label>
<input name="luid" id="luid" value="" size="22" tabindex="3" type="text" />
</p>
<p class="textfield">
<label for="email2"> <small>Password (required)</small> </label>
<input name="lpwd" id="lpwd" size="22" tabindex="4" type="password" />
</p>
<p>
<input name="submit2" id="submit2" tabindex="5" type="image" src="images/submit.png" />
<input name="comment_post_ID2" value="1" type="hidden" />
</p>
<div class="clear"></div>
</form>
<div class="clear"></div>
</section>
</div>
</section>
</article>
</section>
<?php
include("adminmenu.php");
include("footer.php"); ?>
Database:
table fields in administrator are: adminid,password,adminname,address,contactno
table fields in lectures are:
lecid,password,courseid,lecname,gender,address,contactno.
Please provide solution for this issue.
instead of using this code
if(md5($_POST["pwd"])=='$pwdmd5')
use this
if(md5($_POST["pwd"])==$pwdmd5)
In the first case, the hashed pasword is compared to the string $pwdmd5, in the second to the content of $pwdmd5

Form won't submit info to database

I've been building a site recently for a friend and I've gotten stuck on this one form. A button links to url in which this form is on and then once you fill out all the information and click submit, instead of returning you back to home.php it just removes the form from view and all you see is a blank new.php and it doesn't submit the information.
<?php
function renderForm($user, $rank, $position, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<center>
<form action="" method="post">
<div class="form-group">
<label for="username">Username*</label>
<input id="username" class="form-control" type="text" name="user" placeholder="Username" value="<?php echo $user; ?>" />
</div>
<div class="form-group">
<label for="rank">Rank</label>
<select class="form-control" name="rank">
<option value="1">Pending Rank</option>
<option value="2">PVT</option>
</select>
</div>
<div class="form-group">
<label for="position">Position</label>
<input id="position" class="form-control" type="text" name="position" placeholder="MOG/GG" value="<?php echo $position; ?>" />
</div>
<div class="form-group">
<label for="Date">Date*</label>
<input id="Date" class="form-control" type="text" name="date" placeholder="<?php echo date('d M y'); ?>" value="<?php echo $date; ?>" />
</div>
<div class="form-group">
<label for="Tag">Tag*</label>
<input id="Tag" class="form-control" type="text" name="tag" placeholder="[]" value="<?php echo $tag; ?>" />
</div>
<div class="form-group">
<label for="adt">ADT</label>
<input id="adt" class="form-control" type="text" name="adt" placeholder="{TEST}" value="<?php echo $adt; ?>" />
</div>
<div class="form-group">
<label for="exp">EXP</label>
<input id="exp" class="form-control" type="text" name="exp" placeholder="420" value="<?php echo $exp; ?>" />
</div>
<div class="form-group">
<label for="reg">Regiment</label>
<input id="reg" class="form-control" type="text" name="reg" placeholder="[P]" value="<?php echo $reg; ?>" />
</div>
<div class="form-group">
<label for="Notes">Notes</label>
<input id="Notes" class="form-control" type="text" name="notes" placeholder="Notes" value="<?php echo $notes; ?>" />
</div>
<button type="submit" name="submit" class="btn btn-default" value="Submit">Submit</button>
</form>
<script>
$('.modal').on('hidden.bs.modal', function(){
$(this).find('form')[0].reset();
});
</script>
<?php
}
include('config/db.php');
if (isset($_POST['submit']))
{
$user = mysql_real_escape_string(htmlspecialchars($_POST['user']));
$rank = mysql_real_escape_string(htmlspecialchars($_POST['rank']));
$position = mysql_real_escape_string(htmlspecialchars($_POST['position']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$tag = mysql_real_escape_string(htmlspecialchars($_POST['tag']));
$adt = mysql_real_escape_string(htmlspecialchars($_POST['adt']));
$exp = mysql_real_escape_string(htmlspecialchars($_POST['exp']));
$reg = mysql_real_escape_string(htmlspecialchars($_POST['reg']));
$notes = mysql_real_escape_string(htmlspecialchars($_POST['notes']));
$datej = mysql_real_escape_string(htmlspecialchars($_POST['date']));
if ($user == '' || $rank == '' || $date == '' || $tag == '')
{
$error = '<center>ERROR: Please fill in all required fields!</center>';
#renderForm($user, $rank, $position, $error);
}
else
{
mysql_query("INSERT per SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', adt='$adt', exp='$exp', reg='$reg', notes='$notes', datej='$datej'", $db1)
or die(mysql_error());
include('logsadd.php');
write_mysql_log('has added member <font color="black"><b>'. $user .'</b></font>.', $db);
header("Location: home.php");
}
}
else
header("home.php");
{
#renderForm('','','');
}?>
Your else looks like this
else
header("home.php");
{
#renderForm('','','');
it should be
else
{
// header should be inside the else part
header("Location:home.php");
#renderForm('','','');

Hide or clear a PHP form on submit

I have a php contact form which all works well with its validation etc, but I have one niggle with it. The original code which I have tweaked redirected the page to a new one with a thank you message on submission which I was unhappy with, so I've managed to get a thank you message to display on the original page, however the input form content still stays, i'd rather it didn't. Even better I'd like to be able to hide the form completely and replace it with a thank you.
I ought to mention that when completed it will be placed on a page with other items, so it is just the form that I'm after hiding, or clearing.
This is the code in the Header
<?php
$your_email ='email#example.com';
session_start();
$errors = '';
$name = '';
$company = '';
$visitor_email = '';
$phone = '';
$user_message = '';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$user_message = $_POST['message'];
///------------Do Validations-------------
if(empty($name)||empty($visitor_email))
{
$errors .= "\n Name and Email are required fields. ";
}
if(IsInjected($visitor_email))
{
$errors .= "\n Bad email value!";
}
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
//Note: the captcha code is compared case insensitively.
//if you want case sensitive match, update the check above to
// strcmp()
$errors .= "\n The captcha code does not match!";
}
if(empty($errors))
{
//send the email
$to = $your_email;
$subject="New form submission";
$from = $your_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A user $name submitted the contact form:\n".
"Name: $name\n".
"Company: $company\n".
"Email: $visitor_email \n".
"Phone: $phone\n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
//header('Location: #thanks');
$myForm = '< style="visibility: hidden;">';
$thankyou = file_get_contents("thank-you.html");
}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
And this is the form itself minus a chunk of javascript validation which I didn't think was relevant to the question
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id="footer">
<div class="twelve-column-wrapper">
<div class="six-column-wrapper">
<div class="six-column">
<h3>Why not get in touch</h3>
</div>
<div id='contact_form_errorloc' class='err'></div>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<div class="three-column">
<p>
<label for='name'>Name: </label>
<br>
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='company'>Your Company: </label>
<input type="text" name="company" id="company" value='<?php echo htmlentities($company) ?>'/>
</p>
</div>
<div class="three-column">
<p>
<label for='email'>Email: </label>
<br>
<input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='phone'>Phone No. </label>
<input type="text" name="phone" id="phone" value='<?php echo htmlentities($phone) ?>'/>
</p>
</div>
<div class="six-column">
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
</p>
</div>
<div class="three-column">
<p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label>
<br>
<input id="6_letters_code" name="6_letters_code" type="text">
<br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p>
<input id="submit" type="submit" value="Submit" name='submit'>
</div>
<div class="six-column"> <?php echo $thankyou; ?> </div>
</form>
</div>
</div>
</div>
I have tried a few methods I've found by searching but have fallen down mainly due to my basic knowledge of PHP.
Any help would be much appreciated.
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id="footer">
<div class="twelve-column-wrapper">
<div class="six-column-wrapper">
<div class="six-column">
<h3>Why not get in touch</h3>
</div>
<div id='contact_form_errorloc' class='err'></div>
<?php
if(!isset($_POST['submit'])):
?>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<div class="three-column">
<p>
<label for='name'>Name: </label>
<br>
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='company'>Your Company: </label>
<input type="text" name="company" id="company" value='<?php echo htmlentities($company) ?>'/>
</p>
</div>
<div class="three-column">
<p>
<label for='email'>Email: </label>
<br>
<input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='phone'>Phone No. </label>
<input type="text" name="phone" id="phone" value='<?php echo htmlentities($phone) ?>'/>
</p>
</div>
<div class="six-column">
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
</p>
</div>
<div class="three-column">
<p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label>
<br>
<input id="6_letters_code" name="6_letters_code" type="text">
<br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p>
<input id="submit" type="submit" value="Submit" name='submit'>
</div>
</form>
<?php
endif;
?>
<div class="six-column"> <?php echo $thankyou; ?> </div>
</div>
</div>
</div>
That will hide the form. But that won't prevent you to be spammed. If you don't want to be spammed to have to track IPs and before submitting the email check that the IP didn't already send an email let's say in the last 30 seconds, for instance.
if(empty($_POST)){
//You form goes here;
}
else{
echo $thankyou;
}
Also you can have something like:
if(empty($thankyou)){
//You form goes here;
}
else{
echo $thankyou;
}
You can trigger this function onsubmit or onclick event on form
<script>
function hideform()
{
document.forms['contact_form'].style.visibility = 'hidden';
}
</script>
You could use jQuery to clear the form div on a successful submission.
$('#myform').submit(function(){
$('#formcontainer').empty;
});
Where myform and form container are the ids of your form and the div containing your form
You better create separate action file and for display thanks message use:
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
}
else{
//display from here
}

Categories