Jump to content

phpnewbieca

Members
  • Posts

    71
  • Joined

  • Last visited

Everything posted by phpnewbieca

  1. Good point, going international.
  2. I appreciate you taking the time to point out 'readily' identifiable errors. Thank you.
  3. 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.
  4. 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'.
  5. 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.
  6. test2.php function test2_2() { global $Date, $Appointment_number, $Title, $FnameUc, $LnameUc, $Suffix, $Address, $CityState, $Zip, $Email, $Comments; //### Define variables $mailto = $Email; $From = "[email protected]"; $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: [email protected]'."\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 }
  7. 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.
  8. 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> * </td> <td> <strong> Required Fields </strong> </td> <tr> </table> <table> <tr> <td> <strong>Title: </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> </td> <td> <input type="text" name="Fname" maxlength="30" /> </td> </tr> <tr> <td> <strong> Last Name: </strong> </td> <td> <input type="text" name="Lname" maxlength="30" /> </td> </tr> <tr> <td> <strong> Suffix: </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> </td> <td> <input type="text" name="Address" maxlength=40 /> </td> </tr> <tr> <td> <strong> City, State: </strong> </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> </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 = "[email protected]"; $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: [email protected]\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 = "[email protected]"; $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: [email protected] \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 = "[email protected]"; $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: [email protected]\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 }
×
×
  • 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.