Jump to content


Photo

Get Data from URL Variables

get url variables

Best Answer mac_gyver, 25 February 2016 - 01:05 PM

yes, the } you have on line 114 does close the function definition.

 

the error is due to the Heredoc closing tags (two places) being indented. they must be the only thing on a line and cannot have any characters before them on the line and can only have a ; and a newline after them on the line.

 

the color highlighting in your programming editor should have stopped changing at the first   EOF; to alert you to this problem (all the code after that point is considered to be part of the string.)

Go to the full post


  • Please log in to reply
12 replies to this topic

#1 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 25 February 2016 - 01:13 AM

Help Please!
 
I'm trying to pass variables in a URL and use them in a HTML email.
 
When test1.php calls Test2.php, test2.php should send two HTML emails but 
nothing happens. :(
 
I get this error for test2.php:
 

[24-Feb-2016 16:20:42 UTC] PHP Parse error:  syntax error, unexpected end of file in 
/home/my/public_html/test2.php on line 114
 
HTML - call test1.php
<!DOCTYPE html>
<html lang="en-US">
 <head>
  <meta charset="utf8" /> 
  <title>Testform.html</title>
 </head>
 <body bgcolor="tan" text ="black"> 
 <form action="test1.php" method="POST" > 
  <table>
   <tr>
    <th>
     ~  Test  ~
      <br />
    </th>
    </tr>
   </table>
    <br />
  <table>
    <tr>
     <td>
   * &nbsp;
  </td> 
  <td>
   <strong> Required Fields </strong>
     </td>
    <tr>
   </table>   
  <table>  
    <tr>
    <td>
      <strong>Title: &nbsp;&nbsp;&nbsp;
      </strong>
    </td>
    <td>
     <select name="Title" size="1" >
   <option value="" selected>Please Choose</option>
      <option value="Mr."> Mr.</option>
      <option value="Ms."> Ms.</option>
      <option value="Mrs."> Mrs.</option>
     </select>
    </td>
   </tr>
   <tr>
    <td>
     <strong> First Name: </strong> &nbsp;
    </td>
    <td>
     <input type="text" name="Fname"  maxlength="30" />
    </td>
   </tr>
   <tr>
    <td>
      <strong> Last Name: </strong> &nbsp;
    </td>
    <td>
     <input type="text" name="Lname"  maxlength="30" />
    </td>
   </tr>
   <tr>
    <td>
     <strong> Suffix: &nbsp;&nbsp;&nbsp; 
     </strong>
    </td>
    <td>
     <select name="Suffix" size="1">
      <option value=""> Please Choose </option>
      <option value="Sr."> Sr.</option>
      <option value="Jr."> Jr.</option>
      <option value="III"> III</option>
      <option value="IV"> IV</option> 
      <option value="V, "> V</option> 
     </select>
    </td>
   </tr>   
   <tr>
    <td>
      <strong> Address: </strong> &nbsp;
    </td>
    <td>
     <input type="text" name="Address"  maxlength=40 />
    </td>
   </tr>
   <tr>
    <td>
      <strong> City, State: </strong> &nbsp;
    </td>
    <td>
      <select name="CityState">
       <option value=""> Please Choose </option>
       <option value="Hometown, CA"> Hometown, CA</option>
       <option value="My City, CA"> My City, CA</option>
       <option value="Beautiful, CA"> Beautiful, CA</option>
      </select>
    </td>
   </tr>
   <tr>
    <td>
      <strong> Zip Code: </strong> &nbsp;
     </font>
    </td>
    <td class="td2">
     <input type="text" name="Zip" placeholder="99999"  maxlength=10 />
    </td>
   </tr>
  </table>
  <table>
   <tr>
    <th>
     <strong> Comments: </strong>
    </th>
   </tr>
  </table> 
  <table>
   <tr>
    <td>
     <textarea rows="8" cols="60" name="Comments" wrap="hard">
     </textarea>
    </td>
   </tr> 
  </table>
  <table>
   <tr>
    <td>
      <input id="shiny" type="submit" value="Submit Form" />
    </td>
   </tr>
  </table>
  </form>
 </body>
</html>
 
TEST1.PHP
<?php
 //###Process testform.html 
 //### Error Reporting 
 error_reporting(E_ALL);
 //### Create Random Number 
 srand((double) microtime() * 1000000);
 //### Define Variable(s)
 $random_number = rand();
 $Test_number = "$random_number";
 $Date = date("D  d M Y - H:i:s ");
 $path = "/home/mypath/public_html/";
 $myfile = "test1.txt";
 $file = $path.$myfile;
 $fh = fopen($file, "a+") or die("Couldn't open $myfile");
 //### Get Data From Form
 if ($_SERVER["REQUEST_METHOD"] === "POST") { 
   if(isset($_POST["Title"])) {
     $Title = $_POST["Title"];
   }  
  if(isset($_POST["Fname"]) && !empty ($_POST["Fname"])) {
   $Fname = $_POST["Fname"];
     if(preg_match("/[^a-zA-Z]*$/",$Fname)){
       //### Upper Case First Letter of First Name
       $FnameUc = ucfirst($Fname);
   } else {
     echo"<strong>The First Name you entered is not valid.</strong>". PHP_EOL;
       echo "<br />\n";
       exit;
    }
  }
  if(isset($_POST["Lname"]) && !empty ($_POST["Lname"])) {
         $Lname = $_POST["Lname"];
     if(preg_match("/[^a-zA-Z]*$/",$Lname)){
       //### Upper Case First Letter of Last Name
       $LnameUc = ucfirst($Lname);
     } else {
     echo"<strong>The Last Name you entered is not valid.</strong>". PHP_EOL;
       echo "<br />\n";
       exit;
     }
  }
   if(isset($_POST["Suffix"]) && !empty ($_POST["Suffix"])) {
     $Suffix = $_POST["Suffix"];
   } 
  if(isset($_POST["Address"]) && !empty ($_POST["Address"])) {
    $Address = $_POST["Address"];
  } else {
      echo "<br />\n";
  }  
  if(isset($_POST["CityState"]) && !empty ($_POST["CityState"])) {
    $CityState = $_POST["CityState"];
  } else {
     echo " <strong> You didn't choose a City, State </strong> ";
     echo " <br /> ";
     die();
   }   
  if(isset($_POST["Zip"]) && !empty ($_POST["Zip"])) {
    $Zip = $_POST["Zip"];
  } else {
     echo "<strong>You didn't enter Zip Code</strong>";
    echo " <br /> ";
     die(); 
   }
 
if(isset($_POST["Email"]) && !empty ($_POST["Email"])) {
    $Email = trim($_POST["Email"]);   
  //### check if e-mail address is well-formed
    if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
      echo $Email . " <strong>is not a valid email address</strong>";
    }
  }
  }
  //### Call Functions
  test1_1();
  test1_2();
 function test1_1(){
  //### Write order to file appointment.txt
  global $Test_number, $Date, $Title, $FnameUc, $LnameUc, $Suffix, $Address, $CityState, $Zip, $Email, $Comments;
 if(is_readable($file)) {
    echo " ";
 } else {
   echo '<strong>The file is not readable</strong>\n\n';
   die();
 }
 if(is_writable($file)) {
   echo " ";
 } else {
    echo '<strong>The file is not writable</strong>';
    die();
 }
  //### Open file
  if(!$fh) {
   die("couldn't open file <i>$myFile</i>");
  }
  else {
   $str = "\r\n";
   $str.= "Test1 -  $Test_number\r\n"; 
   $str.= "Date: $Date\r\n"; 
   $str.= "Name:\r\n";
   $str.= "\t\t $Title $FnameUc $LnameUc $Suffix\r\n";    
   $str.= "Address:\r\n";
   $str.= "\t\t $Address\r\n"; 
   $str.= "City, State:\r\n";
   $str.= "\t\t $CityState $Zip\r\n"; 
   $str.= "\t\t $Email \r\n";
   $str.= "Comments:\r\n"; 
   $str.= "\t\t $Comments\r\n";   
   $str.= "\r\n";
   $str.= "\r\n";            
   fwrite($fh, $str);
  }
   fclose($fh);
 }
 function test1_2() {
  //### Email (HTML) Someone
  global $Test_number, $Date, $Title, $FnameUc, $LnameUc, $Suffix, $Address, $CityState, $Zip, $Email, $Comments;
  //### Define Variables 
  $mailto = $Email;
  $From = "me@mydomain.com";
  $subject = "Test1";
  //### Beginning of HTML message
  $message = <<<EOF
  <html>
   <body BGCOLOR="tan" TEXT="black">
  <br><br>
  $Title $LnameUc, 
<br>
  We have received your Test.
  <br>
  The information you submitted is below:
  <br>
  <b>$Title $FnameUc $LnameUc $Suffix</b>
  <br>
  <b>$Address</b>
  <br>
  <b>$CityState $Zip</b>
  <br>
  <b>$Email</b>
  <br><br>
  Comments:
  <br>
  <b>$Comments</b>
  <br><br>
   Please click your <b><u>Confirm Email Address</u></b> ,below, to confirm your email address.
  <br><br>
* * * * * * * * * *
  <a href="http://www.mydomain.com/test2.php?test_number= $Test_number&date=$Date&title=$Title&fname=$FnameUc&lname=$LnameUc&suffix=$Suffix&address=$Address&citystate= $CityState&zip=$Zip&email=$Email&comments=$Comments">Confirm Email Address -  $Test_number</a>
* * * * * * * * * *
  <br><br>
   </body>
  </html>
EOF;
  //end of message
  $headers  = "Mime-Version: 1.0" . "\r\n";
  $headers .= "Content-type: text/html; charset=iso-8859-1" ."\r\n";
  $headers .= "Sensitivity: Personal\r\n";  
  $headers .= "From:" .$From . "\r\n";
  $headers .= "Reply To:". $From . "\r\n";
  $headers .= "Return-Path:" .$From . "\r\n";
  $headers.= "BCC: me@mydomain.net\r\n";
  $headers .= "X-Priority: 1 (Highest)\r\n";
  $headers .= "X-MSMail-Priority: High\r\n";
  $headers .= "Importance: High\r\n";
  //### now lets send the email
  if(mail($mailto, $subject, $message, $headers)) {
    echo "<b> <font color='green' size='+2'> Test1 sent.</font></b>";
  } else {
    echo "<b> <font color='red' size='+2'>Test1 not sent.</b></font>";
    }
 }
