CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    16

    REGEX in PHP forms

    I need a regex expression that verifies if a user inputs a valid name (letters and spaces only). However, it doesn't budge.

    Code:
    <?php  
    	function validateInputs(){
    		$status_form = "OK"; $msg="";
    		$fname=$_GET['fname']; $lname=$_GET['lname'];
    		$number=$_GET['number']; $subdivision=$_GET['subdivision'];
    		$street=$_GET['street']; $districtcity=$_GET['districtcity'];
    		$phonenum=$_GET['phonenum']; $cellnum=$_GET['cellnum'];
    		if(!preg_match("/[^A-Za-z\s]/i", $lname)){
    			$status_form="NOTOK";
    			$msg .= "Name format is invalid. Use only alphabets and/or a dash<br>";
    		}
    		if($status_form!="OK"){
    			while (list ($key, $val) = each ($_POST)) $pd .= "$key=".urlencode($val)."&";
    			header ("Location: addentry.php?msg=$msg&$pd"); 
    		} else return true;
    	}
    		
    	if (array_key_exists('add_submit', $_POST)) {
    		if (validateInputs()){
                   
                    }
    		echo "New contact inserted. Details:<br />";
    		echo "<b>FIRST NAME:</b> {$_POST['fname']}<br />";
    		echo "<b>LAST NAME:</b> {$_POST['lname']}<br />";
    		echo "<b>ADDRESS:</b> $addressstring<br />";
    		echo "<b>MOBILE:</b> {$_POST['phonenum']}<br />";
    		echo "<b>PHONE:</b> {$_POST['cellnum']}";
    	} else echo "You can't see this page without submitting the form.";
    ?>
    Last edited by Feebz; October 4th, 2008 at 03:19 AM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: REGEX in PHP forms

    It doesn't work because you haven't told it to do anything if a problem is found. Also you have mixed up $_GET and $_POST throughout your code.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2008
    Posts
    16

    Re: REGEX in PHP forms

    What specific problem? :|

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: REGEX in PHP forms

    First off, you are trying to validate it as boolean.

    PHP Code:
    if (validateInputs()) { 
    But, no where do you return false if there is a problem in the actual function.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Sep 2008
    Posts
    16

    Re: REGEX in PHP forms

    If it's false, it tries to send it back to the form with the error message attached. It also sends back the values that were filled in.

    Code:
    while (list ($key, $val) = each ($_POST)) $pd .= "$key=".urlencode($val)."&";
    			header ("Location: addentry.php?msg=$msg&$pd");
    Do I still have to return false for this?

    And... how do you make the PHP Codebox?
    Last edited by Feebz; October 4th, 2008 at 09:41 PM. Reason: Just aking how you do that

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: REGEX in PHP forms

    If the function is supposed to act boolean, as you have it, you must either return true or false.

    You make the PHP tags the same way you use the code tags. At the bottom of this page on the left, you will find the "Posting Rules" box. In there, you will find a link to all available BBCode possibilities.

    &#91;php&#93;You're PHP code here&#91;/php&#93;
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

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

Featured