abs0lut Posted October 19, 2008 Share Posted October 19, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/ Share on other sites More sharing options...
philipolson Posted October 19, 2008 Share Posted October 19, 2008 Read about AJAX to make pretty little fancy "instant" thingees like this. Regardless, upon submit do the validation and if there are problems fill the form values with what they wrote, and tell them what they did wrong. Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-668908 Share on other sites More sharing options...
abs0lut Posted October 19, 2008 Author Share Posted October 19, 2008 any other solution please.. Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-668920 Share on other sites More sharing options...
JasonLewis Posted October 19, 2008 Share Posted October 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-668924 Share on other sites More sharing options...
abs0lut Posted October 19, 2008 Author Share Posted October 19, 2008 I mean is there any solution without using ajax? Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-668942 Share on other sites More sharing options...
JasonLewis Posted October 19, 2008 Share Posted October 19, 2008 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; Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-668953 Share on other sites More sharing options...
abs0lut Posted October 19, 2008 Author Share Posted October 19, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-669032 Share on other sites More sharing options...
JasonLewis Posted October 19, 2008 Share Posted October 19, 2008 Set the $email variable back to null, so that when it's echoed in the inputs value attribute, nothing will be echoed. Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-669038 Share on other sites More sharing options...
Bendude14 Posted October 19, 2008 Share Posted October 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-669041 Share on other sites More sharing options...
abs0lut Posted October 19, 2008 Author Share Posted October 19, 2008 thank you Quote Link to comment https://forums.phpfreaks.com/topic/129029-is-this-possible-in-php/#findComment-669085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.