?>
 
 
TEST2.PHP
<?php //### Test2.php called by Test1.php
  //### Error Reporting
  error_reporting(E_ALL);
  //### Define varibles
  $Date = $_GET["date"];
  $Appointment_number = $_GET["appointment_number"];
  $Title = $_GET["title"];
  $FnameUc = $_GET["fname"];
  $LnameUc = $_GET["lname"];
  $Suffix = $_GET["suffix"];
  $Address = $_GET["address"];
  $CityState = $_GET["citystate"];
  $Zip = $_GET["zip"];
  $Email = $_GET["email"];
    $Comments = $_GET["comments"];
  //### Call Function(s)
  test2_1();
  test2_2();
  //### HTML Email to Client
  function test2_1() {
  global $Date, $Test_number, $Title, $FnameUc,
$LnameUc, $Suffix, $Address, $CityState, $Zip, $Email, $Comments;
  //### Define variables
  $mailto = $Email;
  $From = "me@mydomain.com";
  $subject = "Test2 - ". $Test_number."\r\n";
  //### Beginning of HTML message
  $message = <<<EOF
  <html>
  <body BGCOLOR="tan" TEXT="black">
  <br><br>
  $Title $FnameUc $LnameUc $Suffix
  <br>
  Your email address <b><$mailto></b> is confirmed.
  <br><br>
  </body>
  </html>
  EOF;
   //### end of message
  $headers  = "Mime-Version: 1.0\r\n";
  $headers .= "Content-type: text/html;  charset=iso-8859-1\r\n";
  $headers .= "Sensitivity: Personal\r\n";
  $headers .= "From: ".$From. "\r\n";
  $headers .= "Reply-To: ".$From. "\r\n";
  $headers .= "Return-Path: ".$From. "\r\n";
  $headers .= "BCC: horacef@horacefranklinjr.net \r\n";
  $headers .= "X-Priority: 1 (Highest)\r\n";
  $headers .= "X-MSMail-Priority: High\r\n";
  //###  Now lets send the email
  if(mail($mailto, $subject, $message, $headers))  {
    echo "<b> <font color='green' size='+2'>Email address Confirmation sent to: ".$mailto."</font></b>";
    echo "<br /> <br />";
  } else {
    echo "<b> <font color='red' size='+2'>Email address Confirmation not sent.</font></b>";
    echo "<br /> <br />";
  }
  }
    //### HTML Email to Horace
    function test2_2() {
    global $Date, $Appointment_number, $Title, $FnameUc,
$LnameUc, $Suffix, $Address, $CityState, $Zip, $Email, $Comments;
    //### Define variables
    $mailto = $Email;
    $From = "me@mydomain.com";
    $subject = "Test2 - ". $Test_number."\n\n";
    //### begin of HTML message
    $message = <<<EOF
    <html>
    <body BGCOLOR="tan" TEXT="black">
    <br><br>
    You have received an Test, <b>that needs to be confirmed.</b>
     <br>
     Date Received: <b>$Date</b>
     <br><br>
     The information submitted is below:
     <br>
     <b>$Title $FnameUc $LnameUc $Suffix</b>
     <br>
     <b>$Address</b>
     <br>
     <b>$CityState $Zip</b>
     <br>
     <b>$mailto</b>
     <br><br>
     Comments:
     <br>
     <b>$Comments</b>
     <br><br>
     Please click the <b><u>Confirm Appointment</u></b> ,below, to confirm the Appointment Date and Time.
    <br><br>
  <a href="http://www.mydomain.com/Test3.php?date=$Date&test_number=$Test_number&title=$Title&fname=$FnameUc&lname=$LnameUc&suffix=$Suffix&address=$Address&citystate=$CityState&zip=$Zip&email=$Email&comments=$Comments"> Confirm  Appointment -  $Test_number</a>
    <br>
    </body>
    </html>
    EOF;
    //###end of message
    $headers  = "Mime-Version: 1.0\r\n";
    $headers .= "Content-type: text/html;  charset=iso-8859-1\r\n";
    $headers .= "Sensitivity: Personal\r\n";
    $headers .= "From:" . $From . "\r\n";
    $headers .= "Reply-To:" . $From . "\r\n";
    $headers .= "Return-Path:" . $From . "\r\n";
    $headers .= "BCC: me@mydomain.net\r\n";
    $headers .= "X-Priority: 1 (Highest)\r\n";
    $headers .= "X-MSMail-Priority: High\r\n";
    //### Now lets send the email
    if(mail($mailto, $subject, $message, $headers)){
      echo "<b> <font color='green' size='+2'> Confirm Appointment email sent to: ". $mailto."</b></font>";
      echo "<br /> <br />";
    } else {
      echo "<b> <font color='red' size='+2'> Confirm Appointment email not sent.</b></font>";
      echo "<br /> <br />";
    }
  ==> Line 114 }

