I want to implement a simple login with PHP. But, also I want to customize the error message when the login is incorrect, for example display a error message in red color.
Login.php
<form id="frmlogin" name="frmlogin" method="POST" action="validarUsuario.php">
<p><input type="text" name="usuario" id="usuario" class="required" maxlength="50" placeholder="Nombre Usuario"></p>
<p><input type="password" name="password" id="password" class="required" maxlength="50" placeholder="Password"></p>
<p id=error> </p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
validarUsuario.php
<?php
include("connection.php");
conectar_bd();
$usr = $_POST['usuario'];
$pw = md5($_POST['password']);
$sql = "SELECT usr FROM Client WHERE usr = '$usr'AND password ='$pw'";
$result = mysql_query($sql, $con);
if ($fila = mysql_fetch_array($result)) {
header('Location: menuclient.php');
} else {
/* How customize my message error */
}
?>
In the Login.php file, when the username or password are not correct, i want to display, for example, in id=error : "Login Incorrect" in red.
I dont know if I can do it with php.
Thanks for your help.
You can use session. Try following way.
Login.php
<?php
session_start();
if($_SESSON['error']): ?>
<span>Error : <?php echo $_SESSION['errorMsg']; ?></span>
<?php endif; ?>
<form id="frmlogin" name="frmlogin" method="POST" action="validarUsuario.php">
<p><input type="text" name="usuario" id="usuario" class="required" maxlength="50" placeholder="Nombre Usuario"></p>
<p><input type="password" name="password" id="password" class="required" maxlength="50" placeholder="Password"></p>
<p id=error> </p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
validarUsuario.php
<?php
session_start();
include("connection.php");
conectar_bd();
$usr = $_POST['usuario'];
$pw = md5($_POST['password']);
$sql = "SELECT usr FROM Client WHERE usr = '$usr'AND password ='$pw'";
$result=mysql_query($sql,$con);
if( $fila=mysql_fetch_array($result) )
{
header('Location: menuclient.php');
}
else {
$_SESSION['errorMsg'] = "Wrong username or Password";
$_SESSION['error'] = true;
header('Location: Login.php');
}
?>
Related
I had a normal site that I built with custom HTML/CSS/JS. I changed the site to become a WordPress site (and made my custom html/css/js into a custom WordPress theme). I had a contact form on the homepage of the site, which broke when I made the change to WordPress.
When the form was functioning, it would refresh the page and bring the user back down to the form (using the anchor id=contactphilly) and then the user would see a message confirming their email was sent.
I did a LOT of Googling and I think it has something to do with the "action" option in the form, but no matter what I try changing it to, it doesn't work. I tried the following for the action value:
<?php the_permalink(); ?>/#contactphilly
#contactphilly
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>
$_SERVER['REQUEST_URI']
None of them worked.
Here is the code for the form:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'WebKeep.com';
$to = 'info#webkeepteam.com';
$subjectemail = 'Message from WebKeepTeam.com Contact Form ';
$body = "From: $name\n E-Mail: $email\n Subject: $subject\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subjectemail, $body, $from)) {
$result='<div>Thank You! We will respond shortly.</div>';
} else {
$result='<div>Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
and
<form name="contactform" method="post" action="#contactphilly">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<fieldset class="form-group">
<input type="text" value="<?php echo $name;?>" class="form-control transparent-input" id="name" name="name" placeholder="Name">
</fieldset>
<?php echo "<p class='text-danger'>$errName</p>";?>
<fieldset class="form-group">
<input type="email" value="<?php echo $email;?>" class="form-control transparent-input" id="email" name="email" placeholder="Email">
</fieldset>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
<fieldset class="form-group">
<input type="text" class="form-control transparent-input" value="<?php echo $subject;?>" id="subject" name="subject" placeholder="Subject">
</fieldset>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<fieldset class="form-group">
<textarea class="form-control transparent-input" id="message" value="<?php echo $message;?>" name="message" rows="6" placeholder="Message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
</fieldset>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-index pull-right">
</div>
<?php echo $result; ?>
</form>
I found an answer to my own question! I completely forgot about this bit of code on GitHub: https://github.com/dwyl/html-form-send-email-via-google-script-without-server I used it elsewhere on the site and it worked, and it also works beautifully for forms within pages too!
This question already has answers here:
Create or write/append in text file
(7 answers)
Closed 5 years ago.
when I hit the post button, comment is posting on the page. If I hit it again, it overriding the previous comment. can someone please clarify this? And I'm running java script to display the date. It is showing all the time without any comments also. How can I modify that too?
Thanks in advance!
<h3 id="reply-title" class="comment-reply-title">Leave a Reply </h3>
<form action="" method="post">
<p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p>
<p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p>
<p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" aria-required="true" required="required"></p>
<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" aria-required="true" required="required"></p>
<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment"></p>
<div>
<?php
if($_POST) {
$name = $_POST['author'];
$comment = $_POST['comment'];
$email = $_POST['email'];
#echo $name . $comment . $email;
$handle = fopen("comments/get-more-twitter-followers.html", "a");
fwrite($handle, "\n".$name."\n".$comment."\n". $email. "\n");
fclose($handle);
}
?>
<?php
if($_POST) {
$email = $_POST['email'];
$useremail = fopen("useremail.html", "a");
fwrite($useremail, "\n".$email. "\n");
fclose($useremail);
}
?>
</div>
<div>
<div>
<p><strong><h4><u>Comments:</u></h4></strong></p>
</div>
<div>
<div>
<strong><?php
$name = $_POST['author'];
echo $name; ?> </strong> <small><script type="text/javascript">
var d = new Date()
document.write(d.getDate())
document.write("/")
document.write(d.getMonth() + 1)
document.write("/")
document.write(d.getFullYear())
</script>
</small>
</div>
<div>
<p>
<?php
$comment = $_POST['comment'];
echo "\n".$comment."\n"."<hr>";
?>
</p>
</div>
</div>
</div>
Whay do you mean with; 'Your email address will not be published.' You write all 'mail addresses' to a plaintext file for crying out loud. DONT PUT THIS ONLINE.
You're not reading the file back to paint the comments, you are just using the POST data that came in:
<div>
<strong><?php
$name = $_POST['author'];
echo $name; ?> </strong> <small><script type="text/javascript">
var d = new Date()
document.write(d.getDate())
document.write("/")
document.write(d.getMonth() + 1)
document.write("/")
document.write(d.getFullYear())
</script>
</small>
</div>
<div>
<p><?php
$comment = $_POST['comment'];
echo "\n".$comment."\n"."<hr>";
?></p></div>
</div></div>
</div>
$_POST in PHP refers to the body of the POST request that the script is responding to.
You need to replace that with functionality that does three things:
Reads from the file.
Splits the records into different entries with an author and comment.
Iterates over those entries and paints a separate div for each record.
Another gotcha: Your code is printing the current date as the date of the comment. If you are not storing the date of the comments, you probably shouldn't show the date.
<?php
$comment = $_POST['comment'];
$name = $_POST['author'];
$data_post->name = $name;
$data_post->comment = $comment;
if(isset($name, $comment)){
header('Content-type: application/json');
echo(json_encode($data_post));
return;
}
?>
<html>
<head>
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
<script>
$("document").ready(function(){
$("#comment").on("submit", function(e){
var comment = $('#comment');
e.preventDefault();
$.ajax({
method: "POST",
url: "index.php", // your php file name
data: comment.serialize(),
success: function (data) {
var d = new Date();
var data_comment = "<div><small>"+d.getDate()+"/"+(d.getMonth() + 1)+"/"+d.getFullYear()+"</small></div><strong>"+data.name+"</strong><p>"+data.comment+"</p>"
$("#list_comment").append(data_comment);
comment[0].reset();
console.log(data.name);
},
error: function (data) {
},
})
});
});
</script>
</head>
<body>
<h3 id="reply-title" class="comment-reply-title">Leave a Reply </h3>
<form action="" method="post" id="comment">
<p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p>
<p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p>
<p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" aria-required="true" required="required"></p>
<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" aria-required="true" required="required"></p>
<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment"></p>
</form>
<div>
<div>
<p><strong><h4><u>Comments:</u></h4></strong></p>
</div>
<div id="list_comment">
<div>
</div>
</body>
</html>
don't forget to change url in ajax post with your php filename (line 26)
I purchased a theme, and I need to make a processing.php for my contact form. The form that sends the data to the server for processing.
This is the form code:
<div class="form_error text-center">
<div class="name_error hide error">Please Enter your name</div>
<div class="email_error hide error">Please Enter your Email</div>
<div class="email_val_error hide error">Please Enter a Valid Email Address</div>
<div class="message_error hide error">Please Enter Your Message</div>
</div>
<div class="Sucess"></div>
<form role="form">
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" id="name" placeholder="Name">
<input type="email" class="form-control" id="email" placeholder="Email">
<input type="text" class="form-control" id="subject" placeholder="Subject">
</div>
<div class="col-md-8">
<textarea class="form-control" id="message" rows="25" cols="10" placeholder=" Message Texts..."></textarea>
<button type="button" class="btn btn-default submit-btn form_submit">SEND MESSAGE</button>
</div>
</div>
</form>
Both shubham and adi's answers are vulnerable to SMTP header injections, allowing your contact form to be used to send spam. It will happen.
Any POST or GET data passed to the additional_headers parameter needs to be sanitised to prevent abuse.
See the following:
http://php.net/manual/en/function.mail.php
https://www.phpsecure.info/v2/article/MailHeadersInject.en.php
see this example
<?php
if(isset($_POST['submit'])){
$to = "email#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if (isset($_POST['contact_name'])&&isset($_POST['contact_mail'])&&isset($_POST['message'])) {
$contact_name = $_POST['contact_name'];
$contact_mail = $_POST['contact_mail'];
$message = $_POST['message'];
if (!empty($contact_name) && !empty($contact_mail) && !empty($message)) {
$to = 'your email#gmail.com'; // this is where you want to send the email
$subject = 'Contact form Submitted';
$body = $contact_name."\n".$message;
$headers = 'From:'.$contact_mail; // email of the sender
if (#mail($to, $subject, $body, $headers)) {
echo 'Thanks for contacting us';
}
else {
echo 'Sorry, an error has been occurred';
}
}
else {
echo 'All fields are required';
}
}
?>
<form action="index.php" method="POST">
Name:<br>
<input type="text" name="contact_name"></input><br><br>
Email Address:<br>
<input type="text" name="contact_mail"></input><br><br>
Message:<br>
<textarea name="message" cols="30" rows="6"></textarea><br><br>
<input type="submit" value="Send"></input>
</form>
maybe this codes can help you
i would like to made a pop-up window for my login and signup sites. but there's a problem with this code, can't post the data from the form and the submit button didn't work when clicked. someone help me please. thanks.
here it is the index.html with the pop-up:
<div class="popup">
<h2>Welcome Guest!</h2>
<form id="formlogin" name="formlogin" method="POST" action="login_proses.php" enctype="multipart/form-data" autocomplete="on">
<p>Please enter your login and password here</p>
<p>
<label for="username">Login</label>
<input type="text" id="username" name="username" value="" required />
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password" value="" required/>
</p>
<p>
<input type="submit" value="Log In" onclick="popupUploadForm()">
</p>
</form>
<a class="close" href="#close"></a>
</div>
<script>
function popupUploadForm(){
var newWindow = window.location.href='login_proses.php';
}
</script>
and here it is login_proses.php that pick the data form, but it's always failed, and always go into index.html as else condition.
<?php
session_start();
include "connect.php";
if (isset($_POST["username"])){
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql=mysql_query("select * from member where username='$username'");
if ($sql && mysql_num_rows($sql)>0){
$result=mysql_fetch_array($sql);
if($password == $result['password']){
$_SESSION['username']=$username;
echo "<script type='text/javascript'>alert('Selamat Datang $username');
window.location.href='member.php';</script>";
}
else{
echo "<script type='text/javascript'>alert('Password salah, silahkan coba lagi');history.go(-1)</script>" ;
}
}
else{
echo "<script type='text/javascript'>alert('Username tidak ada dalam database');history.go(-1)</script>" ;
}
} else {
header("location:index.html");
} ?>
I am creating a simple enquiry form with upload option.
If I click submit button it goes to another page and displays success message but it should come in the same page as a message box. What should I do?
Here is my Html code:
<ul>
<li><span>Name</span>
<input name="name" type="text" size="25" class="text1" />
</li>
<li><span>Contact</span>
<input name="contact" type="text" size="25" class="text2" />
</li>
<li><span>E-mail</span>
<input name="email" type="text" size="25" class="text3" />
</li>
<li><span>Subject</span>
<input name="subject" type="text" size="25" class="text4" />
</li>
<li><span>Message</span>
<textarea name="message" size="250" class="text5"></textarea>
</li>
</ul>
<label for="upload"></label>
<div class="x">
<input type="file" name="upload" id="file" class="button">
</div>
<div class="x1">
<input type="submit" name="submit" value="Submit" class="button" />
</div>
And this is my PHP code for uploading and inserting:
<?php
$target = "upload/";
$target = $target . basename($_FILES['upload']['name']);
$ok = 1;
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{
echo "The file " . basename($_FILES['upload']['name']) . " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>
<?php
include ("conn.php");
if (isset($_POST['submit']))
{
$sql="insert into cform (name,contact,email,subject,message,upload)values('".$_POST['name']."','".$_POST['contact']."','".$_ POST['email']."','".$_POST['subject']."','".$_POST['message']."','$target')";
$result=mysql_query($sql);
if (!$result)
{
echo "could not enter data";
}
else
{
echo "successfuly entered";
}
}
?>
This PHP page has to run in background and when I click the submit button a message box should display success.
Edit the echo portion of the page as
second page
$rspnse = "";
if(!$result){
$rspnse = "Error";
}
else{
$rspnse = "Success";
}
header('Location: previous_page.php?response='.$rspnse);
Replace the previous_page.php with your original url of the first page
first page
<?php
if(isset($_GET['response'])){
?>
<script>
alert('<?php echo $_GET['response'] ?>');
</script>
<?php
}
?>
Hope this helps...
if(! $result) {
$msg = "could not enter data";
}else{
$msg = "successfuly entered";
}
header('Location: http://www.example.com/form.php?message=$msg');
//Redirect to form page with set message.
add script in form page to display message
if(isset($_GET['message'])){
print $_GET['message'];
}
This solution for as per your code
And another option is using ajax.