aeroswat Posted November 25, 2009 Share Posted November 25, 2009 I'm having a very troubling issue. Maybe I'm just looking over something but I have looked at the code for several hours now, making small adjustments here and there to try to fix this problem. Basically what I am doing is trying to POST the values of this form into my next php file which then records it to a database and mails it to me. I am validating the information submitted on the form in my php executing file. The problem occurs whenever a person does not completely fill out the form and causes it to send back the error messages. My code refills the textboxes and dropdown boxes with the previously information but when it comes to checkboxes it does not at the moment. I took this functionality out for the time being because I found out that when the form is resubmitted the checkboxes are no longer even POSTing the correct values anymore when checked. I have a little test at the bottom of my form to show me the values that my php exec file is sending back (located in the echo $c $obj part) All future submissions after the person submits the form the first time with errors will result in the checkboxes POSTing no value at all even if they are checked. I can't figure out where the value is getting lost... ###### student-registration.php ###### <html> <head> <title>Registration Form</title> <link href="loginmodule.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="jquery-1.2.1.pack.js"></script> <script type="text/javascript"> function carryOver(def,obj) { var control = document.getElementById(obj); control.value = def; } function disChange() { if(document.getElementById('hidDiagnostic').style.display=='none') { document.getElementById('hidDiagnostic').style.display = "block"; }else { document.getElementById('hidDiagnostic').style.display = "none"; } } function lookup(inputString) { if(inputString.length == 0) { $('#schoolSystems').hide(); } else { $.post("schoolsystemlookup-exec.php", {ss: ""+inputString+""}, function(data){ if(data.length >0) { $('#schoolSystems').show(); $('#schoolSystemsList').html(data); } }); } } // lookup function fill(thisValue) { $('#ss').val(thisValue); setTimeout("$('#schoolSystems').hide();", 200); } </script> </head> <body> <?php session_start(); if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) { echo '<h1>Error! Form will not be processed till mistakes are fixed</h1>'; echo '<ul class="err">'; foreach($_SESSION['ERRMSG_ARR'] as $msg) { echo '<li>',$msg,'</li>'; } echo '</ul>'; unset($_SESSION['ERRMSG_ARR']); } ?> <div id="hStudent" style="width:100%; height:50px; background-color:#EAE9E4; color:#FF0000; font-size:40px;"> <b>Basic Student Information</b> </div> <form action="studentregistration-exec.php" method="post"> <table> <tr> <td>Student First Name:</td> <td><input type="text" name="stuFName" id="stuFName"/></td> </tr> <tr> <td>Student Last Name:</td> <td><input type="text" name="stuLName" id="stuLName"/></td> </tr> <tr> <td>Student Grade:</td> <td><select name="stuGrade" id="stuGrade"> <option value="IP">Infant Program</option> <option value="PS">Pre-School Program</option> <option value="KG">K</option> <option value="01">1</option> <option value="02">2</option> <option value="03">3</option> <option value="04">4</option> <option value="05">5</option> <option value="06">6</option> <option value="07">7</option> <option value="08">8</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="PG">College</option> <option value="V">Vocational</option> <option value="A">Adult Rehabilitation</option> <option value="OR">Other</option> </select></td> </tr> <tr> <td>Date of Birth:</td> <td><select name="bMonth" id="bMonth"> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="bDate" id="bDate"> <?php for($d = 1; $d <= 31; $d++) { if($d < 10) { echo "<option value='0$d'>0$d</option>"; } else{ echo "<option value='$d'>$d</option>";} } ?> </select> <select name="bYear" id="bYear"> <?php for($y = date("Y"); $y >= (date("Y")-110); $y--) { echo "<option value='$y'>$y</option>"; } ?> </select></td> </tr> <tr> <td>Address #1:</td> <td><input type="text" name="stuAddOne" id="stuAddOne"/></td> </tr> <tr> <td>Address #2 (optional):</td> <td><input type="text" name="stuAddTwo" id="stuAddTwo"/></td> </tr> <tr> <td>City/Zip:</td> <td><input type="text" name="stuCity" id="stuCity"/> <input type="number" size=3 maxlength=5 name="stuZip" id="stuZip"/></td> </tr> </table> <div id="hSchool" style="width:100%; height:50px; background-color:#EAE9E4; color:#FF0000; font-size:40px;"> <b>School and Material Information</b> </div> <table> <tr> <td>School System:</td> <td><input type="text" onkeyup="lookup(this.value);" name="ss" id="ss" /> <div class="suggestionsBox" id="schoolSystems" style="display: none;"> <img src="upArrow.png" style="position: relative; top: -12px; left: 50px" alt="upArrow" /> <div class="suggestionList" id="schoolSystemsList"> </div> </div></td> </tr> <tr> <td>Transfer Student: </td> <td><input type="checkbox" value="-1" name="stuTransfer" id="stuTransfer"/></td> </tr> <tr> <td>Primary Reading Material: </td> <td><select name="readMaterial" id="readMaterial"> <option value="LP">Large Print</option> <option value="BR">Braille</option> <option value="AU">Audio</option> <option value="ET">Electronic Text</option> </select></td> </tr> <tr> <td>Individual Education Plan or 504 Plan on file?</td> <td><select name="iep" id="iep"> <option value="-1">Yes</option> <option value="0">No</option> </select></td> </tr> <tr> <td>Date Enrolled in School:</td> <td><select name="enrMonth" id="enrMonth"> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="enrDate" id="enrDate"> <?php for($d = 1; $d <= 31; $d++) { if($d < 10) { echo "<option value='0$d'>0$d</option>"; } else{ echo "<option value='$d'>$d</option>";} } ?> </select> <select name="enrYear" id="enrYear"> <?php for($y = date("Y"); $y >= (date("Y")-25); $y--) { echo "<option value='$y'>$y</option>"; } ?> </select></td> </tr> </table> <div id="hSchool" style="width:100%; height:50px; background-color:#EAE9E4; color:#FF0000; font-size:40px;"> <b>Eligibility</b> </div> <table> <tr> <td>Print Disabilities:</td> </tr> <tr> <td /><td>Visual Disability:</td> <td><input type="checkbox" value="-1" onclick="disChange();" name="disVisual" id="disVisual"/></td> </tr> <tr> <td /><td>Reading Disability:</td> <td><input type="checkbox" value="-1" name="disReading" id="disReading"/></td> </tr> <tr> <td /><td>Physical Disability:</td> <td><input type="checkbox" value="-1" name="disPhysical" id="disPhysical"/></td> </tr> <tbody id="hidDiagnostic" style="display:none;"> <tr> <td>Right Eye:</td> <td>20 / <input type="text" name="eyeRight" id="eyeRight"/></td> </tr> <tr> <td>Left Eye:</td> <td>20 / <input type="text" name="eyeLeft" id="eyeLeft"/></td> </tr> </tbody> </table> <div id="hAuthorize" style="width:100%; height:50px; background-color:#EAE9E4; color:#FF0000; font-size:40px;"> <b>Authorizing Information</b> </div> I certify that the above information is accurate and fully documented: <table> <tr> <td>Name:</td> <td><input type="text" name="authName" id="authName"/></td> </tr> <tr> <td>Position/Title:</td> <td><input type="text" name="authTitle" id="authTitle"/></td> </tr> <tr> <td>Phone Number:</td> <td>(<input type="text" size=1 maxlength=3 name="authArea" id="authArea"/>) <input type="text" size=1 maxlength=3 name="authPhoneOne" id="authPhoneOne"/> - <input type="text" size=2 maxlength=4 name="authPhoneTwo" id="authPhoneTwo"/></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="authEmail" id="authEmail"/></td> </tr> <tr> <td>Address:</td> <td><input type="text" name="authAddress" id="authAddress"/></td> </tr> <tr> <td>City/Zip:</td> <td><input type="text" name="authCity" id="authCity"/> <input type="number" size=3 maxlength=5 name="authZip" id="authZip"/></td> </tr> </table> <input type="submit" value="Submit"/> </form> <?php if( isset($_SESSION['FORM_ARR']) && is_array($_SESSION['FORM_ARR']) && count($_SESSION['FORM_ARR']) >0 ) { $field_arr = array(); $field_arr[0]="stuFName"; $field_arr[1]="stuLName"; $field_arr[2]="stuGrade"; $field_arr[3]="bMonth"; $field_arr[4]="bDate"; $field_arr[5]="bYear"; $field_arr[6]="stuAddOne"; $field_arr[7]="stuAddTwo"; $field_arr[8]="stuCity"; $field_arr[9]="stuZip"; $field_arr[10]="ss"; $field_arr[11]="stuTransfer"; $field_arr[12]="readMaterial"; $field_arr[13]="iep"; $field_arr[14]="enrMonth"; $field_arr[15]="enrDate"; $field_arr[16]="enrYear"; $field_arr[17]="disVisual"; $field_arr[18]="disReading"; $field_arr[19]="disPhysical"; $field_arr[20]="eyeRight"; $field_arr[21]="eyeLeft"; $field_arr[22]="authName"; $field_arr[23]="authTitle"; $field_arr[24]="authArea"; $field_arr[25]="authPhoneOne"; $field_arr[26]="authPhoneTwo"; $field_arr[27]="authEmail"; $field_arr[28]="authAddress"; $field_arr[29]="authCity"; $field_arr[30]="authZip"; $c=0; foreach($_SESSION['FORM_ARR'] as $obj) { echo "<SCRIPT type='text/javascript'>carryOver('" . $obj . "','" . $field_arr[$c] . "');</SCRIPT>"; echo $c . " - " . $obj . "<br>"; $c++; } unset($_SESSION['FORM_ARR']); } ?> </body> </html> ##### studentregistration-exec.php ##### <?php function session_restart() { if (session_name()=='') { // Session not started yet session_start(); } else { // Session was started, so destroy session_destroy(); // But we do want a session started for the next request session_start(); session_regenerate_id(); // PHP < 4.3.3, since it does not put setcookie(session_name(), session_id()); } } //Start session //session_start(); //Array to store validation errors $errmsg_arr = array(); //Array to store previous form entries $form_arr = array(); //Validation error flag $errflag = false; require_once('config.php'); $link = mysql_connect(DB_HOST, DBA_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DBA_DATABASE); if(!$db) { die("Unable to select database"); } //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } $form_arr[0] = $stuFName = clean($_POST['stuFName']); $form_arr[1] = $stuLName = clean($_POST['stuLName']); $form_arr[2] = $stuGrade = clean($_POST['stuGrade']); $form_arr[3] = $bMonth = clean($_POST['bMonth']); $form_arr[4] = $bDate = clean($_POST['bDate']); $form_arr[5] = $bYear = clean($_POST['bYear']); $form_arr[6] = $stuAddOne = clean($_POST['stuAddOne']); $form_arr[7] = $stuAddTwo = clean($_POST['stuAddTwo']); $form_arr[8] = $stuCity = clean($_POST['stuCity']); $form_arr[9] = $stuZip = clean($_POST['stuZip']); $form_arr[10] = $ss = clean($_POST['ss']); $form_arr[11] = $stuTransfer = clean($_POST['stuTransfer']); $form_arr[12] = $readMaterial = clean($_POST['readMaterial']); $form_arr[13] = $iep = clean($_POST['iep']); $form_arr[14] = $enrMonth = clean($_POST['enrMonth']); $form_arr[15] = $enrDate = clean($_POST['enrDate']); $form_arr[16] = $enrYear = clean($_POST['enrYear']); $form_arr[17] = $disVisual = clean($_POST['disVisual']); $form_arr[18] = $disReading = clean($_POST['disReading']); $form_arr[19] = $disPhysical = clean($_POST['disPhysical']); $form_arr[20] = $eyeRight = clean($_POST['eyeRight']); $form_arr[21] = $eyeLeft = clean($_POST['eyeLeft']); $form_arr[22] = $authName = clean($_POST['authName']); $form_arr[23] = $authTitle = clean($_POST['authTitle']); $form_arr[24] = $authArea = clean($_POST['authArea']); $form_arr[25] = $authPhoneOne = clean($_POST['authPhoneOne']); $form_arr[26] = $authPhoneTwo = clean($_POST['authPhoneTwo']); $form_arr[27] = $authEmail = clean($_POST['authEmail']); $form_arr[28] = $authAddress = clean($_POST['authAddress']); $form_arr[29] = $authCity = clean($_POST['authCity']); $form_arr[30] = $authZip = clean($_POST['authZip']); //Input Validations //if($_SERVER['HTTP_REFERER'] != "http://aidb.hostzi.com/student-registration.php") { // $errmsg_arr[] = 'Your ip address of ' . $_SERVER['REMOTE_ADDR'] . ' is being flagged for a hack attempt on a state database.'; // $errflag = true; //} if($stuFName == '') { $errmsg_arr[] = "Student's first name missing"; $errflag = true; } if($stuLName == '') { $errmsg_arr[] = "Student's last name missing"; $errflag = true; } if($stuAddOne == '') { $errmsg_arr[] = "Student's address missing"; $errflag = true; } if($stuCity == '') { $errmsg_arr[] = "Student's city missing"; $errflag = true; } if($stuZip == '') { $errmsg_arr[] = "Student's zip code missing"; $errflag = true; } if( !is_numeric($stuZip) && strlen($stuZip) != 5 ) { $errmsg_arr[] = "Invalid student zip code"; $errflag = true; } if($ss == '') { $errmsg_arr[] = "School system missing or incorrect"; $errflag = true; } if($disVisual == "-1" && ($eyeRight == '' || !is_numeric($eyeRight) || $eyeLeft == '' || !is_numeric($eyeLeft))) { $errmsg_arr[] = "Incorrect eye examination information provided"; $errflag = true; } if($authName == '') { $errmsg_arr[] = "Authorizing name missing"; $errflag = true; } if($authTitle == '') { $errmsg_arr[] = "Authorizing title missing"; $errflag = true; } if($authArea == '' || $authPhoneOne == '' || $authPhoneTwo == '' || !is_numeric($authArea) || !is_numeric($authPhoneOne) || !is_numeric($authPhoneTwo)) { $errmsg_arr[] = "Incorrect or missing authorizing phone number information"; $errflag = true; } if($authEmail == '' || !strstr($authEmail, "@")) { $errmsg_arr[] = "Invalid or missing email address"; $errflag = true; } if($authAddress == '') { $errmsg_arr[] = "Authorizing address missing"; $errflag = true; } if($authCity == '') { $errmsg_arr[] = "Authorizing city missing"; $errflag = true; } if($authZip == '') { $errmsg_arr[] = "Authorizing zip code missing"; $errflag = true; } if( !is_numeric($authZip) && strlen($authZip) != 5 ) { $errmsg_arr[] = "Invalid authorizing zip code"; $errflag = true; } if($form_arr[11] == '') {$form_arr[11]=0;} if($form_arr[17] == '') {$form_arr[17]=0;} if($form_arr[18] == '') {$form_arr[18]=0;} if($form_arr[19] == '') {$form_arr[19]=0;} //If there are input validations, redirect back to the registration form if($errflag) { session_restart(); $_SESSION['ERRMSG_ARR'] = $errmsg_arr; $_SESSION['FORM_ARR'] = $form_arr; unset($form_arr); session_write_close(); header("location: student-registration.php"); exit(); } $bds = $bYear . "-" . $bMonth . "-" . $bDate; $eds = $enrYear . "-" . $enrMonth . "-" . $enrDate; $aps = $authArea . $authPhoneOne . $authPhoneTwo; $date = date('Y-m-d'); if($disVisual != 0) { mysql_query("INSERT INTO tblStudents (StudentName, DateRegistered, GradeLevel, SchoolSystem, authName, isReading, isPhysical, dateBirth, addressOne, addressTwo, stuCity, stuZip, stuTransfer, readMaterial, iep, dateEnrolled, isVisual, authTitle, authPhone, authEmail, authAddress, authCity, authZip, eyeRight, eyeLeft) VALUES ('$stuFName $stuLName','$date', '$stuGrade', '$ss', '$authName', '$disReading', '$disPhysical', '$bds', '$stuAddOne', '$stuAddTwo', '$stuCity', '$stuZip', '$stuTransfer', '$readMaterial', '$iep', '$eds', '$disVisual', '$authTitle', '$aps', '$authEmail', '$authAddress', '$authCity', '$authZip', '$eyeRight', '$eyeLeft')") or die("Unable to add student!"); }else { mysql_query("INSERT INTO tblStudents (StudentName, DateRegistered, GradeLevel, SchoolSystem, authName, isReading, isPhysical, dateBirth, addressOne, addressTwo, stuCity, stuZip, stuTransfer, readMaterial, iep, dateEnrolled, isVisual, authTitle, authPhone, authEmail, authAddress, authCity, authZip) VALUES ('$stuFName $stuLName', '$date', '$stuGrade', '$ss', '$authName', '$disReading', '$disPhysical', '$bds', '$stuAddOne', '$stuAddTwo', '$stuCity', '$stuZip', '$stuTransfer', '$readMaterial', '$iep', '$eds', '$disVisual', '$authTitle', '$aps', '$authEmail', '$authAddress', '$authCity', '$authZip')") or die("Unable to add student!"); } $to = "[email protected]"; $subject = "Registration for " . $stuFName . " " . $stuLName; $header = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $message = 'Student Name: ' . $stuFName . " " . $stuLName . "\r\n" . 'Student Grade: ' . $stuGrade . "\r\n" . 'Date of Birth: ' . $bMonth . "-" . $bDate . "-" . $bYear . "\r\n" . 'Address #1: ' . $stuAddOne . "\r\n"; if($stuAddTwo != '') {$message .= 'Address #2: ' . $stuAddTwo . "\r\n";} $message .='City/State/Zip: ' . $stuCity . ", AL " . $stuZip . "\r\n" . "\r\n" . 'School System: ' . $ss . "\r\n"; if($stuTransfer == '-1') {$message .= 'Transfer Student: ' . Yes . "\r\n";} else{$message .= 'Transfer Student: No' . "\r\n";} $message .='Primary Reading Medium: ' . $readMaterial . "\r\n"; if($iep == '-1') {$message .= 'IEP or 504: Yes' . "\r\n";} else{$message .= 'IEP or 504: No' . "\r\n";} $message .='Date Enrolled: ' . $enrMonth . "-" . $enrDate . "-" . $enrYear . "\r\n" . "\r\n"; if($disVisual =='-1') {$message .= 'Visual Disability: Yes' . "\r\n";} else{$message .= 'Visual Disability: No' . "\r\n";} if($disReading =='-1') {$message .= 'Reading Disability: Yes' . "\r\n";} else{$message .= 'Reading Disability: No' . "\r\n";} if($disPhysical =='-1') {$message .= 'Physical Disability: Yes' . "\r\n";} else{$message .= 'Physical Disability: No' . "\r\n";} if($disVisual =='-1') {$message .= 'Right Eye: 20/' . $eyeRight . "\r\n" . 'Left Eye: 20/' . $eyeLeft . "\r\n";} else{$message .= 'Right Eye: N/A' . "\r\n" . 'Left Eye: N/A' . "\r\n";} $message .="\r\n" . 'Name: ' . $authName . "\r\n" . 'Title: ' . $authTitle . "\r\n" . 'Phone #: (' . $authArea . ")" . $authPhoneOne . "-" . $authPhoneTwo . "\r\n" . 'E-Mail: ' . $authEmail . "\r\n" . 'Address: ' . $authAddress . "\r\n" . 'City/State/Zip: ' . $authCity . ", AL " . $authZip; if(mail($to, $subject, $message, $header)) { echo "Registration Successful!"; } else { echo "Registration Failed!"; } ?> Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/ Share on other sites More sharing options...
mrMarcus Posted November 25, 2009 Share Posted November 25, 2009 personally, i would setup your script differently: have your form with your error_handling embedded within it, ie. <?php if (isset ($_POST['submit'])) { //include error handling only if form has been submitted; include 'error_handling.php'; //include form; include 'form.php'; } else { //include form; include 'form.php'; } ?> this way, you have have to use sessions to manage error handling at all. much more efficient, and less clean-up. and then for a checkbox: <?php echo '<input name="disVisual" value="1" type="checkbox"' . (isset ($_POST['disVisual']) ? ' checked' : '') . ' />'; ?> Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-965628 Share on other sites More sharing options...
aeroswat Posted November 25, 2009 Author Share Posted November 25, 2009 Do you think that the session variables are what are causing the problems? I thought it might have to do with the javascript at the top of the student-registration form page. My function that is for some reason affecting the checkboxes when it sets values to them. The reason I split them into two pages is because I really don't want to make the code messier than it already is even though session variables require the user to have cookies enabled. Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-965633 Share on other sites More sharing options...
mrMarcus Posted November 25, 2009 Share Posted November 25, 2009 keep in mind that for a checkbox to have a 'checked' state, you need to have checked in the <input> field. Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-965658 Share on other sites More sharing options...
aeroswat Posted November 25, 2009 Author Share Posted November 25, 2009 Doesn't having checked in the input field make it default to checked? I'm just trying to get it to return the proper value when it is checked... which is not what it's doing I have the code sitting on the side to set it to checked. Once I get the php to return the correct values then I will enter the checked code. Could my javascript function be screwing up the values of the checkboxes and making it so when you do check it again there is no value to POST to the php exec page? Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-965662 Share on other sites More sharing options...
mrMarcus Posted November 25, 2009 Share Posted November 25, 2009 which value are you trying to return? -1? looking at this line: if($form_arr[11] == '') {$form_arr[11]=0;} //and so on; you are setting your checkboxes to 0 within the array. what values are you trying to retrieve from the checkboxes? you have given them a value of -1 in the <input> field. man, your code is very over-complicated. you can achieve a very efficient form with error_handling script in half the work (no offense). what is the javascript doing with the form? and more importantly, with the checkboxes? Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-965675 Share on other sites More sharing options...
aeroswat Posted November 25, 2009 Author Share Posted November 25, 2009 Sorry that it's so complicated. I'm pretty new to PHP. The javascript set's the value property of each element. It's the first javascript function at the top. I have a feeling that because checkboxes use the checked property to "set their value" and all other form controls use value to display a "default" value it is screwing it up somehow. What do you think? Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-965679 Share on other sites More sharing options...
aeroswat Posted November 30, 2009 Author Share Posted November 30, 2009 Can anyone figure this out? I'm having a lot of trouble with it Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-968133 Share on other sites More sharing options...
aeroswat Posted November 30, 2009 Author Share Posted November 30, 2009 Ok well since no one knows i'm going to rewrite the page without using the php session variables. If anyone stumbles across a solution though I would be much obliged. Link to comment https://forums.phpfreaks.com/topic/182945-session-variable-help/#findComment-968195 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.