PDA

View Full Version : Is it possible to have a form, check for duplicates but user has a choice?


nanny
06-13-2007, 12:43 AM
Sorry, what I am trying to say is:
1. Register form that has questions for person's "For Sale" listing.
2. User has a choice to place website address or not but must have email.
3. System checks for duplicate website address if placed and also checks for duplicate of email.

Where I am having the problem firstly is how to use php for if the website address is given to then check for duplicate, rather I have no problem with checking a form field left empty. But in this case it can be left empty.

Learning the next level of form input........
Thanks.

davidj
06-13-2007, 07:24 AM
when the user clicks the submit button catch the address and email in a $_POST. Then perform a SELECT statement to look for the email address they posted. If your SELECT comes back with a result you have found a duplicate.


if (! DUPLICATE){

//run update or insert statement
}else{

/// do some stuff like inform user of the duplicate
}

nanny
06-14-2007, 12:25 AM
Hi thanks David, I understand I need a select statement:
$query = "SELECT email FROM list WHERE email='" . $email . "'";
but that is as far as it goes.
For some reason as well whenever I use else or elseif my php debugger says it is an error.
Thanks again....

nanny
06-14-2007, 01:42 AM
I got it almost right:
$loginemail = $_POST['contactemail'];
$_POST['contactemail'] = trim($_POST['contactemail']);
if (empty($_POST['contactemail'])) {
$error['contactemail'] = 'Please enter your email address, if you need one go to hotmail.com';
}
$pattern = '/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|a s|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn |bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|c l|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk| dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|f o|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs |gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|in t|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr| kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md |mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum |mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|n p|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr |pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg| sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf |tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|u m|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za| zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i';
if (!preg_match($pattern, trim($_POST['contactemail']))) {
$error['contactemail'] = 'Please enter a valid email address';
}

$LoginRS__query = "SELECT contactemail FROM class_adfree WHERE contactemail='" . $loginemail . "'";
mysql_select_db($database_config, $config);
$LoginRS=mysql_query($LoginRS__query, $config) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
//if there is a row in the database, the username was found - can not add the requested username
if($loginFoundUser){
$error['contactemail'] = "$loginemail is already in use. Please choose a different email.";
}
$loginweb = $_POST['contactwebsite'];
$address = '^(http://www.)+([[:alnum:]]{1,100})+(\.)+([a-z]{2,6})$';
if (!preg_match($address, trim($_POST['contactwebsite']))) {
$error['contactwebsite'] = 'Please enter a valid web address';
}

$LoginRS__query = "SELECT contactwebsite FROM class_adfree WHERE contactwebsite='" . $loginweb . "'";
mysql_select_db($database_config, $config);
$LoginRS=mysql_query($LoginRS__query, $config) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
//if there is a row in the database, the username was found - can not add the requested username
if($loginFoundUser){
$error['contactwebsite'] = "$loginweb is already in use. Please.";
}

It works great except for the $address pattern won't let me put in a web address without a www. in it and some addresses don't have this.
Any suggestions???

davidj
06-14-2007, 07:06 AM
in your regular expression...


'^(http://www.)+([[:alnum:]]{1,100})+(\.)+([a-z]{2,6})$';


remove this..


(http://www.)+


This is dreamweaver code.

Stop using it!

nanny
06-14-2007, 08:37 AM
Thanks, well it is a very modified version................had to play with it.

davidj
06-14-2007, 11:15 AM
sorry i was wrong!!! I know its a rare thing


in your regular expression where you have ....


'^(http://www.)+([[:alnum:]]{1,100})+(\.)+([a-z]{2,6})$';


do it like this...


'^(http://)+([[:alnum:]]{1,100})+(\.)+([a-z]{2,6})$';

nanny
06-15-2007, 12:44 AM
Thanks David,
Now I am having another problem, if a person adds the web address and if it is correct it still shows the error: 'Please enter a valid web address'
How do I get stop this showing if the user has entered a correct web address?

I have slightly changed the code to be conditional if the user doesn't have a web address, they don't have to enter one.

$loginweb = $_POST['contactwebsite'];
if (!empty($_POST['contactwebsite'])) {
$address = '^(http://)+([[:alnum:]]{1,100})+(\.)+([a-z]{2,6})$';
if (!preg_match($address, trim($_POST['contactwebsite']))) {
$error['contactwebsite'] = 'Please enter your website address';
}
}


OK so to add another part to this that if they have entered an address and the regular expression is correct, how do I stop the $error showing.
Would I put

$loginweb = $_POST['contactwebsite'];
if (!empty($_POST['contactwebsite'])) &&
$address = '^(http://)+([[:alnum:]]{1,100})+(\.)+([a-z]{2,6})$';
if (!preg_match($address, trim($_POST['contactwebsite']))) {
$error['contactwebsite'] = 'Please enter your website address correctly';
}else{
if (!empty($_POST['contactwebsite'])) {
$address = '^(http://)+([[:alnum:]]{1,100})+(\.)+([a-z]{2,6})$';
if (preg_match($address, trim($_POST['contactwebsite']))) {
}


I don't seem to be able to finish it properly. Is there a quicker way to add && ||
I am confusing myself.

nanny
06-15-2007, 09:15 AM
actually I have worked it out but I had to change the regular expression, it seemed to be the problem.
I changed it to:
/http:\/\/(.*)\.(.*)/i
very basic so I now have the problem of only allowing a user place one url rather than at the moment it allows you to keep placing them up until the 80 characters in mysql.

Can you please tell me options on how to fix this? Or at least to get started.
I understand that the ^ and the $ allow only one within the string but for some reason that expression wouldn't work with any web address I placed.