Jump to content

is this possible in php


abs0lut

Recommended Posts

I have input field email with other fields,

I would like to check if the email entered by the user in the field is already in database

if the email is already in database, it will display an error message 'email is already in use' at the top of the fields.

I don't want the user to show back an empty form that has to be filled from scratch.

could you please help me?

Link to comment
Share on other sites

What was wrong with that solution?

 

You need the form to submit to the page that it's on, then do all the form processing above the actual form. If processing fails, show the form and set the values to the correct $_POST value. You can check if the e-mail is already in use by performing a simple MySQL query and checking if the number of rows returned is greater than 0.

 

Plan it out, write some code then post here if you have any trouble with it.

Link to comment
Share on other sites

You don't have to use AJAX. Simply point the form's action to itself, then do the processing above the form. Read what I said carefully.

 

You post the form to itself, above the form you have your PHP that checks to see if the form was submitted. If it is then start error checking and stuff. If you found no errors, process the data and use a redirect the user. If there were errors then output any errors then display the form again. Then in your form value fields you can echo the correct value, so they don't have to resubmit any information.

 

Basic example:

 

//Set up the variables first, to avoid any notices
$name = "";

if(isset($_POST['submit'])){
$name = $_POST['name'];

$errors = array();

if(empty($name)){
	$errors[] = "No name entered.";
}

if(empty($errors)){
	//No errors, process data then redirect...
	exit;
}else{
	//Errors found, display them.
	foreach($errors as $error){
		echo $error . "<br />";
	}
}
}

echo <<<html
<form action="{$_SERVER['REQUEST_URI']}" method="post">
Name: <input type="text" name="name" id="name" value="{$name}" /><br />
<input type="submit" name="submit" id="submit" value="Send Data" />
</form>
html;

Link to comment
Share on other sites

thanks

<?php 
$e = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
if(mysql_num_rows($e) > 0){
echo 'email already in use';
}
?>

how about if the error is 'email already in use', is it possible in php to erase/clear only the email field?

Link to comment
Share on other sites

On your form just where you put info from the POST array back in the the inputs dont put it back into the email if the email exists maybe an example would be clearer

 

<?php

if(isset($_POST['submit']) && !in_array('email', $errors)) { echo $_POST['email']; }

?>

 

something like that should do it. Then just make sure to add "email" into the errors array if it already exists in the DB

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.