SamCec Posted February 23, 2009 Share Posted February 23, 2009 Hi Folks, I'm new to PHP. I have a problem that I have been trying to figure out. Right now, I am totally lost. Here's my code; afterward I will explain the problem. <html> <body> <?php $error = 1; function check_field1($newVariableName) { if(preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\ ]+$/s",$newVariableName)) return TRUE; else echo 'Name is REQUIRED!'; return FALSE; } if ($_POST) { $Fname = $_POST["username"]; $Fmail = $_POST["usermail"]; $FAddress1 = $_POST["Address1"]; $FAddress2 = $_POST["Address2"]; $FCity = $_POST["City"]; $FState = $_POST["State"]; $FZip = $_POST["Zip"]; $FPhone = $_POST["Phone"]; $FSite = $_POST["site"]; $FComment = $_POST["comment"]; $to = "myemail@example.com"; $subject = "Guest Book Entry"; $from = "someonelse@example.com"; $headers = "From: $from"; $message ="\r\n Username: " . $Fname . "\r\n Usermail: " . $Fmail . "\r\n Address1: " . $FAddress1 . "\r\n Address2: " . $FAddress2 . "\r\n City: " . $FCity . "\r\n State: " . $FState . "\r\n Zip: " . $FZip . "\r\n Phone: " . $FPhone . "\r\n Site: " . $FSite . "\r\n Comments: " . $FComment; if(check_field1($Fname)) { $error = 1; } if($error == 0) { // If there are no errors then if (mail($to,$subject,$message,$headers)) { echo '<p>The Guest Book was updated. Thank You.</p>'; echo '<center>'; echo '<a href="Index.shtml">HOME</a>'; echo '</center>'; } } } ?> </body> </html> This routine is being called by an Action tag on a <form>. The USERNAME field is required. The validation is not working. I'm always getting the same result ($error = 1). I did not code the parameters on the "preg_match", someone else coded that for me. All I really want to do is test for alpha characters(upper and lower case). A thought I had--maybe I should, on the form, break down USERNAME into FirstName and Lastname. Reason: the space in between FirstName and LastName in the Username field might be causing me a problem. What do you think? Can you help? Quote Link to comment https://forums.phpfreaks.com/topic/146498-possible-variable-problem/ Share on other sites More sharing options...
Zane Posted February 23, 2009 Share Posted February 23, 2009 I'm always getting the same result ($error = 1). By always do you mean you get it EVERYTIME no matter what, even if the field is blank. because according to your code...error is only set to one when the username passes the test...of validation also...it might help on your preg_match if you took away the dollar sign...there's really no need for it. I'm sure if you change you code to this though, your problem will be solved if(!check_field1($Fname)) { $error = 1; Quote Link to comment https://forums.phpfreaks.com/topic/146498-possible-variable-problem/#findComment-769086 Share on other sites More sharing options...
SamCec Posted February 23, 2009 Author Share Posted February 23, 2009 I'm always getting the same result ($error = 1). By always do you mean you get it EVERYTIME no matter what, even if the field is blank. because according to your code...error is only set to one when the username passes the test...of validation also...it might help on your preg_match if you took away the dollar sign...there's really no need for it. I'm sure if you change you code to this though, your problem will be solved if(!check_field1($Fname)) { $error = 1; zanus: I appreciate the response I did change the "IF" statement, ran the procedure and I am getting the "Name is Required" message no matter if I put a name in or leave it blank. I am attaching a "zip" file that contains the GuestBook.html and the php. If you try this on your machine, don't forget to change the "To$" in the php routine to your email address. Thanks, Sam [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/146498-possible-variable-problem/#findComment-769238 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.