PHP - Form Validation Not Producing The Error Messages When It Should
Similar TutorialsHow is it possible, in PHP, to display an error message next to a form input text field if a user is attempting to submit a form with empty fields? Moreover, how is it possible to remove the same error message when the user fills in the input field with the required data and/or refreshes the page? I have 2 files that I am working with: application.php and process.php.
application.php mainly has the HTML of the form. I am very new to learning PHP (I started learning it last week) and I have been searching for hours for a clear answer. I have already tried different methods for generating the error message including: using empty(), !isset, $_POST["name"] = "", etc, and have tried using session_start();, $row, echo, print, and other variables to try and display error message on the page, and I have tried using unset();, and = null, to try and remove the error message once the input field has been filled but all to no avail. Either the method I try only half works, or nothing works, and I cannot pinpoint which is the part that is not working. I only have 2 files to work with, and as an example of what I want to do is:
1. If the first name field is empty when the user clicks submit, an error message should appear next to the input. Is this possible with PHP? Hi, I have a separate form created using Dreamweaver that calls a separate php script when the Submit button is clicked. Currently I am able display form validation messages in a new html page. If the user leaves fields blank, I would like the messages to appear on the form itself instead of in a new page. How do you make the messages from the php form validation code display into the calling html form? I know I could just do this with Dreamweaver but I would like to learn to do this using php. It might be easier to embed the code within the html page but I was thinking that using the separate script would be more secure. My form can be found here. Validation is working but opens a new page: http://www.tallfirshoa.com/adform.htm Thanks! Rob Hi.. Im new to php.. This is my form validate code.. im getting errors...i did my best to do this code Can anyone help me and correct my code Code: [Select] <?php session_start(); // submit button clicked if(isset($_POST['submit'])){ $errors=array(); // Continue when clicked if(isset($_POST['Role'], $_POST['Name'], $_POST['datepicker'], $_POST['gender'], $_POST['activity'], $_POST['Address'], $_POST['Photo'], $_POST['Biodata'], $_POST['certificates'], $_POST['Salary'])){ // Fields are all set else error : No errors conintue // Set variables an sanitise $Emp_Save= mysql_real_escape_string($_POST['Emp_Save']); // Sanitise $Emp_Cancel=$_POST['Emp_Cancel']; // Sanitise $Role=$_POST['Role']; // Sanitise $Name=$_POST['Name']; // Sanitise $datepicker=$_POST['datepicker']; // Sanitise $gender=$_POST['gender']; // Sanitise $activity=$_POST['activity']; // Sanitise $Address=$_POST['Address']; // Sanitise $Photo=$_POST['Photo']; // Sanitise $Biodata=$_POST['Biodata']; // Sanitise $certificates=$_POST['certificates']; // Sanitise $Salary=$_POST['Salary']; // Sanitise //-- Continue with error checks ;; if($_Role == ""){ $errors[]='Please Select The Employee Role!'; } if(strlen($Name >30){ $errors[]='Name Is Too Long!'; } else if(empty($Name)) { $errors[]='Please Fill The Field!'; } // Vslidate Datepicker if(strlen($datepicker) != 10){ $errors[]='Date should only be 10 characters and number .'; } // explode datepaicker by what ever youre using to separate them $datepicker_ex = explode('/', $datepicker); // Check that theyre numbers if(!is_numeric($datepicker_ex[0]) || !is_numeric($datepicker_ex[1]) || !is_numeric($datepicker_ex[2])){ $errors[]='Date should only be number .'; } else { // they are number format them back and cast to int , just incase $datepicker = (int)$datepicker_ex[0].'/'.(int)$datepicker_ex[1].'/'.(int)$datepicker_ex[2]; } // END - date picker valiadation else if($gender == ""){ $errors[]='Please Select The Gender!'; } else if($Activity == ""){ $errors[]='Please Select the Status!'; } else if($Address == ""){ $errors[]='Please Enter The Address!'; if(array_key_exists('Photo', $_FILES)) { $allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg'); $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Validate the uploaded file if($_FILES['Photo']['size'] === 0 || empty($_FILES['Photo']['tmp_name'])) { echo("<p>No file was selected.</p>\r\n"); } else if($_FILES['Photo']['size'] > 50000) { echo("<p>The file was too large.</p>\r\n"); } else if($_FILES['Photo']['error'] !== UPLOAD_ERR_OK) { // There was a PHP error echo("<p>There was an error uploading.</p>\r\n"); // Check if the filetype is allowed, if not DIE and inform the user. if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); } if(array_key_exists('Biodata', $_FILES)) { $allowed_filetypes = array('.pdf'); $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Validate the uploaded file if($_FILES['Biodata']['size'] === 0 || empty($_FILES['Biodata']['tmp_name'])) { echo("<p>No file was selected.</p>\r\n"); } else if($_FILES['Biodata']['size'] > 50000) { echo("<p>The file was too large.</p>\r\n"); } else if($_FILES['Biodata']['error'] !== UPLOAD_ERR_OK) { // There was a PHP error echo("<p>There was an error uploading.</p>\r\n"); if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); } if(array_key_exists('certificates', $_FILES)) { $allowed_filetypes = array('.pdf'); $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Validate the uploaded file if($_FILES['certificates']['size'] === 0 || empty($_FILES['certificates']['tmp_name'])) { echo("<p>No file was selected.</p>\r\n"); } else if($_FILES['certificates']['size'] > 50000) { echo("<p>The file was too large.</p>\r\n"); } else if($_FILES['certificates']['error'] !== UPLOAD_ERR_OK) { // There was a PHP error echo("<p>There was an error uploading.</p>\r\n"); if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); } elseif($_EmpSalary == ""){ $errors[]='Please Enter The Salary!'; } //----- End error checks do insert or display errors of any are found -- if(empty($errors)){ // do insert $query = mysql_query("INSERT INTO `formdetails` (role,name,,date_of_birth,gender,activity,address,photo,biodata,certificates,salary) VALUES ('$Role', '$Name','$datepicker',, '$gender','$activity','$Address','$Photo','$Biodata','$certificates','$_EmpSalary')"); } else { foreach($errors as $error){ // display errors echo $error; } } //--------------------------------------------------- } else { $errors[]= 'All fields required.'; } } // END -- ?> MOD EDIT: [code] . . . [/code] BBCode tags added. Hello to everyone! I'm complete new in PHP and have the next problem: Please correct the following error: Enter your name doesn't matter if the field(Your name:) is filled or not Code: [Select] <form method="post" action="send.php" id="recomendus"> <table style="width: 100%;" border="0" cellspacing="6px" cellpadding="0" align="center"> <tbody> <tr> <td width="1%"><span style="color: #ff0000;">*</span></td> <td width="39%">Email:</td> <td width="60%"><label> <input id="email" type="text" name="clentEmail" value="" /> </label></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>First Name:</td> <td><input id="clentFname" type="text" name="yourname" value="" /></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Last Name:</td> <td><input id="clentLname" type="text" name="clentLname" value="" /></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Country:</td> <td><select id="Country2" name="Country2"> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option> <option value="American Samoa">American Samoa</option> <option value="Andorra">Andorra</option> <option value="Angola">Angola</option> <option value="Anguilla">Anguilla</option> <option value="Antarctica">Antarctica</option> <option value="Antigua and Barbuda">Antigua and Barbuda</option> <option value="Argentina">Argentina</option> <option value="Armenia ">Armenia </option> <option value="Aruba">Aruba</option> <option value="Australia">Australia</option> <option value="Austria">Austria</option> <option value="Azerbaijan">Azerbaijan</option> <option value="Bahamas">Bahamas</option> <option value="Bahrain">Bahrain</option> <option value="Bangladesh">Bangladesh</option> <option value="Barbados">Barbados</option> <option value="Belarus">Belarus</option> <option value="Belgium">Belgium</option> <option value="Belize">Belize</option> <option value="Benin">Benin</option> <option value="Bermuda ">Bermuda </option> <option value="Bhutan">Bhutan</option> <option value="Bolivia">Bolivia</option> <option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option> <option value="Botswana">Botswana</option> <option value="Bouvet Island">Bouvet Island</option> <option value="Brazil">Brazil</option> <option value="British Indian Ocean Territory">British Indian Ocean Territory</option> <option value="Brunei Darussalam ">Brunei Darussalam </option> <option value="Bulgaria">Bulgaria</option> <option value="Burkina Faso">Burkina Faso</option> <option value="Burundi">Burundi</option> <option value="Cambodia">Cambodia</option> <option value="Cameroon">Cameroon</option> <option value="Canada">Canada</option> <option value="Cape Verde">Cape Verde</option> <option value="Cayman Islands">Cayman Islands</option> <option value="Central African Republic ">Central African Republic </option> <option value="Chad">Chad</option> <option value="Chile">Chile</option> <option value="China">China</option> <option value="Christmas Island">Christmas Island</option> <option value="Cocos Islands">Cocos Islands</option> <option value="Colombia">Colombia</option> <option value="Comoros">Comoros</option> <option value="Congo">Congo</option> <option value="Congo, Democratic Republic of the">Congo, Democratic Republic of the</option> <option value="Cook Islands ">Cook Islands </option> <option value="Costa Rica">Costa Rica</option> <option value="Cote d">Cote d'Ivoire</option> <option value="Croatia">Croatia</option> <option value="Cuba">Cuba</option> <option value="Cyprus">Cyprus</option> <option value="Czech Republic">Czech Republic</option> <option value="Denmark">Denmark</option> <option value="Djibouti">Djibouti</option> <option value="Dominica">Dominica</option> <option value="Dominican Republic">Dominican Republic</option> <option value="Ecuador ">Ecuador </option> <option value="Egypt">Egypt</option> <option value="El Salvador">El Salvador</option> <option value="Equatorial Guinea">Equatorial Guinea</option> <option value="Eritrea">Eritrea</option> <option value="Estonia">Estonia</option> <option value="Ethiopia">Ethiopia</option> <option value="Falkland Islands">Falkland Islands</option> <option value="Faroe Islands">Faroe Islands</option> <option value="Fiji">Fiji</option> <option value="Finland">Finland</option> <option value="France ">France </option> <option value="French Guiana">French Guiana</option> <option value="French Polynesia">French Polynesia</option> <option value="Gabon">Gabon</option> <option value="Gambia">Gambia</option> <option value="Georgia">Georgia</option> <option value="Germany">Germany</option> <option value="Ghana">Ghana</option> <option value="Gibraltar">Gibraltar</option> <option value="Greece">Greece</option> <option value="Greenland">Greenland</option> <option value="Grenada">Grenada</option> <option value="Guadeloupe ">Guadeloupe </option> <option value="Guam">Guam</option> <option value="Guatemala">Guatemala</option> <option value="Guinea">Guinea</option> <option value="Guinea-Bissau">Guinea-Bissau</option> <option value="Guyana">Guyana</option> <option value="Haiti">Haiti</option> <option value="Heard Island and McDonald Islands">Heard Island and McDonald Islands</option> <option value="Honduras">Honduras</option> <option value="Hong Kong">Hong Kong</option> <option value="Hungary ">Hungary </option> <option value="Iceland">Iceland</option> <option value="India">India</option> <option value="Indonesia">Indonesia</option> <option value="Iran">Iran</option> <option value="Iraq">Iraq</option> <option value="Ireland">Ireland</option> <option value="Israel">Israel</option> <option value="Italy">Italy</option> <option value="Jamaica">Jamaica</option> <option value="Japan">Japan</option> <option value="Jordan">Jordan</option> <option value="Kazakhstan">Kazakhstan</option> <option value="Kenya">Kenya</option> <option value="Kiribati">Kiribati</option> <option value="Kuwait">Kuwait</option> <option value="Kyrgyzstan ">Kyrgyzstan </option> <option value="Laos">Laos</option> <option value="Latvia">Latvia</option> <option value="Lebanon">Lebanon</option> <option value="Lesotho">Lesotho</option> <option value="Liberia">Liberia</option> <option value="Libya">Libya</option> <option value="Liechtenstein">Liechtenstein</option> <option value="Lithuania">Lithuania</option> <option value="Luxembourg">Luxembourg</option> <option value="Macao">Macao</option> <option value="Madagascar">Madagascar</option> <option value="Malawi">Malawi</option> <option value="Malaysia">Malaysia</option> <option value="Maldives ">Maldives </option> <option value="Mali">Mali</option> <option value="Malta">Malta</option> <option value="Marshall Islands">Marshall Islands</option> <option value="Martinique">Martinique</option> <option value="Mauritania">Mauritania</option> <option value="Mauritius">Mauritius</option> <option value="Mayotte">Mayotte</option> <option value="Mexico">Mexico</option> <option value="Micronesia">Micronesia</option> <option value="Moldova">Moldova</option> <option value="Monaco">Monaco</option> <option value="Mongolia">Mongolia</option> <option value="Montenegro ">Montenegro </option> <option value="Montserrat">Montserrat</option> <option value="Morocco">Morocco</option> <option value="Mozambique">Mozambique</option> <option value="Myanmar">Myanmar</option> <option value="Namibia">Namibia</option> <option value="Nauru">Nauru</option> <option value="Nepal">Nepal</option> <option value="Netherlands">Netherlands</option> <option value="Netherlands Antilles">Netherlands Antilles</option> <option value="New Caledonia">New Caledonia</option> <option value="New Zealand">New Zealand</option> <option value=" Nicaragua"> Nicaragua</option> <option value="Niger">Niger</option> <option value="Nigeria">Nigeria</option> <option value="Norfolk Island">Norfolk Island</option> <option value="North Korea">North Korea</option> <option value="Norway">Norway</option> <option value="Oman">Oman</option> <option value="Pakistan">Pakistan</option> <option value="Palau">Palau</option> <option value="Palestinian Territory">Palestinian Territory</option> <option value="Panama">Panama</option> <option value=" Papua New Guinea"> Papua New Guinea</option> <option value="Paraguay">Paraguay</option> <option value="Peru">Peru</option> <option value="Philippines">Philippines</option> <option value="Pitcairn">Pitcairn</option> <option value="Poland">Poland</option> <option value="Portugal">Portugal</option> <option value="Puerto Rico">Puerto Rico</option> <option value="Qatar">Qatar</option> <option value="Romania">Romania</option> <option value="Russian Federation">Russian Federation</option> <option value="Rwanda ">Rwanda </option> <option value="Saint Helena">Saint Helena</option> <option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option> <option value="Saint Lucia">Saint Lucia</option> <option value="Saint Pierre and Miquelon">Saint Pierre and Miquelon</option> <option value="Saint Vincent and the Grenadines">Saint Vincent and the Grenadines</option> <option value="Samoa ">Samoa </option> <option value="San Marino">San Marino</option> <option value="Sao Tome and Principe">Sao Tome and Principe</option> <option value="Saudi Arabia">Saudi Arabia</option> <option value="Senegal">Senegal</option> <option value="Serbia">Serbia</option> <option value="Seychelles">Seychelles</option> <option value="Sierra Leone">Sierra Leone</option> <option value="Singapore">Singapore</option> <option value="Slovakia">Slovakia</option> <option value="Slovenia ">Slovenia </option> <option value="Solomon Islands">Solomon Islands</option> <option value="Somalia">Somalia</option> <option value="South Africa">South Africa</option> <option value="South Georgia">South Georgia</option> <option value="South Korea">South Korea</option> <option value="Spain">Spain</option> <option value="Sri Lanka">Sri Lanka</option> <option value="Sudan">Sudan</option> <option value="Suriname">Suriname</option> <option value="Svalbard and Jan Mayen">Svalbard and Jan Mayen</option> <option value=" Swaziland"> Swaziland</option> <option value="Sweden">Sweden</option> <option value="Switzerland">Switzerland</option> <option value="Syrian Arab Republic">Syrian Arab Republic</option> <option value="Taiwan">Taiwan</option> <option value="Tajikistan">Tajikistan</option> <option value="Tanzania">Tanzania</option> <option value="Thailand">Thailand</option> <option value="The Former Yugoslav Republic of Macedonia ">The Former Yugoslav Republic of Macedonia </option> <option value="Timor-Leste">Timor-Leste</option> <option value="Togo">Togo</option> <option value="Tokelau">Tokelau</option> <option value="Tonga">Tonga</option> <option value="Trinidad and Tobago">Trinidad and Tobago</option> <option value="Tunisia">Tunisia</option> <option value="Turkey">Turkey</option> <option value="Turkmenistan">Turkmenistan</option> <option value="Tuvalu">Tuvalu</option> <option value="Uganda">Uganda</option> <option value="Ukraine">Ukraine</option> <option value="United Arab Emirates ">United Arab Emirates </option> <option selected="selected" value="United Kingdom">United Kingdom</option> <option value="United States">United States</option> <option value="United States Minor Outlying Islands">United States Minor Outlying Islands</option> <option value="Uruguay">Uruguay</option> <option value="Uzbekistan">Uzbekistan</option> <option value="Vanuatu">Vanuatu</option> <option value="Vatican City">Vatican City</option> <option value="Venezuela">Venezuela</option> <option value=" Vietnam"> Vietnam</option> <option value="Virgin Islands, British">Virgin Islands, British</option> <option value="Virgin Islands, U.S.">Virgin Islands, U.S.</option> <option value="Wallis and Futuna">Wallis and Futuna</option> <option value="Western Sahara">Western Sahara</option> <option value="Yemen">Yemen</option> <option value="Zambia">Zambia</option> <option value="Zimbabwe">Zimbabwe</option> </select></td> </tr> </tbody> </table> <p><strong>Friend Details</strong></p> <table style="width: 100%;" border="0" cellspacing="6px" cellpadding="0" align="center"> <tbody> <tr> <td width="1%"><span style="color: #ff0000;">*</span></td> <td width="39%">Email:</td> <td width="60%"><label> <input id="email2" type="text" name="email2" value="" /> </label></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Title:</td> <td><label> <select id="Title" name="Title"> <option value="">[Select Title]</option> <option value="Mr.">Mr.</option> <option value="Mrs.">Mrs.</option> <option value="Miss.">Miss.</option> <option value="Ms.">Ms.</option> <option value="Dr.">Dr.</option> </select> </label></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>First Name:</td> <td><input id="FirstName" type="text" name="FirstName" value="" /></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Last Name:</td> <td><input id="LastName" type="text" name="LastName" value="" /></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Country:</td> <td><select id="Country" name="Country"> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option> <option value="American Samoa">American Samoa</option> <option value="Andorra">Andorra</option> <option value="Angola">Angola</option> <option value="Anguilla">Anguilla</option> <option value="Antarctica">Antarctica</option> <option value="Antigua and Barbuda">Antigua and Barbuda</option> <option value="Argentina">Argentina</option> <option value="Armenia ">Armenia </option> <option value="Aruba">Aruba</option> <option value="Australia">Australia</option> <option value="Austria">Austria</option> <option value="Azerbaijan">Azerbaijan</option> <option value="Bahamas">Bahamas</option> <option value="Bahrain">Bahrain</option> <option value="Bangladesh">Bangladesh</option> <option value="Barbados">Barbados</option> <option value="Belarus">Belarus</option> <option value="Belgium">Belgium</option> <option value="Belize">Belize</option> <option value="Benin">Benin</option> <option value="Bermuda ">Bermuda </option> <option value="Bhutan">Bhutan</option> <option value="Bolivia">Bolivia</option> <option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option> <option value="Botswana">Botswana</option> <option value="Bouvet Island">Bouvet Island</option> <option value="Brazil">Brazil</option> <option value="British Indian Ocean Territory">British Indian Ocean Territory</option> <option value="Brunei Darussalam ">Brunei Darussalam </option> <option value="Bulgaria">Bulgaria</option> <option value="Burkina Faso">Burkina Faso</option> <option value="Burundi">Burundi</option> <option value="Cambodia">Cambodia</option> <option value="Cameroon">Cameroon</option> <option value="Canada">Canada</option> <option value="Cape Verde">Cape Verde</option> <option value="Cayman Islands">Cayman Islands</option> <option value="Central African Republic ">Central African Republic </option> <option value="Chad">Chad</option> <option value="Chile">Chile</option> <option value="China">China</option> <option value="Christmas Island">Christmas Island</option> <option value="Cocos Islands">Cocos Islands</option> <option value="Colombia">Colombia</option> <option value="Comoros">Comoros</option> <option value="Congo">Congo</option> <option value="Congo, Democratic Republic of the">Congo, Democratic Republic of the</option> <option value="Cook Islands ">Cook Islands </option> <option value="Costa Rica">Costa Rica</option> <option value="Cote d">Cote d'Ivoire</option> <option value="Croatia">Croatia</option> <option value="Cuba">Cuba</option> <option value="Cyprus">Cyprus</option> <option value="Czech Republic">Czech Republic</option> <option value="Denmark">Denmark</option> <option value="Djibouti">Djibouti</option> <option value="Dominica">Dominica</option> <option value="Dominican Republic">Dominican Republic</option> <option value="Ecuador ">Ecuador </option> <option value="Egypt">Egypt</option> <option value="El Salvador">El Salvador</option> <option value="Equatorial Guinea">Equatorial Guinea</option> <option value="Eritrea">Eritrea</option> <option value="Estonia">Estonia</option> <option value="Ethiopia">Ethiopia</option> <option value="Falkland Islands">Falkland Islands</option> <option value="Faroe Islands">Faroe Islands</option> <option value="Fiji">Fiji</option> <option value="Finland">Finland</option> <option value="France ">France </option> <option value="French Guiana">French Guiana</option> <option value="French Polynesia">French Polynesia</option> <option value="Gabon">Gabon</option> <option value="Gambia">Gambia</option> <option value="Georgia">Georgia</option> <option value="Germany">Germany</option> <option value="Ghana">Ghana</option> <option value="Gibraltar">Gibraltar</option> <option value="Greece">Greece</option> <option value="Greenland">Greenland</option> <option value="Grenada">Grenada</option> <option value="Guadeloupe ">Guadeloupe </option> <option value="Guam">Guam</option> <option value="Guatemala">Guatemala</option> <option value="Guinea">Guinea</option> <option value="Guinea-Bissau">Guinea-Bissau</option> <option value="Guyana">Guyana</option> <option value="Haiti">Haiti</option> <option value="Heard Island and McDonald Islands">Heard Island and McDonald Islands</option> <option value="Honduras">Honduras</option> <option value="Hong Kong">Hong Kong</option> <option value="Hungary ">Hungary </option> <option value="Iceland">Iceland</option> <option value="India">India</option> <option value="Indonesia">Indonesia</option> <option value="Iran">Iran</option> <option value="Iraq">Iraq</option> <option value="Ireland">Ireland</option> <option value="Israel">Israel</option> <option value="Italy">Italy</option> <option value="Jamaica">Jamaica</option> <option value="Japan">Japan</option> <option value="Jordan">Jordan</option> <option value="Kazakhstan">Kazakhstan</option> <option value="Kenya">Kenya</option> <option value="Kiribati">Kiribati</option> <option value="Kuwait">Kuwait</option> <option value="Kyrgyzstan ">Kyrgyzstan </option> <option value="Laos">Laos</option> <option value="Latvia">Latvia</option> <option value="Lebanon">Lebanon</option> <option value="Lesotho">Lesotho</option> <option value="Liberia">Liberia</option> <option value="Libya">Libya</option> <option value="Liechtenstein">Liechtenstein</option> <option value="Lithuania">Lithuania</option> <option value="Luxembourg">Luxembourg</option> <option value="Macao">Macao</option> <option value="Madagascar">Madagascar</option> <option value="Malawi">Malawi</option> <option value="Malaysia">Malaysia</option> <option value="Maldives ">Maldives </option> <option value="Mali">Mali</option> <option value="Malta">Malta</option> <option value="Marshall Islands">Marshall Islands</option> <option value="Martinique">Martinique</option> <option value="Mauritania">Mauritania</option> <option value="Mauritius">Mauritius</option> <option value="Mayotte">Mayotte</option> <option value="Mexico">Mexico</option> <option value="Micronesia">Micronesia</option> <option value="Moldova">Moldova</option> <option value="Monaco">Monaco</option> <option value="Mongolia">Mongolia</option> <option value="Montenegro ">Montenegro </option> <option value="Montserrat">Montserrat</option> <option value="Morocco">Morocco</option> <option value="Mozambique">Mozambique</option> <option value="Myanmar">Myanmar</option> <option value="Namibia">Namibia</option> <option value="Nauru">Nauru</option> <option value="Nepal">Nepal</option> <option value="Netherlands">Netherlands</option> <option value="Netherlands Antilles">Netherlands Antilles</option> <option value="New Caledonia">New Caledonia</option> <option value="New Zealand">New Zealand</option> <option value=" Nicaragua"> Nicaragua</option> <option value="Niger">Niger</option> <option value="Nigeria">Nigeria</option> <option value="Norfolk Island">Norfolk Island</option> <option value="North Korea">North Korea</option> <option value="Norway">Norway</option> <option value="Oman">Oman</option> <option value="Pakistan">Pakistan</option> <option value="Palau">Palau</option> <option value="Palestinian Territory">Palestinian Territory</option> <option value="Panama">Panama</option> <option value=" Papua New Guinea"> Papua New Guinea</option> <option value="Paraguay">Paraguay</option> <option value="Peru">Peru</option> <option value="Philippines">Philippines</option> <option value="Pitcairn">Pitcairn</option> <option value="Poland">Poland</option> <option value="Portugal">Portugal</option> <option value="Puerto Rico">Puerto Rico</option> <option value="Qatar">Qatar</option> <option value="Romania">Romania</option> <option value="Russian Federation">Russian Federation</option> <option value="Rwanda ">Rwanda </option> <option value="Saint Helena">Saint Helena</option> <option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option> <option value="Saint Lucia">Saint Lucia</option> <option value="Saint Pierre and Miquelon">Saint Pierre and Miquelon</option> <option value="Saint Vincent and the Grenadines">Saint Vincent and the Grenadines</option> <option value="Samoa ">Samoa </option> <option value="San Marino">San Marino</option> <option value="Sao Tome and Principe">Sao Tome and Principe</option> <option value="Saudi Arabia">Saudi Arabia</option> <option value="Senegal">Senegal</option> <option value="Serbia">Serbia</option> <option value="Seychelles">Seychelles</option> <option value="Sierra Leone">Sierra Leone</option> <option value="Singapore">Singapore</option> <option value="Slovakia">Slovakia</option> <option value="Slovenia ">Slovenia </option> <option value="Solomon Islands">Solomon Islands</option> <option value="Somalia">Somalia</option> <option value="South Africa">South Africa</option> <option value="South Georgia">South Georgia</option> <option value="South Korea">South Korea</option> <option value="Spain">Spain</option> <option value="Sri Lanka">Sri Lanka</option> <option value="Sudan">Sudan</option> <option value="Suriname">Suriname</option> <option value="Svalbard and Jan Mayen">Svalbard and Jan Mayen</option> <option value=" Swaziland"> Swaziland</option> <option value="Sweden">Sweden</option> <option value="Switzerland">Switzerland</option> <option value="Syrian Arab Republic">Syrian Arab Republic</option> <option value="Taiwan">Taiwan</option> <option value="Tajikistan">Tajikistan</option> <option value="Tanzania">Tanzania</option> <option value="Thailand">Thailand</option> <option value="The Former Yugoslav Republic of Macedonia ">The Former Yugoslav Republic of Macedonia </option> <option value="Timor-Leste">Timor-Leste</option> <option value="Togo">Togo</option> <option value="Tokelau">Tokelau</option> <option value="Tonga">Tonga</option> <option value="Trinidad and Tobago">Trinidad and Tobago</option> <option value="Tunisia">Tunisia</option> <option value="Turkey">Turkey</option> <option value="Turkmenistan">Turkmenistan</option> <option value="Tuvalu">Tuvalu</option> <option value="Uganda">Uganda</option> <option value="Ukraine">Ukraine</option> <option value="United Arab Emirates ">United Arab Emirates </option> <option selected="selected" value="United Kingdom">United Kingdom</option> <option value="United States">United States</option> <option value="United States Minor Outlying Islands">United States Minor Outlying Islands</option> <option value="Uruguay">Uruguay</option> <option value="Uzbekistan">Uzbekistan</option> <option value="Vanuatu">Vanuatu</option> <option value="Vatican City">Vatican City</option> <option value="Venezuela">Venezuela</option> <option value=" Vietnam"> Vietnam</option> <option value="Virgin Islands, British">Virgin Islands, British</option> <option value="Virgin Islands, U.S.">Virgin Islands, U.S.</option> <option value="Wallis and Futuna">Wallis and Futuna</option> <option value="Western Sahara">Western Sahara</option> <option value="Yemen">Yemen</option> <option value="Zambia">Zambia</option> <option value="Zimbabwe">Zimbabwe</option> </select></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Telephone (Home):</td> <td><input id="telhome" type="text" name="telhome" value="" /></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Telephone (Work):</td> <td><input id="telwork" type="text" name="telwork" value="" /></td> </tr> <tr> <td><span style="color: #ff0000;">*</span></td> <td>Telephone (Mobile):</td> <td><input id="telmobile" type="text" name="telmobile" value="" /></td> </tr> <tr> <td> </td> </tr> <tr> <td> </td> <td> </td> <td><label> <input id="button" type="submit" name="button" value="Submit" /> </label></td> </tr> </tbody> </table> </form> <?php /* Set e-mail recipient */ $myemail = "myemail@gmail.com"; /* Check all form inputs using check_input function */ $clentEmail = check_input($_POST['clentEmail']); $yourname = check_input($_POST['clentFname'], "Enter your name"); $clentLname = check_input($_POST['clentLname'], "Enter your last name"); $lastname = check_input($_POST['lastname'], "Write your last name"); $Country2 = check_input($_POST['Country2'], "Your Country"); $email2 = check_input($_POST['email2'], "Enter your email"); $Title = check_input($_POST['Title']); $FirstName = check_input($_POST['FirstName'], "Enter your name222"); $LastName = check_input($_POST['LastName'], "Enter your last name"); $Country = check_input($_POST['Country'], "Your Country"); $telhome = check_input($_POST['telhome'], "Enter your home nomber"); $telwork = check_input($_POST['telwork'], "Enter your work nomber"); $telmobile = check_input($_POST['telmobile'], "Enter your mobile nomber"); /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } /* If URL is not valid set $website to empty */ if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) { $website = ''; } /* Let's prepare the message for the e-mail */ $message = "Hello! Your contact form has been submitted by: Client Name: $clentFname Client Last Name: $clentLname Client E-mail: $email Client Country: $Country2 Friend Detail: E-mail: $email2 Title: $Title First Name: $FirstName Last Name: $LastName Country: $Country Home Nomber: $telhome Work Nomber: $telwork Mobile Nomber: $telmobile End of message "; /* Send the message using mail() function */ mail($myemail, $subject, $message); /* Redirect visitor to the thank you page */ header('Location: '); exit(); /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?> Hello.. I have two php form and one has categories and their subjects. In there, users can select category and relevant subjects to that category. Likewise users can select upto 3 categories and their subjects in first form. when those select and after submitting those should handle in my second page. But I have made so frustrated here when Im going to handle those in my second page. Please Can anybody help me to do this. I use this, but...... Code: [Select] if ( ( isset($_POST['submitted2'])) && ( isset($_SESSION['category'])) && (isset( $_SESSION['subjects'])) && sizeof( $_SESSION['category']) == 1) { //$_SESSION['category'] = $_POST['category']; print_r ( $_SESSION['category']); echo 'good'; } elseif ( ( isset($_POST['submitted2'])) && ( isset($_SESSION['category'])) && (isset( $_POST['main_category'] ))) { print_r ( $_SESSION ); echo 'very good'; } Thanks in advance. Hi, I'm quite a newbie to PHP and am doing a website. Have got a login working and the registration half way there, but am now in process of putting validation in Registration form. Have got the errors appearing if domething isn't filled in or if the username is taken however, it clears all the fields and i would like it to keep the values in them if possible. Is there an easy way round this? I can give my code if needed Cozzy Been screwing around on Google for about 3 hours trying to find a tutorial on what I am trying to do with absolutely no luck! I am simply trying to get my test script to echo errors from an array when a form criteria does not validate. This is my final revision which is still not working! Can someone please tell me what I am doing wrong? No matter what I do, I can't get away from this error: Notice: Undefined variable: error in C:\wamp\www\php\form_validation.php on line 13 <?php $o = $error[]; // test echo $o; // test if(!preg_match('/[^0-9A-Za-z]/',$_POST['first_name'])) { $error[] = "Please enter a valid First Name"; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> First Name:<br /> <input name="first_name" type="text" size="50" maxlength="50" /><br /><br /> <input type="submit" /><br /><br /> </form> I am trying to use the new way of validating the entered email in a register form. Code: [Select] /* REGISTER FORM */ // check if submit button has been clicked if (isset($_POST['submit_signup'])) { // process and assign variables after post submit button has been clicked $user_email = strip_tags(trim($_POST['email'])); $user_email = filter_var($user_email, FILTER_VALIDATE_EMAIL); $nickname = strip_tags(trim($_POST['nickname'])); $password = $_POST['password']; $repassword = $_POST['repassword']; $month = $_REQUEST['month']; $day = $_REQUEST['day']; $year = $_REQUEST['year']; $dob = $year . "-" . $month . "-" . $day; $find_us_question = strip_tags(trim($_POST['find_us_question'])); // connect to database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $check_query = "SELECT * FROM user WHERE nickname = '$nickname'"; $check_connect = mysqli_query($dbc, $check_query) or die(mysqli_error($dbc)); $check_count = mysqli_num_rows($check_connect); // Check if the email exists twice $query_get = "SELECT email FROM user WHERE email = '$user_email'"; $query_run = mysqli_query($dbc, $query_get); $num_rows = mysqli_num_rows($query_run); // check if username is already taken if ($check_count != 0) { echo "Username already exists!"; } elseif ($num_rows != 0) { echo "This email address is already registered in the database, you can not register it twice."; // check if fields are empty } elseif (empty($user_email) || empty($nickname) || empty($password) || empty($day) || empty($month) || empty($year)) { echo "Please fill out all the fields!"; // check char length of input data } elseif (strlen($nickname) > 30 || strlen($user_email) > 50) { echo "Maximum allowed character length for nickname/firstname/lastname are 30 characters!"; // check password char length } elseif (strlen($password) > 25 || strlen($password) < 6) { echo "Your password must be between 6 and 25 characters!"; // check if passwords match with each other } elseif ($password != $repassword) { echo "Please make sure your passwords are matching!"; } else { // encrypt password $password = sha1($password); I would like to implement now an error message stating something along the lines that the entered email address is not valid, how would I have to do the if statement to check the condition? Hi, I am creating a cms using php and my sql for my website. I have managed to create the majority of it but am having trouble added a menu on each page. It appears on the homepage with the following code: <ul id="headlines"> <?php foreach ( $results['articles'] as $article ) { ?> <li> <h2> <a href=".?action=viewArticle&articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a> </h2> </li> <?php } ?> </ul> Once I click on a selection from the menu it takes me to the correct article but there is no menu now. If I try to copy and paste the code from homepage into my viewArticle file it gives me the error messages 'undefined index articles on line 6' and 'invalid argument supplied for for each'. This is the viewArticle code after I pasted in the code from homepage: <ul id="headlines"> <?php foreach ( $results['articles'] as $article ) { ?> <li> <h2> <a href=".?action=viewArticle&articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a> </h2> </li> <?php } ?> </ul> <h1 style="width: 75%;"><?php echo htmlspecialchars( $results['article']->title )?></h1> <div style="width: 75%;"><?php echo $results['article']->content?></div> I would really appreciate anybody helping me figure out how I can add the menu onto the viewArticle page. Thanks in advance hello , i have this code: <?php function error_table($err){ echo"<p><span class='textborder'>$err</span></p>"; } ?> in a separate php file and then call it from another php file , like this : $msg_err='bla bla'; error_table($msg_err); when i click the button it shows the text with its border(the class has a border ,color and 1px) but when i have the code inside other if functions it shows it 2 or 3 times... I have to get the error messages to appear...but for some reason they're not showing up if I leave the forms blank and click submit. Code: [Select] <? $PROVINCES = array( "--" => "--- Please Select Provinces ---", "nf"=>"Newfoundland", "pe"=>"Prince Edward Island", "nb"=>"New Brunswick", "ns"=>"Nova Scotia", "qc"=>"Quebec", "on"=>"Ontario", "mb"=>"Manitoba", "sk"=>"Saskatchewan", "ab"=>"Alberta", "bc"=>"British Columbia", "nt"=>"Northwest Territories"); if ($_SERVER['REQUEST_METHOD'] == 'GET') { $dimwelcomeMsg = TRUE; } if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_POST['uname'] == ''){ $errMsg['uname'] = "Username must not be blank!!!"; $unameInError = true; } if (empty($_POST['year']) == TRUE){ $errMsg['year'] = "Year must not be blank!!"; } if (count($errMsg) > 0 ): $dispErrorMsg = TRUE; else: foreach ($_POST as $key => $val): $key = strtoupper($key); $successMsg[$key] = $val; endforeach; $dispSuccessMsg = TRUE; endif; } ?> <html > <head> <title>Comp10065 - Simple Form Validatioon</title> <style type="text/css"> body { margin: 0; padding: 0; font: 80%/1.5 Arial,Helvetica,sans-serif; color: #111; background-color: green; } h2 { margin: 0px; padding: 10px; font-family: Georgia, "Times New Roman", Times, serif; font-size: 200%; font-weight: normal; color: #FFF; background-color: #CCC; border-bottom: #BBB 2px solid; } h1 {color:#006600} p#copyright { margin: 20px 10px; font-size: 90%; color: #999; } div.form-container { margin: 10px; padding: 5px; background-color: #FFF; border: #EEE 1px solid; } p.legend { margin-bottom: 1em; } p.legend em { color: #C00; font-style: normal; } div.errors { margin: 0 0 10px 0; padding: 5px 10px; border: #FC6 1px solid; background-color: #FFC; } div.errors p { margin: 0; } div.errors p em { color: #C00; font-style: normal; font-weight: bold; } div.success { margin: 0 0 10px 0; padding: 5px 10px; border: #FC6 1px solid; background-color: #FFC; } div.success p { margin: 0; color:#006633} div.success li {list-style-type: none; } div.form-container form p { margin: 0; } div.form-container form p.note { margin-left: 170px; font-size: 90%; color: #333; } div.form-container form fieldset { margin: 10px 0; padding: 10px; border: #DDD 1px solid; } div.form-container form legend { font-weight: bold; color: #666; } div.form-container form fieldset div { padding: 0.25em 0; } div.form-container label, div.form-container span.label { margin-right: 10px; padding-right: 10px; width: 150px; display: block; float: left; text-align: right; position: relative; } div.form-container label.error, div.form-container span.error { color: #000; } div.form-container select.error, div.form-container div.error {background-color: #FEF;} div.form-container label em, div.form-container span.label em { position: absolute; right: 0; font-size: 120%; font-style: normal; color: #C00; } div.form-container input.error { border-color: #C00; background-color: #FEF; } div.form-container input:focus, div.form-container input.error:focus, div.form-container textarea:focus { background-color: #FFC; border-color: #FC6; } div.form-container div.controlset {margin: 3px} div.form-container div.controlset label, div.form-container div.controlset input { display: inline; float: none; } div.form-container div.controlset div { margin-left: 170px; } div.form-container div.buttonrow { padding-left: 430px; border: 1px solid #CCCCCC } div#wrapper {width: 700px ; margin-left: auto; margin-right: auto} pre {color:black; font-size: 10pt; font-weight: bold; margin-left: 30px} </style> </head> <body> <div id="wrapper"> <h1> Form Validation Lab</h1> <div class="form-container"> <? if ($_SERVER['REQUEST_METHOD'] == 'GET') : ?> <? if (displayWelcomeMsg) : ?> <p>Please fill in the form........</p> <? endif; ?> <? endif; ?> <? if ($displaySuccessMsg): ?> <div class="success"> <p>Thank you for your submission.</p> <ol> <? foreach($successMsgs as $key => $successMsg): ?> <li><?= $key ?> = <?= $successMsg ?> </li> <? endforeach; ?> </ol> </div> <? endif; ?> <? if ($dispErrorMsg) : ?> <div class="errors"> <p><em>Oops...the following errors were encountered</em></p> <ul> <? foreach ($errMsgs as $key => $errMsg): ?> <li><?= $errMsg ?> </li> <? endforeach; ?> </ul> <p>Please correct the errors and re-submit this form.</p> </div> <? endif; ?> <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post"> <input type="hidden" name="act" value="post"> <div class="buttonrow"> <input type="submit" value="Submit This Form " class="button" /> <a href="<?= $_SERVER['PHP_SELF'] ?>" >Start Again</a> </div> <p class="legend"><strong>Note:</strong> Required fields are marked with an asterisk (<em>*</em>)</p> <fieldset> <legend>User Details</legend> <div> <label for="uname" >User Name <em>*</em></label> <? if (isset($errMsgs['uname'])) echo 'class = "errors"' ?> <input id="uname" type="text" name="uname" value="<?=$_POST['uname'] ?>" /> (must be greater than 5 chars)</div> <div> <label for="email">Email Address </label> <input id="email" type="text" name="email" value="" /> </div> </fieldset> <fieldset> <legend>Submission</legend> <div> <label for="year">Year (YYYY) <em>*</em></label> <input id="year" type="text" name="year" <? if (isset($errMsgs['year'])) echo 'class = "errors"' ?> value="" size="4" maxlength="4" /> (4 digit number)</div> <div> <label for="date">Month (MM)</label> <input id="date" type="text" name="month" value="" size="4" maxlength="2" /> (number ranging from 1-12)</div> </fieldset> <fieldset> <legend>Preferences</legend> <div> <label for="type" >Province (Multiple Select) <em>*</em></label> <select name = "province[]" multiple size = "12" id="type" > <? foreach ($PROVINCES as $abbreviation => $longname); ?> <option value = "<?=$abbreviation ?> "></option> <? enfgoreach; ?> </select> </div> <div class= 'controlset' > <span class="label">Status (Mult Select)<em>*</em></span> <input name="status[]" id="approved" value="approved" type="checkbox" /> <label for="approved">Approved</label> <input name="status[]" id="pending" value="pending" type="checkbox" /> <label for="pending">Pending Application</label> <input name="status[]" id="actives" value="actives" type="checkbox" /> <label for="actives">Active Service</label> </div> <div class= 'controlset'> <span class="label"> Location <em>*</em></span> <input name="location" id="radio1" value="Garage" type="radio" /> <label for="radio1">Garage</label> <input name="location" id="radio2" value="Attic" type="radio" /> <label for="radio2">Attic</label> <input name="location" id="radio3" value="House" type="radio" /> <label for="radio3">House</label> </div> </fieldset> </form> <p>This debug info is here to help you. You are only required to display the POST array</p> <pre><?= print_r($_POST,true) ?> </pre> </div> </div> </body> </html> I wanna give error messages if the user dont enter their name, email or comments. I trıed to do it but mine didnt work. Please help me do it. Index.php page Code: [Select] <html> <head> <title>Guestbook</title> </head> <body> <font color="#CC0000" size="+2">Please sign our guestbook</font> <br \> <br \> <?php echo "<form name=\"Guestbook\" action=\"confirm.php\" method=\"post\">\n"; echo "<table bgcolor=\"#DAE5CD\">\n"; echo " <tr>\n"; echo " <td valign=\"top\">Name: </td>\n"; echo " <td><input type=\"text\" name=\"name\" size=\"25\" value=\"Your Name\"></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td valign=\"top\">E-mail:</td>\n"; echo " <td><input type=\"text\" name=\"email\" size=\"25\" value=\"example@mail.com\"></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td valign=\"top\">Comments:</td>\n"; echo " <td><textarea rows=\"5\" cols=\"30\" name=\"comments\">Comments</textarea></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td></td>\n"; echo " <td align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Submit\"> <input type=\"reset\" value=\"Clear\"> </td>\n"; echo " </tr>\n"; echo "</table>\n"; echo "</form> "; ?> </body> </html> confirm.php page Code: [Select] <html> <head> <title> Submission receieved! </title> </head> <body> <font size="+2" color="#00B0EB">Your submission has been sent successfully!</font><br /> <br /> <?php //extract($_REQUEST); if (!isset($_POST["$submit"])) { if (empty($_POST["name"])) { echo "Please enter your name!"; } else if (empty($_POST["email"])) { echo "Please enter your E-mail"; } else if (empty($_POST["comments"])) { echo "Please enter some text"; } } ?> <table bgcolor="#DAE5CD" width="%50" cellpadding="5"> <tr> <td valign="top" width="75">Name : </td> <td> <?php echo $_POST["name"];?> </td> </tr> <tr> <td valign="top">E-Mail : </td> <td> <?php echo $_POST["email"]; ?> </td> </tr> <tr> <td valign="top">Comments :</td> <td> <?php echo $_POST["comments"]; ?> </td> </tr> </table> </body> </html> I have a process that runs in the background, so I don't get to see its output. What I would like to do, is if a error occurs (mysql or php) send me an email with the error message(s), then end the process. Any way I can do this? Right now, I am not sure if any errors occur, but I am thinking that they might be happening. So any way for me to get the error would be awesome! Hey there I've adapted the search code from the tutorial on this site here http://www.phpfreaks.com/tutorial/simple-sql-search for a project I've been working on but have an error. The search works just fine but if either of the messages I've included are tripped (zero search characters or nothing found) then I get a blank screen and the error message is not echoed. (The vanilla code works fine - the error has arisen through the changes I needed to make). I don't know why this is happening and would appreciate some help please. Thanks in advance for any help. Code: [Select] session_start(); // check session variable if (isset($_SESSION['first_name'])) { include ('includes/header_loggedin.html'); //db connection info deleted $error = array(); //get search term, strip it if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 1) { $error[] = 'Search terms must be longer than 1 character.'; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } //formulate query if (count($error) < 1) { $query = "SELECT * FROM table WHERE row LIKE '%{$searchTermDB}%'"; //make the query $result = mysql_query($query) or die(mysql_error()); //display the results if (mysql_num_rows($result) < 1) { $error[] = "Your search term yielded no results."; } else { while ($endresult = mysql_fetch_array($result)) { echo '<br />'; echo 'This is what we found'; echo '<br />'; echo '<a href="newpage.php?w=' . $endresult['row'] . '">' . $endresult['row'] . '</a>'; } } } } } Hello, I've been working on a search script that call on a function to retrieve info from my database. What I'm trying to do next is to echo various errors e.g. No search results to display. At the moment I have three messages I would like to display based on the user input but it is not working when I try to set up a second functions within the same tags. I have got it working by using header:location but it would be much faster and easier to just echo the messages on one page I will post the code below. Code: [Select] <?php require_once('Connections/price_db.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); //this is the function I would like to add function showErrors($error1 = "", $error2 = "", $error3 = ""){ $var = $_GET['search'] ; $error = false; $error1 = ""; $error2 = ""; $error3 = ""; if (strlen ($var) < 3){ $error = true; $error1 = "You Must Enter At Least Three Characters To Search"; } if (strlen ($var) == 0) { $error = true; $error2 = "Please Enter a Search"; } if ($totalRows_Recordset1 == 0){ $error = true; $error3 = "Your Search Returned No Results"; } if ($error){ showErrors($error1,$error2,error3); } //above is where I have come into troubles switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } }} $colname_Recordset1 = "-1"; if (isset($_GET ['search'])) { $colname_Recordset1 = $_GET['search']; } mysql_select_db($database_price_db, $price_db); $query_Recordset1 = sprintf("SELECT * FROM price_db WHERE tb_name LIKE %s OR tb_desc LIKE %s", GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text")); $Recordset1 = mysql_query($query_Recordset1, $price_db) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> and this is the html for it Code: [Select] <div class="bold_14"> <?php echo $error1 ?> <?php echo $error2 ?> <?php echo $error3 ?> </div> This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=348687.0 One of my tables was hacked by a couple of morons causing me to create a new one The old table was named lner, the new one is named lner3 which has a couple of extra fields Using a playton script to display and manipulate lner3 works just fine but the script used to put detail in gallery pages just DON'T. I basically get a blank page not even the table Remember this is the working script I am still using with my old table until I get the new one working So header and stuff is all fine and it all checks out with CSS and Validation Code: [Select] <body> <div id="head"> <?php // please note $tble = 'lner3'; // Only the above line has changed from //$tble = 'lner'; $brnum = '60112'; include ("../../include/adsense.php"); include_once("../../include/bsgal_conn.php"); ?> </div> <?php $connect = mysqli_connect($host,$account,$password) OR DIE("Error !! Unable to connect to database"); $db = mysqli_select_db($connect,"$dbname") OR DIE( "Unable to select database "); $db="SELECT * FROM $tble WHERE br_no = $brnum OR other_no = $brnum"; if ($result = mysqli_query($connect,$db)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)){ ?> <div id="main"> <table width="410" align="center" border="4" cellspacing="0" cellpadding="1"> <tr> <th>Class</th> <th>C.M.Engineer</th> <th>Arrangment</th> </tr> <tr> <td><?php echo $row['lclas']."/".$row['class2']; ?></td> <td><?php echo $row['cme']; ?></td> <td><?php echo $row['wheel']; ?></td> </tr> </table> </div> </body> </html> So some hints on debugging why it does not work would be most welcome Thanks Roy... Hello, I am hoping to get some advice on how to make my code work. What I would like to do is display a "you are already subscribed" message if the user experiences error 1062 (duplicate entry) and die, or if it's a different error that occurs I would just like it to display the raw error message and die. I've tried it this and way and that but I'm stumped (obviously I'm also pretty new to this). Here is my code: Quote <?php $con = mysql_connect("localhost"); if (!$con) { die ('Could not connect: ' . mysql_error()); } mysql_select_db("subscribe", $con); $sql="INSERT INTO subscribe_init (id, notes) values ('$_POST[id]','$_POST[notes]')"; if (!mysql_errno() == 1062) echo 'You are already subscribed.'; elseif (!mysql_query($sql,$con)) die ('Error: ' . mysql_errno() . mysql_error()); else echo "Thank you for signing up!"; mysql_close($con) ?> Any advice is greatly appreciated. Thank you! This is what I'm getting... Notice: Undefined variable: sitetitle in /home/vehiclep/public_html/functions.php on line 121 Notice: Undefined variable: ADcode in /home/vehiclep/public_html/functions.php on line 121 Notice: Undefined variable: email in /home/vehiclep/public_html/functions.php on line 121 Notice: Undefined variable: url in /home/vehiclep/public_html/functions.php on line 121 Notice: Undefined variable: charge in /home/vehiclep/public_html/functions.php on line 121 Notice: Undefined variable: currency in /home/vehiclep/public_html/functions.php on line 121 Notice: Undefined variable: price in /home/vehiclep/public_html/functions.php on line 121 this is functions.php Code: [Select] function sitesets() { $sql = "select * from settings limit 1"; $rs = mysql_query($sql); $row = @mysql_fetch_array($rs); extract($row); $sitesets = array("title"=>$sitetitle, "ADcode"=>$ADcode, "email" => $email, "url" => $url, "charge" => $charge, "currency" => $currency, "price" => $price); return $sitesets; } Ive tried going into phpMyadmin and creating a row and entering the info manualy but it doesnt work. I don't know much about coding so this may be a dumb question but it has me stumped. I hope somebody can tell me what I'm missing here. Im trying to create a page that collects personal info from users and registers them. I have a page that contains the registration form which i will call page 1. A second page which I will call page 2, processes the form. A simplified version of page 2 looks like this Code: [Select] <?php if (isset($_POST['submitted'])) { $errors = array(); // Connect to the database. require_once ('config.php'); //Check for errors. //Check to ensure that the password is long enough and is of the right format. if (eregi ("^[[:alnum:]]{8,16}$" , $_POST['password'])) { $b = TRUE; } else { $b = FALSE; $errors[] = 'Please enter a password that consists only of letters and numbers between 8 and 16 characters long.'; } //Check to make sure the password matches the confirmed password. if ($_POST['password'] == $_POST['password2']) { $c = TRUE; $password = $_POST['password']; //Encrypt the password. } else { $c = FALSE; $errors[] = 'The password you entered did not match the confirmed password.'; } //Check to make sure they entered their first name and it's of the right format. if (eregi ("^([[:alpha:]]|-|')+$", $_POST['firstname'])) { $d = TRUE; } else { $d = FALSE; $errors[] = 'Please enter a valid first name.'; } //Check to make sure they entered their last name and it's of the right format. if (eregi ("^([[:alpha:]]|-|')+$", $_POST['lastname'])) { $e = TRUE; } else { $e = FALSE; $errors[] = 'Please enter a valid last name.'; } //Insert data into database. if (empty($errors)) { //query the database. for the sake of simplicity, I wont include the code // Show thank you message echo "<h3>Thank You!</h3> You have been registered"; } else { echo '<font color="red">You could not be registered, please contact us about the problem and we will fix it as soon as we can.</font>'; } //Display error messages. } else {// if errors array is not empty header("Location: page1.php?message=$errors");//Trying to pass the errors array into the url for page1 } } ?> So the idea is to create an array that contains all the errors. Upon querying the database, if the user completed the fields as required, a thank you message is printed. If not, he is redirected to the page containing the form (page 1) and the array containing the errors is transferred to page 1. Now I will include the relevant section of page 1 whihc is supposed to print the error messages. Code: [Select] <?php if(isset($_GET['message']) { echo '<h3>Error!</h3> The following error(s) occured:<br />'; foreach ($errors as $msg) { echo "$msg<br/>\n"; } } ?> I get the following error message when I submit the registration forms with deliberate errors: Error! The following error(s) occured: PHP Error Message Warning: Invalid argument supplied for foreach() in /home/a4993450/public_html/register_page_content.php on line 66 Free Web Hosting Can anyone point out why this error occurs? |