A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: My Input Validation Code causing Errors...

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    27

    My Input Validation Code causing Errors...

    Hey

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

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in...

    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;
    	}
    ?>
    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;
    
    
    
    ?>
    Any ideas on this? Had me stuck for days now...

    Thanks

    Jon

  2. #2
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    echo takes a string literal as its only argument. You can't execute logic as part of the string, you have to join the whatever the logic produces as a new string using the . (period) join string operator. While you can put logical statements in parentheses joined by the period operator, and put the whole resulting string into an echo, in your case and in most cases it makes a lot more sense to start with a string, build on it as necessary, and then echo it when it's done. e.g.

    PHP Code:
    $output 'hello ';
    if (
    $a==0) {
      
    $output .= 'world';
    } else {
      
    $output .= 'people';
    }
    echo 
    $output

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center