Moving up the php programming ladder to php Guru.

#2 Jacques1

Jacques1
  • Members
  • PipPipPip
  • Turtles all the way down
  • 4,224 posts

Posted 25 February 2016 - 02:55 AM

Since you seem to struggle a lot with syntax errors, I recommend you grab a proper IDE (integrated development environment) like Netbeans or Eclipse. This will analyze your code as you write it and notify you immediately when things go wrong. Then you don't end up with 100 lines of broken code.

 

It's also helpful to write code step-by-step and top-down. That is, you start with the basic structure of the script and then slowly add new statements, testing each stage.

 

As to your code: The terminators of heredocs must not be indented (they need to start in column zero), and you're missing the closing brace for the test2_2 function. Besides that, your code is a complete mess full of security vulnerabilities…



#3 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 25 February 2016 - 12:04 PM

One day my hope is to be a PHP 'Guru' but until that day arrives I must read and filter responses from Guru's like you. Who forget that before they 'earned' the title 'Guru' they were noobs/newbies that made mistakes coding and needed help too. 

 

Is the missing closing brace for function test2_2() on line 114?

 

Thanks for responding.


Moving up the php programming ladder to php Guru.

#4 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 25 February 2016 - 12:26 PM

test2.php

    function test2_2() {
    global $Date, $Appointment_number, $Title, $FnameUc,
$LnameUc, $Suffix, $Address, $CityState, $Zip, $Email, $Comments;
    //### Define variables
    $mailto = $Email;
    $From = "me@mydomain.com";
    $subject = "Test2 - ". $Test_number."\n\n";
    //### begin of HTML message
    $message = <<<EOF
    <html>
    <body BGCOLOR="tan" TEXT="black">
    <br><br>
    You have received an Test, <b>that needs to be confirmed.</b>
     <br>
     Date Received: <b>$Date</b>
     <br><br>
     The information submitted is below:
     <br>
     <b>$Title $FnameUc $LnameUc $Suffix</b>
     <br>
     <b>$Address</b>
     <br>
     <b>$CityState $Zip</b>
     <br>
     <b>$mailto</b>
     <br><br>
     Comments:
     <br>
     <b>$Comments</b>
     <br><br>
     Please click the <b><u>Confirm Appointment</u></b> ,below, to confirm the Appointment Date and Time.
    <br><br>
  <a href="http://www.mydomain.com/Test3.php?date=$Date&test_number=$Test_number&title=$Title&fname=$FnameUc&lname=$LnameUc&suffix=$Suffix&address=$Address&citystate=$CityState&zip=$Zip&email=$Email&comments=$Comments"> Confirm  Appointment -  $Test_number</a>
    <br>
    </body>
    </html>
    EOF;
    //###end of message
    $headers  = "Mime-Version: 1.0\r\n";
    $headers .= "Content-type: text/html;  charset=iso-8859-1\r\n";
    $headers .= "Sensitivity: Personal\r\n";
    $headers .= "From:" . $From . "\r\n";
    $headers .= "Reply-To:" . $From . "\r\n";
    $headers .= "Return-Path:" . $From . "\r\n";
    $headers .= 'BCC: me@mydomain.net'."\r\n";
    $headers .= "X-Priority: 1 (Highest)\r\n";
    $headers .= "X-MSMail-Priority: High\r\n";
    //### Now lets send the email
    if(mail($mailto, $subject, $message, $headers)){
      echo "<b> <font color='green' size='+2'> Confirm Appointment email sent to: ". $mailto."</b></font>";
      echo "<br /> <br />";
    } else {
      echo "<b> <font color='red' size='+2'> Confirm Appointment email not sent.</b></font>";
      echo "<br /> <br />";
    }
  ==> Line 114 }

