wright67uk Posted May 26, 2011 Share Posted May 26, 2011 Hello. Ive added a tickbox to my code below, and now I get errors returned, Parse error: syntax error, unexpected T_LOGICAL_OR I know that this is relation to; <?php elseif ($Website == '' or (!preg_match("~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i", $Website))) or $Tickbox == ("!True"); { die("<div class=\"form\">You completed the website field incorrectly, please try again</div>"); } ?> Im just unsure of how to write the aditional tickbox validation into my script. I either want a correctly written website address to be submitted OR for the customer to tick a box saying they dont have one. Please can anybody help me? <form name="add" action="#" method="post"> <fieldset> <legend>Add Your Business</legend> <p><label for="Name">Name</label><input type="text" name="Name" id="Name" maxlength="50" value=" <?php $Name = $_POST['Name']; if (!empty($Name)) echo $Name; ?>"/></p> <p><label for="Phone">Phone</label> <input type="text" name="Phone" id="Phone" maxlength="50" value=" <?php $Phone = $_POST['Phone']; if (!empty($Phone)) echo $Phone; ?>"/></p> <p><label for="Email">Email</label><input type="text" name="Email" id="Email" maxlength="50" value=" <?php $Email = $_POST['Email'];if (!empty($Email)) echo $Email; ?>" /></p> <p><label for="Postcode">Postcode</label><input type="text" name="Postcode" id="Postcode" maxlength="50" value=" <?php $Postcode = $_POST['Postcode']; if (!empty($Postcode)) echo $Postcode; ?>"/></p> <p><label for="Web Address">Web Address</label><input type="text" name="Website" id="Website" maxlength="50" value=" <?php $Website = $_POST['Website']; if (!empty($Website)) echo $Website; ?>"/> <label for="Web Tick">I dont have a website</label><input type="checkbox" name="Tickbox" id="Tickbox" value="True" /></p> <select name="drop_1" id="drop_1"> <option value="" selected="selected" disabled="disabled">Select a Category</option> <?php getTierOne(); ?> </select> <span id="wait_1" style="display: none;"> <img alt="Please Wait" src="ajax-loader.gif"/> </span> <span id="result_1" style="display: none;"></span> <br /> </fieldset> </form> <div id="results"> <?php function checkPostcode (&$toCheck) { $alpha1 = "[abcdefghijklmnoprstuwyz]"; $alpha2 = "[abcdefghklmnopqrstuvwxy]"; $alpha3 = "[abcdefghjkstuw]"; $alpha4 = "[abehmnprvwxy]"; $alpha5 = "[abdefghjlnpqrstuwxyz]"; $pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$'; $pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$'; $pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$'; $pcexp[3] = '^(gir)(0aa)$'; $pcexp[4] = '^(bfpo)([0-9]{1,4})$'; $pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$'; $Postcode = strtolower($toCheck); $Postcode = str_replace (' ', '', $Postcode); $valid = false; foreach ($pcexp as $regexp) { if (ereg($regexp,$Postcode, $matches)) { $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]); $toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck); $valid = true; break; } } if ($valid){return true;} else {return false;}; } if(isset($_POST['submit'])) { $drop = mysql_real_escape_string($_POST['drop_1']); $tier_two = mysql_real_escape_string($_POST['Subtype']); $Name = mysql_real_escape_string($_POST["Name"]); $Phone = mysql_real_escape_string($_POST["Phone"]); $Email = mysql_real_escape_string($_POST["Email"]); $Postcode = mysql_real_escape_string($_POST["Postcode"]); $Website = mysql_real_escape_string($_POST["Website"]); $Tickbox = mysql_real_escape_string($_POST["Tickbox"]); if($Name == '') { die ("<div class=\"form\">You did not complete the name field, please try again</div>"); } elseif ($Phone == '' or (preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $Phone))) { die("<div class=\"form\"> You completed the telephone field incorrectly, please try again</div>"); } elseif ($Email == '' or (!filter_var($Email, FILTER_VALIDATE_EMAIL))) { die("<div class=\"form\"> You completed the Email field incorrectly, please try again</div>"); } elseif ($Postcode == '' or (!checkPostcode($Postcode))) { die("<div class=\"form\"> You did not complete the Postcode field correctly, please try again</div>"); } elseif ($Website == '' or (!preg_match("~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i", $Website))) or $Tickbox == ("!True"); { die("<div class=\"form\">You completed the website field incorrectly, please try again</div>"); } else { echo("<div id=\"formtwo\">Thankyou for submiting your details, you will be added to our directory shortly</div>"); } $query = ("INSERT INTO business (`id`, `Name`, `Type`, `Subtype`, `Phone`, `Email`, `Postcode`, `WebAddress`, `confirmed`) VALUES ('NULL', '$Name', '$drop', '$tier_two' , '$Phone', '$Email', '$Postcode', '$Website', 'Yes')"); mysql_query($query) or die ( "<br>Query: $query<br>Error: " .mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237503-else-if-this-or-this-or-this/ Share on other sites More sharing options...
Psycho Posted May 26, 2011 Share Posted May 26, 2011 elseif (preg_match("~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i", $Website) OR ($Website=='' AND $Tickbox=="True")); Quote Link to comment https://forums.phpfreaks.com/topic/237503-else-if-this-or-this-or-this/#findComment-1220425 Share on other sites More sharing options...
btherl Posted May 26, 2011 Share Posted May 26, 2011 There's a few problems there: <?php elseif ($Website == '' or (!preg_match("~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i", $Website))) or $Tickbox == ("!True"); { die("<div class=\"form\">You completed the website field incorrectly, please try again</div>"); } ?> First is you should be using "||" instead of "or", unless you have a good reason to use "or" and know why you're using it. Second is your brackets are not matching, and that's what's causing the error. Third is you have a ";" at the end of the first line - this should be removed. Fourth is you should use single quotes around the regex in preg_replace(), otherwise you'll match more than you expect. The reason is the "\." will get rewritten to just ".", which matches any charater at all, not just a dot. <?php elseif ($Website == '' || (!preg_match('~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i', $Website)) || $Tickbox == "!True"); { die("<div class=\"form\">You completed the website field incorrectly, please try again</div>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237503-else-if-this-or-this-or-this/#findComment-1220426 Share on other sites More sharing options...
btherl Posted May 26, 2011 Share Posted May 26, 2011 Let me have another go at that, having read mjdamato's post. He's fixed the logic as well as the syntax: <?php elseif (!preg_match('~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i', $Website) || ($Website == '' && $Tickbox == "True")) { die("<div class=\"form\">You completed the website field incorrectly, please try again</div>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237503-else-if-this-or-this-or-this/#findComment-1220427 Share on other sites More sharing options...
Psycho Posted May 26, 2011 Share Posted May 26, 2011 First is you should be using "||" instead of "or", unless you have a good reason to use "or" and know why you're using it. There is no difference between OR or ||, although I tend to use || most of the time myself. Same with AND and &&. http://php.net/manual/en/language.operators.logical.php Quote Link to comment https://forums.phpfreaks.com/topic/237503-else-if-this-or-this-or-this/#findComment-1220439 Share on other sites More sharing options...
Psycho Posted May 26, 2011 Share Posted May 26, 2011 After looking over the original request I see I created the logic to look for a VALID input. btherl's wasn't correct either. In that one an empty $Website would make the first condition true (since it does not match the expression), therefore the parser will not check the other condition because of the OR. You could use the first one I give and wrap it all inside parens with a "!" before it: elseif ( !(preg_match('~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i', $Website) || ($Website=='' && $Tickbox=="True")) ) { //Invalid entry } basical the condition is ( [it is a valid address] OR ([address is empty] AND [Tickbox = "True"]) ) which is the validation for correct input, but by putting a "!" before the entire thing it will return TRUE for a false condition. Quote Link to comment https://forums.phpfreaks.com/topic/237503-else-if-this-or-this-or-this/#findComment-1220447 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.