Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/146498-possible-variable-problem/
Share on other sites

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;
    

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]

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.