Moving up the php programming ladder to php Guru.

#5 mac_gyver

mac_gyver
  • Staff Alumni
  • Staff Alumni
  • 4,095 posts

Posted 25 February 2016 - 01:05 PM   Best Answer

yes, the } you have on line 114 does close the function definition.

 

the error is due to the Heredoc closing tags (two places) being indented. they must be the only thing on a line and cannot have any characters before them on the line and can only have a ; and a newline after them on the line.

 

the color highlighting in your programming editor should have stopped changing at the first   EOF; to alert you to this problem (all the code after that point is considered to be part of the string.)


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting.

#6 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 25 February 2016 - 07:07 PM

Mac_gyver, 

 

I have removed the indentation from the heredoc per:

$message = <<<EOF

EOF;

 

I will test it and share the results of the test

 

Thank you.


Moving up the php programming ladder to php Guru.

#7 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,864 posts
  • LocationCanada

Posted 25 February 2016 - 09:32 PM

Some other feedback:

 

1. You are using GLOBAL in a function - don't! It is a bad practice. Instead, pass the variables to the function that it needs. You are creating a lot of them, so rather than pass each variable as individual values, create the data in an array then simple pass the array to the function.

 

2. Give your variables and functions meaningful names. test2_1() doesn't tell you anything about what that function does. Yes, as you are actively working on it you know what id does, but when you need others to help you with code or when you need to go back to code that is even a day or two old you have to relearn what they are.

 

