PHP Script won't recognise email [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm getting pretty far with my registration script now. This has been such an amazing learning curve for me. As of now I've just finished up some bugs with my user recognition and registration email send outs. I'm having some issues with recovering a password though. At the moment I am just trying to get an email recognised, here is my HTML and PHP:
HTML
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php include "header.php" ?>
<div id="wrapper">
<form method="post" action="">
<h2>Recover Password</h2>
<div id="underline"></div>
<ul>
<li>
<label for="usn">Email : </label>
<input type="text" maxlength="30" required autofocus name="reset" />
</li>
<li class="buttons">
<input type="submit" name="reset" value="Reset Pass" class="xbutton" />
</li>
</ul>
</form>
</div>
</body>
</html>
<?php include "prec.php" ?>
PHP
<?php
if($_POST)
{
if(empty($_POST['reset']))
{
echo 'Please enter all fields';
}
else
{
$email = $_POST['reset'];
$password = $_POST['password'];
$db_name =
$db_user =
$db_pass =
$conn = new PDO('mysql:host=localhost;dbname=tweezy_php', 'tweezy_php', 'XXXXXX',
array( PDO::ATTR_PERSISTENT => true ));
$stmt = $conn->prepare("SELECT email FROM users WHERE email = ? ");
$stmt->execute(array($email));
if($stmt->rowCount() === 1 )
{
echo "That email exists";
}
else
{
echo "Sorry, that email doesn't exsist.";
}
}
}
?>
For some reason, no matter what I enter the supplied email is never recognised. Looking through my code I don't quite see why though. I've tried a couple of variations, but it just seems to give me the same result. I'm thinking it has something to do with my SQL query, but I can't seem to quite put my finger on it.
Any insights would be wonderful!

Your input field is:
<input type="text" maxlength="30" required autofocus name="reset" />
Change this to:
<input type="text" maxlength="30" required autofocus name="email" />
And, in your PHP, you'd do something like this to retrieve the email from the user:
$email = $_POST['email'];
A simple example to demonstrate why it's failing:
test.php
<?php
if(isset($_POST['fieldname'])){
echo $_POST['fieldname']; //outputs "Submit" instead of the user input
}
?>
<form action="" method="post">
<input type="text" name="fieldname"/>
<input type="submit" name="fieldname">
</form>
Both the input field and submit button has the same name. So when you input something and click on submit, you will find that instead of echoing the user input, it echoes the text Submit. This is because the first input is being overridden by the name attribute in your Submit button. This can be resolved by changing your email input's name attribute to something different, like email so it makes more sense.
Hope this helps!

You have two fields in the form that contain name="reset". One is the email field, the other is the submit button.
This will confuse things -- only one of those values will get into your $_POST array, and it looks like it's the wrong one.
You should tidy up the form and ensure that your field name attributes do not clash.
In addition, I note that the email field has a label near it that has for="usn", but there isn't a usn field anywhere to be seen. That won't cause any problems, but is badly incorrect (it looks like a copy+paste bug) -- you probably fix that too.

Change $email to This:
$email = $_POST['username'];
And
if(!isset($_POST['reset']))

Try the following:
$conn = new PDO('mysql:host=localhost;dbname=tweezy_php', 'tweezy_php', 'XXXXXX',
array( PDO::ATTR_PERSISTENT => true ));
$stmt = $conn->prepare("SELECT email FROM users WHERE email = ? ");
$stmt->bindValue(1, $email);
$stmt->execute();
$result = $stmt->fetchAll();
if(count($result) === 1 ){
echo "That email exists";
}else{
echo "Sorry, that email doesn't exsist.";
}
Also, you should change the name of the email's input. It is conflicting with another input and if two inputs have the same name without them being an array, the last input's value will be presented to the server.
That should resolve the issue. Hope this helps.

Your issue is because you're checking the Reset button for a value and not the email field. Here's your email field:
<input type="text" maxlength="30" required autofocus name="username" />
So you need to change this:
if(empty($_POST['reset']))
AND
$email = $_POST['reset'];
To check $_POST['username'] instead.

Both your reset and email input fields are named reset (name="reset"). This will result in the first field (which should be named email) being overriden by your actual reset input field
Change your email input to
<input type="text" maxlength="30" required autofocus name="email" />
And your $email to
$email = $_POST['email'];

Related

