I am trying to implement a simple yet effective contact form. The HTML for the page is below. Also the mail.php form I am using is listed below as well. What I want the contact form to do is require fields, and if the user did not fill in the required fields it sends them back to the same form with the error code. I think I'm close, but for some reason it's giving me an error code when submitting the form, filled out or empty. You can see it live, instead of using Apache server at darthvixcustomsabers.com and see. I also would love to be able to specify the cols/rows in css by using textarea, but that is not working either.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo ;
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("horgantm#gmail.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
<!doctype html>
<html>
<head>
<title> DV Custom Sabers </title>
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="style/kotorsale.css" />
<meta name="viewport" content="width=device-width" />
</head>
<div class="header">Darthvix Custom Sabers</div>
<div class="header1">DV Custom Sabers is the place to go for your saber needs!</div>
<div class="logo"><img src="images/logo.png" alt="schedule" height="200" width="350" border="0"></div>
<ul id="nav">
<li>Home</li>
<li>About Me</li>
<li>Services</li>
<li>Gallery</li>
<li>For Sale</li>
<li>Build Log</li>
<li> Contact Us</li>
</ul>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
</div>
</body>
</html>
Hi why you are using this line of code?
<form action="mail.php" method="POST" enctype="multipart/form-data">
you are sending only the data there is no image of file to be send, its better use this
<form action="mail.php" method="POST">
remove the enctype . enctype is use only if there is an image or file ...
use the correct tag
You haven't quite presented your information in a clear manner, but hopefully I understand your problems correctly.
First, your form action in contact.html needs to point to your PHP email script. The following:
<form action="" method="POST" enctype="multipart/form-data">
Should be:
<form action="mail.php" method="POST" enctype="multipart/form-data">
You are missing quotes in the following echo statement:
echo ;
This should be:
echo '';
Also, if you are trying to redirect the user back to the form if any fields are blank, then you will need more than just an empty link displayed on the page. An easy way would be to replace the following:
echo '';
With:
header( 'location:contact.html' );
This will direct the user back to the origin contact form. Unfortunately, this would be confusing to a user. You may want to change contact.html to contact.php so you can leverage PHP to display error messages.
Related
I'm struggling to create a two-paged form using $_SESSION. What I want to achieve is the first page (page1.php) requires the user to enter his/her email address. And the second page (page2.php) requires the user to enter his/her password.
When the user submits page1.php, it takes you to page2.php, where the email address submitted will be printed. But unfortunately, the email address is not printed, as intended.
Please, note I've tried to adopt related resolved threads, but I'm still missing something.
<?php
session_start();
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>2 Step Login - Page 1</title>
<link href="page1.css" rel="stylesheet">
</head>
<body>
<div id="formwrap">
<div id="form_inner">
<div id="logo">
<img src="" alt="logo">
</div>
<div id="email">
</div>
<div id="pwd">
Sign in
</div>
<div id="form">
<form action="page2.php" method="post" enctype="multipart/form-data" name="form">
<?php
//On page 1
$_SESSION['username'] = $var_value;
?>
<input id="username" name="username" type="text" placeholder="Email" autofocus required>
<div id="forgot">No Yet A Member, Register Here</a></div>
<input type="hidden" name="username" value="var_value">
<input id="send" name="submit" type="submit" value="Next">
</form>
</div>
</div>
</div>
</body>
</html>
<?php
session_start();
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>2 Step Login - Page 2</title>
<link href="page2.css" rel="stylesheet">
</head>
<body>
<div id="formwrap">
<div id="form_inner">
<div id="logo">
<img src="" alt="logo">
</div>
<div id="email">
<?php
//On age 2
$var_value = $_SESSION['username'];
echo $_SESSION['username'];
?>
</div>
<div id="pwd">
Enter password
</div>
<div id="form">
<form action="login.php" method="post" enctype="multipart/form-data" name="form">
<input id="password" name="password" type="password" placeholder="Password" autofocus>
<div id="chkbx">
<div id="inptch">
<input id="keep_signed_in" name="keep_signed_in" type="checkbox" value="">
</div>
Keep me signed in</div>
<div id="forgot">I Forgot My Password</div>
<div id="different_account">Not A Member, Register Here</div>
<input type="hidden" name="username" value="var_value">
<input type="hidden" name="password" value="var_value">
<input id="send" name="submit" type="submit" value="Sign in">
</form>
</div>
</div>
</div>
</body>
</html>
From your code, it looks like you've skipped the step from using the POST value from the first form before getting to the second.
After a form is submitted, in PHP a $_POST variable contains the values of what was in the form from the keys of the name in the form.
For example <input name="myinput"> in a form set to POST will result in $_POST['myinput'] containing the value from the submit you submit.
The simple way to achieve what you want would be to just post the value from page1.php to page2.php, and then it looks like you handle the login on a login.php. For example:
page1.php
<?php
if (isset($_GET['invalidEmail'])) {
echo "Error: Invalid email address entered.";
}
?>
<form action="page2.php" method="post">
<input type="email" type="email" />
<input type="submit" value="Continue" />
</form>
page2.php
<?php
$email = htmlentities($_POST['email']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// the email address is not valid, redirect them back to page1.php
header('Location: page1.php?invalidEmail=true');
}
?>
<form action="login.php" method="post">
<input type="hidden" value="<?=$email?>" />
<input type="password" type="password" />
<input type="submit" value="Continue" />
</form>
login.php
<?php
session_start(); // before any HTML is echoed
$email = $_POST['email'];
$password = $_POST['password'];
// ..do whatever you need todo to validate the email and password
// now you can use something like $_SESSION['loggedin'] = $email which will persist across any pages
// you can check `if (isset($_SESSION['loggedin']))` to check if they're signed in
// if the validation fails, redirect them back to page1.php with another error
?>
This way you're not using $_SESSION between pages and a user must go through from entering their email on page 1, to then adding their password on page 2 and finally validating on the login.php file.
The above will get the basics right for you and hopefully allow you to get where you need to go. There are a few extra things you will want to consider (though without having a firm understanding of the above, the rest won't work..)
Validation between pages - from page1 (collecting email) to page2, I've added a line to collect and validate an email address format. You might want to do some extra validation to see if it exists in your records before asking for a password.
Input sanitization - you'll notice I've used htmlentities around the $_POST['email'] - this is to prevent users from injecting extra code on page2
From page2.php, if you visit it directly you're always redirected to page1.php because it requires an email address to be posted to it. You may want to consider what should happen here.
The flow here also means you can't skip between pages and must go through page1 => page2 => login. This is almost to be expected, but setting $_SESSION['email'] = $_POST['email'] would allow you to persist the entered data between pages.
I have a simple form that I've made to help me learn php. I'm using my local host server to host the php file (form.php). However, when I refresh the page it resubmits the form, I know I can use the post/redirect/get method to negate this problem. Except implementing the header('Location: form.php'); hasn't been working, if you could take a look at the code- and tell me what I'm doing wrong, that would be much appreciated.
Code example i.e without header('Location: form.php');
<?php
if (empty($_POST) === false) {
echo '<pre>', print_r($_POST, true), '</pre>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
This is the result...
Screen Shot without header location
then I add:
header('Location: form.php');
And I get this...
To prevent a form from resubmitting on page refresh, two methods are used:
Method 1: Use AJAX + Redirect
Submit your form using AJAX and then redirect to another page using JQuery.
Method 2: Reload the page
Refresh the page using Javascript.
Your code should be like this:
<?php
if (!empty($_POST)) {
echo '<pre>', print_r($_POST, true), '</pre>';
echo '<script type="text/javascript"> location.reload();</script>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
</body>
</html>
Was trying to follow a tuitorial online on how to submit data using html to mysql database.
I created 2 php files named index.php and process.php. What i wanted to show was what I typed in inside index.php will show on process.php when I clicked on the button "add employee". But nothing showed up.
Here is what's inside index.php
<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title>Add Employee</title>
</head>
<body>
<form method="post" action="">
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>Department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<input type="submit" value="Add Employee">
</form>
</body>
</html>
Here is a Screenshot of the form.
click here for index.php image
Wile here is what's inside the process.php
<?php
print_r($_POST);
This is what shows up
process.php image
Sorry a beginner at this. Hope you guys can help. Thank you!
Maybe you are missing the "action"?
<form method="post" action="/process.php">
You should specify what you need.Somethings like this:
<?php
print_r($_POST['first_name']);
Or use foreach loop to get full array value.
You can go through with fill up action tag so when you submit the form it will go to proccess.php page.
<form method="post" action="process.php">
I am having some difficulty feeding variables from my html form into my php. Would someone mind helping me, I have been at this for hours.
HTML CODE:
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Welcome to Chorelistings - Log in Here</title>
<!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>
<form action="test.php" class="login">
<h1>Chore Listings </h1>
<input type = "email" id="email" name = "email" class = "login-input" placeholder = "Username (Email)">
<input type="password" name="password " id ="password" class="login-input" placeholder="Password">
<input type="submit" value="Login" class="login-submit">
<p class="create-account">Create Account</p>
<p class="login-help">Forgot password?</p>
</form>
</body>
</html>
My VERY SIMPLE php script:
<?php
$email=$_POST['email'];
echo "hi";
?>
Firstly, try adding method="post" to the <form> tag:
<form action="test.php" class="login" method="post">
Secondly, inside your test.php file, try debugging what was actually sent:
<?php
print_r($_POST);
// or
var_dump($_POST);
// or for more info
print_r($_REQUEST);
?>
Thirdly, try to remove whitespaces when defining your input fields in html:
<input type="email" id="email" name="email" class="login-input" placeholder="Username (Email)">
And finally, add <submit> before closing </form> to your form to actually post the data.
Currently, I see you have links (<a href>) but that won't submit the form.
So your final html should look (simplified) like that:
<form method="post" action="test.php">
<input type="text" name="email">
<submit>
</form>
You have no method on your form, so it's defaulting to GET, which doesn't send $_POST values.
Add method="post" to your <form> tag.
A few things here. Your form does not specify a request method. Add a method attribute to your form tag like so:
<form method="post" action="test.php" class="login">
Next, just a side-note, there's no need for spaces when specifying your attributes, eg:
<input type = "email" id="email" name = "email" class = "login-input" placeholder = "Username (Email)" />
You'll be good to go with:
<input type="email" id="email" name="email" class="login-input" placeholder="Username (Email)" />
A few (less common) things to think about -- make sure your .htaccess is not rewriting any post requests in an unwanted way. If you happen to have any file uploads within your form(s), make sure you specify the enctype attribute on your form eg:
enctype="multipart/form-data"
Best of luck!
Script1 is based on CSS and html design(with some texture effect) I tried to setup php form action script to a html/css form however it keeps refreshing to the home page.. according to script if successful then it will refresh to thankyou.php, but it is not success.
However script2, I don't use any css, it is very basic html but it is working fine! but since my whole website design have css effect, I want the form also needs to have some css work.
Could someone please help me with script1? if it not possible then could you please suggest me an action script for below form, currently I don't have any action.php script, I tried few in online, unfortunately without success.
CSS form script:
<div class="row add-bottom-main">
<form name="myform" id="contactForm" action="" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
</div>
Script 1
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" action="" enctype="multipart/form-data" method="post">
<article class="span6">
<textarea id="msg" rows="3" cols="40" name="message" placeholder="Message">Message</textarea>
</article>
<article class="span6">
<input size="100" type="text" name="name" id="name" placeholder="Name">
<input type="text" size="30" id="email" name="email" placeholder="email">
<button type="submit" name="submit" id="submit" class="btn btn-renova-alt add-top-half">Send Message</button>
</article>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#gmail.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
SCRIPT 2
<div class="row add-bottom-main">
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="myform" id="contactForm" action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill again.";
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("niranjan.thampu#capital-placement.com", $subject, $message, $from);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=thankyou.php">';
exit;
}
}
?>
</div>
Your "css form" as posted has no action set on the form and no PHP to process the page in the event that it's submitted. Your two other forms both have PHP that is testing to see if the form has been submitted and process it when it has. The PHP on those two pages is doing the work of processing the form. Without it, they would also not do anything when submit.