3. You are trying to enforce business rules that you should not. For example, you are forcing the names to start with an uppercase character and the remaining letters to be lower case. There are many names where this would be invalid: "De La Hoya", "da Vinci", etc.

 

4. You have some 'validations' that have holes. E.g. !empty ($_POST["Lname"]) If the user just enters one or more spaces it will pass this validation. Additionally, values such as "0" or "FALSE" would cause that function to return the same as an empty string. For a name that might be fine, but in other contexts the string "false" or a "0" (zero) may be legitimate values. Instead, you should first trim() the value before checking to see if a value was passed.

 

5. There are conditions with no handling of the false results. E.g.

if(isset($_POST["Lname"]) && !empty ($_POST["Lname"])) {
         $Lname = $_POST["Lname"];
     if(preg_match("/[^a-zA-Z]*$/",$Lname)){
       //### Upper Case First Letter of Last Name
       $LnameUc = ucfirst($Lname);
     } else {
     echo"<strong>The Last Name you entered is not valid.</strong>". PHP_EOL;
       echo "<br />\n";
       exit;
     }
  }

What happens if $_POST["Lname"] is empty or if it is empty? There is no error handling and the code will go continue on and possibly fail when it tries to reference $LnameUc

 

6. Error handling should not require an exit() function and it should not stop checking for errors on the first error encountered. You should try to provide the user with as many of the error conditions as possible. For example, if they are inserting a new record, check for the possible date entry errors on each piece of data (required fields, format, data type (e.g. numbers), etc.) and store the errors into an array. Then after all the validations are complete, if there are any errors, provide them to the user and complete the page. If there are no errors then attempt the next stage in the process, such as inserting the record. If there are errors at that point then show that error and complete the page. There's nothing more frustrating then submitting a form to be told a piece of data needs to be corrected, resubmitting and then being told a different piece of data was also invalid - but not mentioned previously.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#8 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 25 February 2016 - 10:21 PM

