Jump to content

Else if.... This OR This OR this...


wright67uk

Recommended Posts

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());
}
?>

Link to comment
Share on other sites

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>");
}
?>

Link to comment
Share on other sites

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>");
}
?>

Link to comment
Share on other sites

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.

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.