Jump to content

Recommended Posts

Before 2 days ago, I knew nothing about PHP. I read some tutorials and followed some examples and got to this point. I don't do this for a living, I'm retired and trying to keep the mind active.

 

I wrote using HTML a page that has a <form> tag on it. The <form> tag has ACTION = "mail.php". The contents of the form, I want e-mailed to me.  Without the attempted "IF" clause THAT PART IS WORKING. The first input of the form is username--that's required. I am having  trouble with that. Here is my code of "mail.php":

<html>
<body>
<?php
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;
    }
    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>';
	<center>
                 <a href="Index.shtml">HOME</a>
	</center>
       }
   }
   function check_field1($fname)
   {
     if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\
	]+$/s”,$fname))
       return TRUE;
     else
       echo 'Name is REQUIRED!';
       return FALSE;
   }
}
?> 
</body>
</html>

 

 

When I run this, I get this error:

Parse error: syntax error, unexpected '<' in /homepages/8/d271334063/htdocs/McDevitt/mail.php on line 31
which is the <center> tag.

 

I then took the <center> and </center> tags off but left the line positioning the same. I got this error:

Parse error: syntax error, unexpected '<' in /homepages/8/d271334063/htdocs/McDevitt/mail.php on line 32
This is the <a...> tag.

 

Sorry folks, I can't figure what the problem is. I need help.

 

Thanks,

Sam

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/145889-a-newbie-needs-help-with-php-code/
Share on other sites

Since PHP cannot process exclusive HTML tags u need to echo them

Just do this and it will work :

 

echo '<p>The Guest Book was updated. Thank You.</p>';
echo '<center>';
echo '<a href="Index.shtml">HOME</a>';
echo '</center>';

 

A tip for the parse errors !

This mostely means that php cannot understand what is at that line :P

Just something to know in the future :P

Since PHP cannot process exclusive HTML tags u need to echo them

Just do this and it will work :

 

echo '<p>The Guest Book was updated. Thank You.</p>';
echo '<center>';
echo '<a href="Index.shtml">HOME</a>';
echo '</center>';

 

A tip for the parse errors !

This mostely means that php cannot understand what is at that line :P

Just something to know in the future :P

 

Thank you, that helped but now I am getting a Parse error on line 47, the </body> tag.

I then removed the <body> and </body> tags and the Parse error still appeared on the preceding lines.

Based on the color highlighting that the forum software did to your posted code, your preg_match() has a problem with the closing double-quote. This ” is some word processor smart/curved quote and is not the same as ".

Based on the color highlighting that the forum software did to your posted code, your preg_match() has a problem with the closing double-quote. This is some word processor smart/curved quote and is not the same as ".

 

indeed thats the problem

open wordpad

delete that "

replace it with a new " from wordpad wich will make it work

Based on the color highlighting that the forum software did to your posted code, your preg_match() has a problem with the closing double-quote. This ” is some word processor smart/curved quote and is not the same as ".

 

indeed thats the problem

open wordpad

delete that "

replace it with a new " from wordpad wich will make it work

Folks: I don't think I was meant to be a PHP coder. Your above solution worked but...

Fatal error: Call to undefined function: check_field1() in /homepages/8/d271334063/htdocs/McDevitt/mail.php on line 21
I checked the spelling and it looks good.

place ur function checkfield on top of ur page

ur function is not yet declared so the page doesnt know the function

 

make it like this

 

<html>
<body>
<?php
//insert functions first
function check_field1($fname)
   {
     if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\
	]+$/s",$fname))
       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;
    }
    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>';
	<center>
                 <a href="Index.shtml">HOME</a>
	</center>
       }
   }
}
?> 
</body>
</html>

Your function definition is inside of a conditional statement so it will only be defined when the condition is true. In this conditionally defined case, a function definition must be before the call to the function.

 

The best solution is to not put function definitions inside of conditional statements and to put them near the start of your code.

Your function definition is inside of a conditional statement so it will only be defined when the condition is true. In this conditionally defined case, a function definition must be before the call to the function.

 

The best solution is to not put function definitions inside of conditional statements and to put them near the start of your code.

 

Like i indicated ^^

But nice addon to my solution :P

Your function definition is inside of a conditional statement so it will only be defined when the condition is true. In this conditionally defined case, a function definition must be before the call to the function.

 

The best solution is to not put function definitions inside of conditional statements and to put them near the start of your code.

 

Like i indicated ^^

But nice addon to my solution :P

Folks: Thank you so much for your help. I am not getting any "Parsing" errors, The function is not working. I believe it is checking spaces as a valid character and therefore returning a TRUE even when I do not enter a name. The USERNAME can be entered FirstNamespaceLastname.

Question: In PHP can you check the length of the input?

Thanks again,

Sam

I still need help Here's what I have on the code:

<html>
<body>
<?php
function check_field1($fname)
   {

    if(!preg_match("/[a-zA-Z]+$/s",$Fname))
       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;
    }
    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>

The function is not working. First of all, the USERNAME can be entered as FirstNameSpaceLastName. My intent: if the username is not entered, show the words "Name is Required" and DO NOT send the e-mail. If the USERNAME is entered, then send the e-mail.

 

What is currently happening, the e-mail is sent  even when nothing is in the USERNAME. I was thinking about using the strlen function and checking for a length > 0. I tried that and my syntax was wrong.

 

Question: Is there a better way to do this? Should I have separate <INPUT> tags for FirstName and LastName?

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.