Coding Problem (If Statement)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SamCec
Forum Newbie
Posts: 17
Joined: Mon Aug 16, 2010 11:43 am

Coding Problem (If Statement)

Post by SamCec »

In the preview, the Font Colours did not show. If they don't show on your browser, look for "color=#FF0000" and "color=#0000FF".

Code: Select all

<html>
<body>
<?php
function check_field1($newVariableName)
   {
     if(preg_match("/[^A-Z]+$/s",$newVariableName))
{
       return TRUE;
}
     else
        return FALSE;
   }
if ($_POST)
[color=#FF0000]{ $Franlist = $_POST["ranlist"];
  $Fcopy = $_POST["copy"];
  if ($Franlist <> $Fcopy)
	echo "You did not COPY the number correctly. Please try again";
}
  else[/color]
{
    $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 = "samc@xxxxxx.com";
    $subject = "Guest Book Entry";
    $from = $_POST["usermail"];
    $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;         
}    
[color=#0000FF]if(!check_field1($Fname))
    {
echo "NAME is REQUIRED";[/color]
[color=#FF0000]echo '<p />';
echo '<center>';
     echo '<a href="TestGuestBook.html">Return to Guest Book</a>';
	 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ';[/color]
     echo '<a href="Index.shtml">HOME</a>';
     echo '</center>';
    }
else
    {
       // 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>
Background: This PHP code is associated with an HTML form. I was getting a lot of span (e-mail) so I wrote a "random Number" routine using javascript and placed it in my HTML document.In the document, I have two text boxes--one in which the random number is placed (name = "ranlist") and the other is where the user retypes the number (name="copy").

I added the code you see highlighted in "red" above.

My problem: The routine is always failing the Name check (highlighted in BLUE) On the form, even though I enter a NAME, I am getting the message "Name is Required".

I can't find the error. Can You Help????

Thanks, Sam
Last edited by SamCec on Thu Aug 26, 2010 12:08 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Coding Problem (If Statement)

Post by McInfo »

Please show us your form page.
SamCec
Forum Newbie
Posts: 17
Joined: Mon Aug 16, 2010 11:43 am

Re: Coding Problem (If Statement)

Post by SamCec »

McInfo wrote:Please show us your form page.
I have attached a "ZIP" file of the form.

Thank you,
Sam
Attachments
guestbook.zip
(1.72 KiB) Downloaded 48 times
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Coding Problem (If Statement)

Post by McInfo »

Might you be missing some curly braces? Make sure they are all matched with the correct ifs and elses.
SamCec
Forum Newbie
Posts: 17
Joined: Mon Aug 16, 2010 11:43 am

Re: Coding Problem (If Statement)

Post by SamCec »

McInfo wrote:Might you be missing some curly braces? Make sure they are all matched with the correct ifs and elses.
That's the first thing I checked.

Without the added code in the PHP routine (the code comparing to two numbers--$Franlist and $Fcopy) and the code toward the bottom, the "echo <a..TestGuestBook...>,the routine worked.

DO I have the curly braces correct where I am comparing to two numbers?

Sam
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Coding Problem (If Statement)

Post by McInfo »

Here is your code again. I did not modify the logic, but I cleaned up the formatting so it might be easier to see what the problem is. To me, it doesn't look like the braces are positioned logically.

Code: Select all

<html>
    <body>
        <?php
        function check_field1 ($newVariableName) {
            if (preg_match('/[^A-Z]+$/s', $newVariableName)) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
        if ($_POST) {
            $Franlist  = $_POST['ranlist'];
            $Fcopy     = $_POST['copy'];
            if ($Franlist <> $Fcopy) {
                echo 'You did not COPY the number correctly. Please try again';
            }
        } else {
            $Fname     = $_POST['username']; // Else if NOT $_POST use $_POST?
            $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        = 'samc@xxxxxx.com';
            $subject   = 'Guest Book Entry';
            $from      = $_POST['usermail'];
            $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)) { // $Fname is not set because it is trapped in the else block above
            echo 'NAME is REQUIRED';
            echo '<p />';
            echo '<center>';
            echo '<a href="TestGuestBook.html">Return to Guest Book</a>';
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ';
            echo '<a href="Index.shtml">HOME</a>';
            echo '</center>';
        } else {
            if (true || mail($to, $subject, $message, $headers)) { // Added "true || " temporarily to disable mail()
                echo '<p>The Guest Book was updated. Thank You.</p>';
                echo '<center>';
                echo '<a href="Index.shtml">HOME</a>';
                echo '</center>';
            }
        }
        ?>
    </body>
</html>
TestGuestBook.html has formatting issues too. Use an editor like NetBeans or paste the HTML into W3C's Markup Validation Service to see all the problems.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Coding Problem (If Statement)

Post by McInfo »

Another thing to consider is that your anti-spam procedure can be circumvented by someone submitting any two identical values for the "ranlist" and "copy" inputs, which are not necessarily related to the random number your JavaScript generates.
SamCec
Forum Newbie
Posts: 17
Joined: Mon Aug 16, 2010 11:43 am

Re: Coding Problem (If Statement)

Post by SamCec »

I'm just learning PHP so there is a major chance I am wrong but the way I am reading this code :

Code: Select all

if ($_POST) {
            $Franlist  = $_POST['ranlist'];
            $Fcopy     = $_POST['copy'];
            if ($Franlist <> $Fcopy) {
                echo 'You did not COPY the number correctly. Please try again';
            }
        } else {
            $Fname     = $_POST['username']; // Else if NOT $_POST use $_POST?
            $Fmail     = $_POST['usermail'];
            $FAddress1 = $_POST['Address1'];
            $FAddress2 = $_POST['Address2'];
            $FCity     = $_POST['City'];
is if $_Post copy the value of the two names to variables. If they are not equal, display the message and go down to the code below (not shown here) but is shown in Post #1.

If they are equal populate the rest of the variables including $Fname.
Then check "Check_field" to see if it contains a value. and then go on on from there.
I need to go out for about 1/2 hour. I will check my e-mail when I return.

Sam
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Coding Problem (If Statement)

Post by McInfo »

Here is some pseudo-code to show the difference between two nestings of if/else blocks.

This is the way your code is structured:

Code: Select all

if (there is something posted) {
    if (the two inputs are not equal) {
    	show an error message
    }
} else (there is nothing posted) {
    no posted values to use here
}
This is the way you want your code to be structured:

Code: Select all

if (there is something posted) {
    if (the two inputs are not equal) {
    	show an error message
    } else (the two inputs are equal) {
        can use the posted values here
    }
}
SamCec
Forum Newbie
Posts: 17
Joined: Mon Aug 16, 2010 11:43 am

Re: Coding Problem (If Statement)

Post by SamCec »

McInfo:
Thank you for your help. Your solution was 100% correct. Further in the code I had another problem but worked it out.

I do have another question: I noticed if the user enters an incorrect number (copy) and chooses to go back to the Form, all of the info entered originally on the form is gone.

My question is this: In this routine:
Syntax: [ Hide ] [ Select ] [ Expand ]
Syntax: [ Show ]
if(!check_field1($Fname))
{
echo "NAME is REQUIRED";
echo '<p />';
echo '<center>';
echo '<a href="GuestBook.html">Return to Guest Book</a>';
echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ';
echo '<a href="Index.shtml">HOME</a>';
echo '</center>';
}
do I re-initialize the values in the form? Something like: form1.name="username".value = $fname
I know this code is not correct but am I on the right tract and can you give me an example of moving info back to the form?

Thanks,
Sam
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Coding Problem (If Statement)

Post by McInfo »

If your guest uses the browser's Back button, the form values should be restored. You can simulate the action of the Back button in a hyperlink by assigning some JavaScript to the link's onclick event:

Code: Select all

history.go(-1);
The alternative is to use sessions in PHP. The process might seem backwards at first: when the user visits the form page, check the session for stored form values and, if found, echo them into the value attributes of the form elements. When processing the form, store the submitted form values in session variables. If the guest visits the form page again in the same session, the inputs will persist.
Post Reply