belbin09 Posted November 8, 2017 Share Posted November 8, 2017 (edited) Hi, I am very new to PHP. I am trying to program a course registration using a txt file for students and course. The user inputs the name and student number and selects a course from the drop down menu. Currently I have tried to take the name and number submitted and turn it into a variable called $sname and $snumber and then turn that variable into an array so that I can check it against the array of the students name and number. However I keep getting the error that $sname_array is an unidentified variable. Because I am so new at this I am not sure exactly how I go about fixing this. Thank you <!DOCTYPE html> <html> <body> <h1>Course Selection</h1><br> <?php // Course $coursedata = "course.txt"; // text file coursedata $students = "student.txt"; // text file for students and student number if (isset ($_POST['course'])) { $course = ($_POST['course']); $cfile = fopen ($coursedata, 'r+') or die ("File does not exist"); $found = FALSE; while ($line = fgets ($cfile) and ! $found) { $drop = explode ("||", trim ($line)); $found = $course === $drop[0]; } fclose ($coursedata); //close data if ($found) { echo "<p>You have selected $drop[0]</p>\n"; } // end of if ($found) } // end of if (isset) ?> <form action="index.php" method="post"> Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50"> <br><br> Student Number: <input type="text" name "snumber" required="required" maxlength="9"> <br><br> <?php //turning student name into a variable if (isset ($_POST['name'])) { $sname = ($_POST['name']); $sname_array = explode (' ', $sname); } // turning student number into a variable if (isset ($_POST['snumber'])) { $snumber = ($_POST['snumber']); $snumber_array = explode (' ', $snumber); } //form // select course echo "Select a course: <select name = \"pcourse\">\n"; $cfile = fopen ($coursedata, 'r+') or die ("File does not exist or you do not have permission"); while ($line = fgets ($cfile)) { $drop = explode ('||', $line); echo " <option value =\"$drop[0]\">$drop[0] $drop[1] $drop[2]</option>\n"; } fclose ($cfile); echo " </select>\n"; //student file array $sfile = fopen($students, 'r+') or die ("File does not exist"); while ($sline = fgets ($sfile)) { $name = explode (',', $sline); } fclose ($sfile); //checking name entered against student txt if ($sname_array == $name[0] && $snumber_array == $name[1] ) { echo "Correct"; } else { echo "incorrect"; } ?> <br><br> <input type = "submit" value="submit" name= "submit"> </form> </body> </html> The student text file looks like this: Jim Smith, 400424565 Sarah Hillier, 534712479 Jonathan Quinlan, 764134296 Keith Roberts, 123456789 Sarah Hillier, 200343656 Chloe Butler, 678123987 and the course text file looks like this: English||COMP-1004||4 Web Development||COMP-6002||5 Object-Oriented Java||COMP-1006||3 Networking Essentials||COMP-1035||4 Dynamic Websites AMP||COMP-3006||2 The last number represent the number of seats still available course.txt student.txt Edited November 8, 2017 by belbin09 Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 8, 2017 Share Posted November 8, 2017 (edited) Didn't study the code thoroughly, but I would start at this line: if ($sname_array == $name[0] && $snumber_array == $name[1] ) I guess $sname_array and $snumber_array are arrays, but you use them as strings. It should be something like: if ($sname_array[!!!INDEX_KEY_NUMBER!!!] == $name[0] && $snumber_array[!!!INDEX_KEY_NUMBER!!!] == $name[1] ) Also, are you sure PHP displays only unidentified variable error and doesn't display exact line number where error occurs? Edited November 8, 2017 by phpmillion Quote Link to comment Share on other sites More sharing options...
belbin09 Posted November 8, 2017 Author Share Posted November 8, 2017 (edited) It does tell me a line. Its line 91 which is: if ($sname_array == $name[0] && $snumber_array == $name[1]) { which you are saying should be something like this: if($sname_array[0] == $name[0] && $snumber_array[0] == $name[1] ) { Although that gives me the same error Edited November 8, 2017 by belbin09 Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 8, 2017 Share Posted November 8, 2017 Took a closer look at your code. You get this error because you try to access sname_array BEFORE you create it. Take look at your code for yourself: if (isset ($_POST['name'])) { $sname = ($_POST['name']); $sname_array = explode (' ', $sname); } You only create sname_array IF sname variable is submitted, which is a good thing. However: if ($sname_array == $name[0] && $snumber_array == $name[1] ) You try to access sname_array all the time, no matter if form is submitted or not. You should do a similar check here as well. Something like: if (isset($sname_array)) Hope that makes sense? Quote Link to comment Share on other sites More sharing options...
Sepodati Posted November 8, 2017 Share Posted November 8, 2017 You need to learn that when dealing with forms, there are two actions that take place. The first action is to display your form and the second action is to process the form once it's submitted. You're blending these actions in the same file (which is okay), but the form creation and form processing are being handled all within each other and it's confusing. $sname_array is only set when you're processing the form, not before. Because it's only defined in a block of code that's run when a $_POST variable is present. Structure your code so that it's clear when the form is being processed versus when it's being displayed. if(isset($_POST['submit'])) { //process form } else { //display form } Also, I don't think these lines are doing what you think they're doing... while ($sline = fgets ($sfile)) { $name = explode (',', $sline); } Quote Link to comment Share on other sites More sharing options...
belbin09 Posted November 8, 2017 Author Share Posted November 8, 2017 Thanks phpmillion and Sepodati. I am going to look at my code and clean it up so that way I can look at the suggests you have given me Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.