Save User Info Form PHP HTM [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to do a form that when the user types the info and then submits it saves it on a text file. But when I open the text file, it only displays the IP adress.... I can't figure out what I'm doing wrong with my code:
I already tried changing §_SERVER to $_POST but it doens't work
Thanks :)
HTML:
<form action="login.php" id="login.php" method="get">
<div class="imgcontainer">
<img src="zimbra.png" alt="Avatar" class="avatar">
</div>
<div class="bodypage">
<div class="font">
<div class="container">
<label>EEB2 Zimbra Email</label>
<input type="text" placeholder="Enter Email" name="email" id="email" required>
<label>Password</label>
<input type="password" placeholder="Enter Password" name="psw" id="psw" required>
<label>New Password</label>
<input type="password" placeholder="Enter New Password" name="newpsw" required id="newpassword">
<label>Confirm New Password</label>
<input type="password" placeholder="Enter New Password" name="newpsw" required id="confirmnewpassword">
</div>
<button type="submit">Change Password</button>
</div>
</form>
PHP:
<?php
$handle = fopen("Passwords.txt", "a");
$ip = $_SERVER['REMOTE_ADDR'];
$email = $_SERVER['email'];
$psw = $_SERVER['psw'];
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, PHP_EOL);
}
fwrite($handle, "IP=$ip");
fwrite($handle, "PASS=$psw");
fwrite($handle, "EMAIL=$email");
fclose($handle);
header ('Location: http://www.google.be/');
exit;
?>
EDIT: I'm not trying to get the new password, just the Email and the password.
Your method is $_GET so it would be $_GET['email']
There's no such thing as $_SERVER['email'], $_SERVER['psw'], etc. This is documented here.
Assuming that they have "name" attributes in the HTML form (which I can't, for some reason, see at Dropbox), you should find these values at $_POST['psw'], $_POST['email'], etc.
And I would add that you should use filter_input or some other mechanism to test these values before you put them onto your filesystem.
First if you are trying to save credentials(like passwords) you should not use method "GET" as in your code. Because when the form is submitted, as you have used GET method all the credentials will be shown on the URL. So it will be a security issue. Most developers prefer POST method over GET for form data handling.
As it says on W3schools "$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations."
Using POST method
Try Changing to this.(Use "$_POST[]" method just to capture the data you're trying to POST from the web Form. So this will work for you.
$ip = $_POST['REMOTE_ADDR'];
$email = $_POST['email'];
In order this to work for you you should change
USING GET method
But if you still would like to stick with the GET method you can simply change only these to $_GET['']
$ip = $_GET['REMOTE_ADDR'];
$email = $_GET['email'];
Hope that would help you! :D Good Luck

PHP: Refresh page on invalid form submit

How can I refresh a page with a form on submission pending the outcome of the submitted data and display a result.
e.g I have a page with a form:
<form action="" method="post">
<input type="name" value="" name="name" placeholder="Your Name" />
<input type="button" name="submit" value="submit form "/>
</form>
The engine that handles the form is external, but required in the page:
require_once 'form_engine.php';
form_engine.php checks the input,
$success = "true";
$errorMessage = " ";
$name = $_POST['name'];
if ( $name == '') {
$errorMessage = 'Please enter your name';
$success = false;
}
else (if $success = true) {
// do something with the data
}
The form page contains the result:
<form action="" method="post">
<input type="name" value="" name="name" placeholder="Your Name" />
<input type="button" name="submit" value="submit form "/>
</form>
<p><?php echo $errorMessage; ?></p>
Will the error message get displayed after the form is submitted incorrectly? Or do I have to use a session to store it?
You need something like this:
if (!isset($_POST['name']))
instead of
if ( $name == 'name')
UPDATE
Try this, it should give you the idea:
<?php
$errorMessage = false;
if (isset($_POST['submit'])) {
if (!isset($_POST['name']) || $_POST['name']=='') {
$errorMessage = 'Please enter your name';
}
else {
// do something with the data
echo "Success!!";
}
}
?>
<form method="post">
<input type="name" value="" name="name" placeholder="Your Name" />
<input type="submit" name="submit" />
</form>
<p><?php if ($errorMessage) echo $errorMessage; ?></p>
Note: leaving out the action attribute will just submit the form to the current page
Note 2: The PHP here could very well be stored in another page. Using require() is the same as putting the code directly into the page.
You can use redirect on php side:
header('Location: www.mysite.com/index.php');
You seem to be a little confused in terms of the exact process that occurs in terms of rendering a page, as do some of those commenting. You do not need to use sessions to solve this problem. There is no need to store anything server-side between page requests because the user's browser with retain everything that you need, at least for this situation. My guess is the others took you mentioning an "external engine" and thought that the form would be submitting away to a different site/page.
form loops
Below is a diagram showing a typical form request loop:
You do not have to do this, as coding is as much about personal preference to anything else, but typically people will design their form to submit back to the same URI that generated it — as you seem to be doing in your example, by leaving the action attribute blank. By doing this, as long as you embed everything you wish to pass back to the server side within the form — each time the user submits — that information will be resent and be available in PHP.
Obviously you need to be wary of what information might constitute as sensitive, as this data should only ever be written into markup if your requests are protected by HTTPS/SSL. You should also filter/escape any user input to prevent markup injection into your site. You can prevent many problems by using htmlentities, however this can cause issues depending on the values you are trying to capture from the user. Because you are using double quoted HTML attributes (the right way to do them ;) I have not set the ENT_QUOTES option.
back to the point
So in the above loop the user will be shown the form for the first time, and after any subsequent submit, which means that each time your PHP notices that there is an error you can just add your message into the page flow. The trick with this kind of system is what exactly do you do once the form is fully complete. To get out of the loop most people will use a header location call:
<?php
require_once 'form_engine.php';
$name = !empty($_POST['name']) ? trim($_POST['name']) : '';
$name = htmlentities($name);
if ( $success ) {
header('location: next-step.php');
exit;
}
?>
<form action="" method="post">
<input type="name" value="<?php echo $name; ?>" name="name" placeholder="Your Name" />
<input type="button" name="submit" value="submit form "/>
</form>
<?php
if ( $errorMessage ) {
echo "<p>$errorMessage</p>";
}
?>
form engine repairs
You should also rectify your form_engine.php as per my comments above and Shekhar Joshi's answer, although I would keep the header code outside of your engine logic, and leave that decision to the code that requires in the engine — as the above does.
may be, you are looking for this! the header() method.
$success = true;
$errorMessage = " ";
$name = $_POST['name'];
if(isset($_POST['name'])) {
if ( $_POST['name'] == '') {
$errorMessage = 'Please enter your name';
$success = false;
header('Location: www.something.com/some.php');
}
else if ($success == true) {
// do something with the data
}
}

Not set when I first open page

I'm still new and trying to learn php. I have a form and everytime I run it I get an error displaying that the variable were not set when they should be. I'm definately missing something. Kindly explain what why is the variable $_POST['login_button'] not set the first time i run the page?
Code can be found below:
<?php
require 'connect.inc.php';
if (isset($_POST['login_button']) && isset($_POST['username']) && isset($_POST['password'])){
$login_button = $_POST['login_button'];
$username = $_POST['username'] ;
$password = $_POST['password'];
$password_hash = md5($_POST['password']);
if(!empty($username)&&!empty($password)){
$sql = "SELECT `id` FROM `golden_acres_username` WHERE `uname`='$username' AND '".$password_hash."'";
if($sql_run = mysql_query($sql)){
$query_num_rows = mysql_num_rows($sql_run);
}
if($query_num_rows==0){
echo'User name and password are incorrect';
}
else if($query_num_rows==1)
{
echo 'Username and password are correct';
}
}
else
{
echo 'Please fill in user name and password';
}
}
else
{
echo'Fields are not set';
}
?>
<form class="home_logon_area" action="test.php" method="POST">
Username:
<input type="text" name="username" />
Password:
<input type="password" type="password" name="password"/>
<input type="submit" name="login_button">
</form>
Thanks in advance,
Joseph
$_POST contains the result of submitting a form. If no form has been submitted yet, it will not contain anything.
Your script is working just fine; remove echo 'Fields are not set';, or use that line for code that should only run when the form hasn't been submitted yet.
The $_POST variable is set by the server to capture the data content sent by the browser as part of the form POST action. When the page is initially loaded, the browser has only executed/requested a GET call for the content of the page without sending the POST request.
Hope that helps!
This is simple to understand ;-)
First time the phpscript is executed to get the Form
So there will be no information at all (the visitor is new and have not seen the form before)
Then the User fills the form and press Submit button
The form is linked to the same side so the same phpscript gets executed again
Now you have the Formular values transmitted and you can acess them over $_POST
For more information look at php.net
Remove last else from your code and update the form with this one
<form class="home_logon_area" action="test.php" method="POST">
Username:
<input type="text" name="username" required />
Password:
<input type="password" type="password" name="password" required/>
<input type="submit" name="login_button">
</form>

Wordpress contact form

I've a very basic contact form on my Wordpress site (hard coded) and I can't get things to work. It works locally fine through XAMPP and I'm sure it's something I'm just missing but any help would be greatly appreciated. Thanks in advance!. I'm also using a template I created
<?php /* Template Name: contact */?>
<?php get_header(); ?>
<?php
//vars declared to store form input
$name=$email=$comment=$phone="";
//Error vars - to relay error message to the form
$nameError=$emailError=$commentError="";
$error_message="";
$sentMessage="";
$status=0; //Will monitor if all fields have no errors and increment if so.
function sanitise_var($string){
htmlentities($string);
strip_tags($string);
return stripslashes($string);
}
if(isset($_POST['submitted'])){
if($_POST['name']==""){
$nameError="Please enter a name";
$error_message="Oops, error in the form. Please check";
}
else {
$name=$_POST['name'];
++$status;
}
if($_POST['email'] == "" || !preg_match("/^[a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", $_POST['email'])){
$error_message="Oops, error in the form. Please check";
$emailError="Please enter a valid email";
}
else{
$email=$_POST['email'];
++$status;
}
if(!$_POST['phone']=="") $phone=$_POST['phone'];
if($_POST['comment']==""){
$error_message="Oops, error in the form. Please check";
$commentError="Please enter a message";
}
else{
$comment=$_POST['comment'];
++$status;
}
}//submitted if statement
if($status==3 && $_POST['submitted']){
$sentMessage="From: $name, email: $email, Phone: $phone, Comment: $comment";
wp_mail("mathornley#gmail.com", "From Android Scoop contact form", $sentMessage);
echo "Thanks, your email was sent successfully!";
}
else{
echo<<<SOQ
<div class="entry-content">
<h1 class="entry-title">Contact</h1>
<p class="contact">
If you have a query drop us a line using the form below. We're always happy to hear from people with ideas for posts and content they'd like to feature or maybe write about. Or maybe you just have some feedback you'd like to share with us. Why not just swing by and say hello.
</p>
<p class="requiring">* Denotes required fields</p>
<div class="form_left">
<form action="/contact/" method="POST">
<p><label>Name:</label><input type="text" name="name" value="$name"/></p>
<p class="error">$nameError</p>
<p><label>Email</label><input type="text" name="email" value="$email"/></p>
<p class="error">$emailError</p>
<p><label>Phone:</label><input type="text" name="phone" value="$phone"/></p>
<input type="hidden" name="submitted" value="yes"/>
<input type="submit" value="Send your message"/>
</div>
<div class="form_right">
<p><label>Message:</label><br/><textarea name="comment" rows="20" cols="20">$comment</textarea></p>
<p class="error">$commentError</p>
</form>
</div>
</div>
SOQ;
}
?>
<?php get_footer();?>
Try to use blank value for action like:
<form action="" method="POST">
If that doesn't work try renaming name parameter of the first input field to something else like:
<input type="text" name="myname" value="$name"/>
Am not aware about the wordpress but as the general PHP rules go, and according to what you replied me in the comment, the error lies here
<form action="/contact/" method="POST">
----^----
Why won't you use some out-of-the-box contact form for WordPress? For example, Contact Form 7 is pretty good.
Contact Form 7 is an open source software and can manage multiple contact forms, plus you can customize the form and the mail contents flexibly with simple markup. The form supports Ajax-powered submitting, CAPTCHA, Akismet spam filtering and so on.
Installation
Upload the entire contact-form-7 folder to the /wp-content/plugins/
directory.
Activate the plugin through the ‘Plugins’ menu in
WordPress.
You will find ‘Contact’ menu in your WordPress admin
panel.
Create a contact form, copy it's url, paste it everywhere you want.
Contact form will appear exactly in it's location.
If you want an example, you can check out this site to see it's look & feel.

Help With Notify Me Form

I'm trying to make a simple form like this http://birdboxx.com/, where the user can enter their email address to be notified when our site has launched.
I've looked at the HTML their using and it all looks pretty simple, I'm just not sure on how to code the PHP part. Could someone maybe help me out?
Thanks in advance.
The form in the given example (the relevant parts):
<form name="form-email-submit" id="form-email-submit" action="add_email.php" method="POST">
<input type="text" id="input-email" name="input-email" value="Enter your email here"/>
<input type="submit" title="Notify Me" value="Notify Me">
</form>
In your PHP script:
//In the add_email.php
$notificationEmail = $_POST['input-email']; // from the name="input-email" in the form
So now you have the email submitted, you can do whatever you want with it. You can write it to a file, or save it to a database, or whatever.
Basic solution for the email saving part.
The HTML:
<form method="post" action="">
<input type="email" name="email" placeholder="Enter your email here" /> <input type="submit" name="send" value="Notify me" />
</form>
The PHP with MySQL for saving:
$dbhost = "your host";
$dbuser = "your username";
$dbpass = "your password";
$dbname = "database name";
$c = #mysql_pconnect($dbhost,$dbuser,$dbpass) or die();
#mysql_select_db($dbname,$c) or die();
//It isn't secure in this state: validation needed as it is an email?
if(isset($_POST['send'])){
$email = mysql_real_escape_string($_POST['email']);
mysql_query('INSERT INTO email (id,email) VALUES (NULL,'.$email.');');
}
For sending emails I recommend phpmailer or any other solution: http://phpmailer.worxware.com/

Categories