Hi all, I have 2 selection in my code. After select first selection, user need to click Go button then will show the second selection menu which depend on the first selection. But, what problem I faced is when I click Go button nothing display at second selection menu. Is it the sql wrong or what?? Can someone help me?? thanks~ <body> <h2>insert course</h2> <form method="post" action="" name="form"> Institution Name: <?php $sql = "SELECT DISTINCT i_name FROM school ORDER BY i_name"; // execute query $result = mysql_query($sql); if(isset($_POST["InstitutionName"])) $InstitutionName = $_POST["InstitutionName"]; echo"<select name='InstitutionName'>"; while ($row = mysql_fetch_array($result)) { $i_name=$row["i_name"]; echo"<option value='$i_name'"; if (isset($InstitutionName) && $InstitutionName == $i_name) echo " selected> $i_name"; else echo "> $i_name"; } echo "</select>"; ?> <input type="submit" name="submit" value="Go" /> </form> School Name: <?php if(isset($_POST["submit"])){ $submit = $_POST["submit"]; if($submit=="Go") { $InstitutionName=$_POST['InstitutionName']; $sql = "SELECT DISTINCT s_name from school WHERE i_name=$InstituionName ORDER BY s_name"; // execute query $result = mysql_query($sql); if(isset($_POST["SchoolName"])) $SchoolName = $_POST["SchoolName"]; echo"<select name='SchoolName'>"; while ($row = mysql_fetch_array($result)) { $s_name=$row["s_name"]; echo"<option value='$s_name'"; if (isset($SchoolName) && $SchoolName ==$s_name) echo " selected> $s_name"; else echo "> $s_name"; } echo "</select><br>";}} ?> <body>
You're using this variable "$InstituionName" in the SQL, and there's no variable named like that because you have a typo. Fix Instituion, it should be Institution.
From where are you getting SchoolName?, is that an input name?, I don't see it. I mean, in order to be able to POST the value of SchoolName and have it return the other form, you must display the form, and have SchoolName as one of the input's name, so that you can submit it. Here, you're testing to see if SchoolName returns true: if(isset($_POST["SchoolName"])) else it wont display the form. Also, add the opening brackets to (isset($_POST["SchoolName"])) { { , and end the body tag <body> with </body>
But before you display the second form, you must submit it, by clicking a button, that way X value is sent to the PHP program for processing. In your current code, you test SchoolName first and after it returns true, it displays the form, but since you never submitted a form containing the name "SchoolName" in one of the inputs, it takes it as false or not-set (!isset).
Can you help me to modify? I know what you mean but i don't know how to do it. I am just a beginner of PHP. Thanks~ Database: test1 table: course s_id, i_name, s_name, c_name, mode, duration, fee, requirement, description <?php $connection = mysql_connect("localhost", "root", "") or die("Could not connect to MySQL " .mysql_error() ); $selection = mysql_select_db("test1") or die("Unable to select database. " .mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Insert Course</title> <script language="javascript"> var counter = 0; function moreFields(){ counter++; var newFields = document.getElementById('readroot').cloneNode(true); newFields.id = ''; newFields.style.display = 'block'; var newField = newFields.childNodes; for (var i=0;i<newField.length;i++) { var theName = newField.name if (theName) newField.name = theName + counter; } var insertHere = document.getElementById('writeroot'); insertHere.parentNode.insertBefore(newFields,insertHere); } window.onload = moreFields; </script> </head> <body> <h2>insert course</h2> <form method="post" action="" name="form"> Institution Name: <?php $sql = "SELECT DISTINCT i_name FROM school ORDER BY i_name"; // execute query $result = mysql_query($sql); if(isset($_POST["InstitutionName"])) $InstitutionName = $_POST["InstitutionName"]; echo"<select name='InstitutionName'>"; while ($row = mysql_fetch_array($result)) { $i_name=$row["i_name"]; echo"<option value='$i_name'"; if (isset($InstitutionName) && $InstitutionName == $i_name) echo " selected> $i_name"; else echo "> $i_name"; } echo "</select>"; ?> <input type="submit" name="submit" value="Go" /> </form> School Name: <?php if(isset($_POST["submit"])){ $submit = $_POST["submit"]; if($submit=="Go") { $InstitutionName=$_POST['InstitutionName']; $sql = "SELECT DISTINCT s_name from school WHERE i_name=$InstitutionName ORDER BY s_name"; // execute query $result = mysql_query($sql); if(isset($_POST["SchoolName"])) $SchoolName = $_POST["SchoolName"]; echo"<select name='SchoolName'>"; while ($row = mysql_fetch_array($result)) { $s_name=$row["s_name"]; echo"<option value='$s_name'"; if (isset($SchoolName) && $SchoolName ==$s_name) echo " selected> $s_name"; else echo "> $s_name"; } echo "</select><br>";}} ?> <div id="readroot" style="display: none"> <br /><input type="button" value="Remove" onClick="this.parentNode.parentNode.removeChild(this.parentNode);" /> <table> <tr valign="baseline"> <td nowrap="nowrap" align="left">Course Name</td> <td>:</td> <td> <input type="text" name="CourseName" size="50" /> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Mode of Study</td> <td>:</td> <td> <input type="text" name="ModeOfStudy" size="50" /> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Duration</td> <td>:</td> <td> <input type="text" name="Duration" size="50" /> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Total Fees (Local)</td> <td>:</td> <td> RM <input type="text" name="TotalFees" size="45" /> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Entry Requirement</td> <td>:</td> <td> <input type="text" name="EntryRequirement" size="50" /> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="left">Description of Course</td> <td>:</td> <td> <input type="text" name="Description" size="50" /> </td> </tr> </table> </div> <form method="post" action="view_insert_course_test.php"> <span id="writeroot"></span> <input type="button" value="Add" onClick="moreFields()"/> <input type="submit" value="Insert Record" /> </form> </form> </body> </html>
Sorry, you had the sql query separate. Replace if(mysql_num_rows($sql) > 0) { with if(mysql_num_rows($result) > 0) {
Sorry for disturb!! Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\xampp\htdocs\innosphere\insert_course.php on line 60
Well, I need to be in the actual server so that I can debug the program, reading only the code only allows me to give assumptions to what the problem might be.