Mac_gyver,

Your analysis of the problem and the solution solved the problem.

Thank you.

Phycho,

Thank you for providing me with a through
Analysis of the problem areas of my code.

I'M sure it will put me one step closer to obtaining my goal of becoming a 'php Guru'.
Moving up the php programming ladder to php Guru.

#9 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,864 posts
  • LocationCanada

Posted 25 February 2016 - 10:58 PM

Phycho,

Thank you for providing me with a through
Analysis of the problem areas of my code.

I'M sure it will put me one step closer to obtaining my goal of becoming a 'php Guru'.

 

That was by no means a thorough analysis - it was just a handful of things that I readily saw. There were plenty of other things, I just don't have the time to comment on everything.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#10 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 26 February 2016 - 04:45 PM

Psycho,

I really appreciate the advice you gave me about php programming. Please read my Responses.

Some other feedback:

1. You are using GLOBAL in a function - don't! It is a bad practice. Instead, pass the variables to the function that it needs. You are creating a lot of them, so rather than pass each variable as individual values, create the data in an array then simple pass the array to the function.

I changed this:
function use_form_data() {
global var1, var2, var3, etc.

To this:
function use_form_data($form_data) {
//### Define variables
echo "Subject ".$form_data [0];
echo "Name ".$form_data [1]." ".$form_data [2]." ".$form_data [3]." ".$form_data [4]
."etc\r\n";
}

2. Give your variables and functions meaningful names. test2_1() doesn't tell you anything about what that function does. Yes, as you are actively working on it you know what id does, but when you need others to help you with code or when you need to go back to code that is even a day or two old you have to relearn what they are.

See '2.' above


3. You are trying to enforce business rules that you should not. For example, you are forcing the names to start with an uppercase character and the remaining letters to be lower case. There are many names where this would be invalid: "De La Hoya", "da Vinci", etc.

You are right, of course, so I changed the code to this:

if(isset($_POST["fname"]) && !empty ($_POST["fname"])) {
$Lname = trim($_POST["fname"]);
if(preg_match("/[^a-zA-Z]*$/",$fname)){
} else {
echo"<strong>The First Name you entered is not valid.</strong>";
echo "<br />\n";
}
if(empty($_POST["fname"])) {
echo"<strong>You didn't enter a First Name.</strong>";
echo "<br />\n";
}
}

Note: I don't there is any way to, completely, prevent a user from putting garbage in input fields i.e. xxx xxx would pass the validation.

Also:
What preg_match validation matches the following U.S. street address:

123 Main St.
123 Main St
123 Main Street
123 NW Main
123 Main St. North
123 Main St., Apt. 4
123 Main St., Suite 456

if(isset($_POST["address"]) && !empty ($_POST["adresss"])) {
$address = trim($_POST["address"]);

* * * My Guess is * * *
if(preg_match("/([0-9]+)\s([a-zAZ]+)(,\s|\s)([a-zAZ]+)(,\s|\s)([a-zAZ]+)(,\s|\s)([a-zAZ0-9]+)*$/",$address)){

* * * Am I right? * * *
} else {
echo"<strong>The address you entered is not valid.</strong>";
echo "<br />\n";

}
if(empty($_POST["address"])) {
echo"<strong>You didn't enter a address.</strong>";
echo "<br />\n";
}
}

4. You have some 'validations' that have holes. E.g. !empty ($_POST["Lname"]) If the user just enters one or more spaces it will pass this validation. Additionally, values such as "0" or "FALSE" would cause that function to return the same as an empty string. For a name that might be fine, but in other contexts the string "false" or a "0" (zero) may be legitimate values. Instead, you should first trim() the value before checking to see if a value was passed.

See '3.' above.

5. There are conditions with no handling of the false results. E.g.
if(isset($_POST["Lname"]) && !empty ($_POST["Lname"])) {
$Lname = $_POST["Lname"];
if(preg_match("/[^a-zA-Z]*$/",$Lname)){
//### Upper Case First Letter of Last Name
$LnameUc = ucfirst($Lname);
} else {
echo"<strong>The Last Name you entered is not valid.</strong>". PHP_EOL;
echo "<br />\n";
exit;
}
}
What happens if $_POST["Lname"] is empty or if it is empty? There is no error handling and the code will go continue on and possibly fail when it tries to reference $LnameUc

See '3.' above.

6. Error handling should not require an exit() function and it should not stop checking for errors on the first error encountered. You should try to provide the user with as many of the error conditions as possible. For example, if they are inserting a new record, check for the possible date entry errors on each piece of data (required fields, format, data type (e.g. numbers), etc.) and store the errors into an array. Then after all the validations are complete, if there are any errors, provide them to the user and complete the page. If there are no errors then attempt the next stage in the process, such as inserting the record. If there are errors at that point then show that error and complete the page. There's nothing more frustrating then submitting a form to be told a piece of data needs to be corrected, resubmitting and then being told a different piece of data was also invalid - but not mentioned previously.


Huh?
Example please.
Moving up the php programming ladder to php Guru.

#11 Jacques1

Jacques1
  • Members
  • PipPipPip
  • Turtles all the way down
  • 4,224 posts

Posted 26 February 2016 - 06:48 PM

Forget about “validating” human names. You somehow seem to think that there are no characters outside of the latin alphabet and that people are named either “Joe” or “Jane”, but that's not the case. There's a large variety of different languages and names. There's José de San Martín, Bjørn Kjos, Jürgen Kleine-Frauns and Bhārat kē Pradhānmantrī. Who are you to say that those names are “invalid”?

 

Yes, you could force everybody to transliterate their names to make your PHP script happy, but this is user-hostile, distriminating, confusing and completely unnecessary. We have Unicode now, there's no reason to artificially limit your users to 7-bit ASCII.

 

If at all, you'd only check for basic features like “There must be at least one graphic symbol”:

<?php

const HUMAN_NAME_REGEX = '/[[:graph:]]/u';



$test_name = 'Bjørn Kjos';
var_dump( preg_match(HUMAN_NAME_REGEX, $test_name) );


#12 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 27 February 2016 - 01:31 PM

That was by no means a thorough analysis - it was just a handful of things that I readily saw. There were plenty of other things, I just don't have the time to comment on everything.

I appreciate you taking the time to point out 'readily' identifiable errors.

Thank you.

Edited by phpnewbieca, 27 February 2016 - 01:45 PM.

Moving up the php programming ladder to php Guru.

#13 phpnewbieca

phpnewbieca
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts
  • LocationCA
  • Age:68

Posted 27 February 2016 - 01:34 PM

Forget about “validating” human names. You somehow seem to think that there are no characters outside of the latin alphabet and that people are named either “Joe” or “Jane”, but that's not the case. There's a large variety of different languages and names. There's José de San Martín, Bjørn Kjos, Jürgen Kleine-Frauns and Bhārat kē Pradhānmantrī. Who are you to say that those names are “invalid”?

Yes, you could force everybody to transliterate their names to make your PHP script happy, but this is user-hostile, distriminating, confusing and completely unnecessary. We have Unicode now, there's no reason to artificially limit your users to 7-bit ASCII.

If at all, you'd only check for basic features like “There must be at least one graphic symbol”:

<?phpconst HUMAN_NAME_REGEX = '/[[:graph:]]/u';$test_name = 'Bjørn Kjos';var_dump( preg_match(HUMAN_NAME_REGEX, $test_name) );
Good point, going international.

Edited by phpnewbieca, 27 February 2016 - 01:44 PM.

Moving up the php programming ladder to php Guru.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users