/    Sign up×
Community /Pin to ProfileBookmark

My Input Validation Code causing Errors…

Hey

I cannot get my php validation code to run without errors. I get the following
error:

[b]Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in…[/b]

I have ‘validate.php’ which sets the validation functions for each and i call this in ‘require_once’ at the start of my main php page. I have a switch statement that when ‘formchoiceone’ is chosen it echos the form and the validation.

I have tested this code in a standard php file (one that does not output the form and validation in an echo) and it all works fine but having it inside the echo is producing errors. Code is below:

validate.php

[CODE]
<?php
function validateInputOne($one){
//if it’s NOT valid
if(ereg(‘[^A-Za-z]’, $one))
return false;
//if it’s valid
else
return true;
}
function validateInputTwo($two){
//if it’s NOT valid
if(ereg(‘[^0-9]’, $two))
return false;
//if it’s valid
else
return true;
}
?>[/CODE]

main php page

[CODE]
<?php
session_start();
require_once(“validate.php”);

switch ($choice) {

case ‘formchoiceone’:
echo ‘
<div id=form>
<form name=”formone” method=”post” action=””>
if( isset($_POST[‘send’]) && (!validateInputOne($_POST[‘one’]) || !validateInputTwo($_POST[‘two]) ) ):
<div id=”error”>
if(!validateInputOne($_POST[‘one’])):
<p class=”cform”>[Please enter Letters Only]</p>
endif
if(!validateInputTwo($_POST[‘two’])):
<p class=”cform”>[Please enter Numbers Only]</p>
endif
</div>
elseif(isset($_POST[‘send’])):
<div id=”error” class=”valid”>
</div>
endif
<p class=”cform”>Name<input id=”one” name=”one” type=”text”/></p>
<p class=”cform”>Age<input id=”two” name=”two” type=”text”/></p>
<input id=”send” name=”send” class=”submitbutton” type=”submit” value=”Send”></input>
</form>
</div>’;
break;

?>
[/CODE]

Any ideas on this? Had me stuck for days now…

Thanks

Jon

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJan 25.2010 — A couple of things - when you post for help with an error message it is helpful if you identify the line where the error occurs. It's also good if you use [code=php] tags when posting code.

You have a couple of errors on this line:

[code=php]if( isset($_POST['send']) && (!validateInputOne($_POST['one']) || !validateInputTwo($_POST['two]) ) ):[/code]


Firstly, you do not have a closing quote after [B]two[/B], and secondly you then try to output some html without exiting PHP mode. You need to use the PHP closing tag (?>), followed by your html, followed by a PHP opening tag. Or else just echo the html.
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 25.2010 — Beyond what was stated by others, all code specified for a given case or default in any switch has to be standard PHP code subject to the same parsing and syntax checking as if it were outside the switch. The rules don't change simply because it's in a switch.

You embedded PHP code into your echo string which ended up being parsed as text, not PHP. Revise your conditions similar to this:

[code=php]switch ($choice) {

case 'formchoiceone':
echo '<div id=form><form name="formone" method="post" action="">';
if( isset($_POST['send']) && (!validateInputOne($_POST['one']) || !validateInputTwo($_POST['two']) ) ) {
echo '<div id="error">';
}


[/code]
... and so on...

I thought about revising your code to use the proper if/else if/else but this is only partial code with one case - switches traditionally have other cases and a default - and that's up to you to resolve now that you know essentially what went wrong.

-jim
×

Success!

Help @oddball25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...