tmandava Posted July 3, 2008 Share Posted July 3, 2008 Hello, I just started learning PHP and as part of the learning process I am creating a listing page that displays data from DB and also filters the data depending on users selection in the html select controls. I got the displaying data part working but am not able to figure out how to pass the filter from html select into php function and perform the query. Here is how my code looks: <?php include 'config.php'; include 'opendb.php'; $result = mysql_query("select * from $table"); echo "db queried " . mysql_numrows($result) . " .\n<br>"; ?> <?php function setFilter(){ echo "in func"; $class= $POST['Classes']; echo "class val is" . $class; $result = mysql_query("select * from $table where course_tile = " . $class); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <TITLE>Course Listing</TITLE> <BODY bgcolor = "#FFCC66"></body> <form action = "" method="post"> <table bgcolor = "#87CEFA" border = "1"cellspacing = "5" cellpadding = "10"> <th colspan = "5"bgcolor = "gray" >COURSE LIST</th> <tr><th>Classes</th><th>Location</th><th>Day of the week</th><th>Time of the day</th><th>Start Date</th></tr> <tr> <td> <select name="Classes" onchange="<?php setFilter(); ?>" > <option value="Select">SELECT</option> <option value="Simple Steps">Simple Steps</option> <option value="My Business Action Plan">My Business Action Plan</option> </select> </td> ........ </tr> </table> </form> </HTML> Initially I display all the data and when user selects a course from Classes dropdown I need to show data only for the selected class. How do I pass this filter? I have multiple dropdowns that need to be used as a filter condition. What is the best way of applying these filters and querying data? I appreciate your help in advance. -Tara. (edited by kenrbnsn to add tags) Link to comment https://forums.phpfreaks.com/topic/113109-php-html-select-control/ Share on other sites More sharing options...
lemmin Posted July 3, 2008 Share Posted July 3, 2008 You can't catch events with php, you have to catch the event with javascript and then reload something (like an iframe) using php. Or you can use AJAX to handle the events, that is a common way of doing this. Link to comment https://forums.phpfreaks.com/topic/113109-php-html-select-control/#findComment-581210 Share on other sites More sharing options...
tmandava Posted July 11, 2008 Author Share Posted July 11, 2008 Hi, Thanks for your response. I have tried using javascript to catch the event and construct the filter and then doing the query in php by passing the constructed filter to php but it doesn't seem to work. When I click on the search button, the flow is not going through the javascript function setFilter but directly going to query.php. Can you please help? Thanks in advance for your help. Here is my code: <?php include 'config.php'; include 'opendb.php'; $result = mysql_query("select * from $table"); echo "db queried " . mysql_numrows($result) . " .\n<br>"; include 'query2.php'; ?> <script> function setFilter(){ echo "in setfilter"; classVal = ""; locVal = ""; dowVal = ""; todVal = ""; sdVal = ""; filterStr = ""; //Get the dropdown values only when the value is something other than 'Please select' if (document.form1.Classes.selectedIndex != 0){ classVal = document.form1.Classes.options[document.form1.Classes.selectedIndex].text; filterStr = "course_title = " + classVal; } if (document.form1.Location.selectedIndex != 0){ locVal = document.form1.Location.options[document.form1.Location.selectedIndex].text; filterStr = filterStr + " location = " + locVal; } if (document.form1.Dayofweek.selectedIndex != 0){ dowVal = document.form1.Dayofweek.options[document.form1.Dayofweek.selectedIndex].text; filterStr = filterStr + " course_day = " + dowVal; } if (document.form1.TimeofDay.selectedIndex != 0){ todVal = document.form1.TimeofDay.options[document.form1.TimeofDay.selectedIndex].text; filterStr = filterStr + " course_time = " + todVal; } if (document.form1.StartDate.selectedIndex != 0){ sdVal = document.form1.StartDate.options[document.form1.StartDate.selectedIndex].text; filterStr = filterStr + " course_date = " + sdVal; } alert("filterstr is " + filterStr); document.form1.filterVal.value = filterStr; return true; } </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <TITLE>Course Listing</TITLE> <BODY bgcolor = "#FFCC66"></body> <form name="form1" action="query.php" method="post" onsubmit="setFilter();"> <table bgcolor = "#87CEFA" border = "1"cellspacing = "5" cellpadding = "10"> <th colspan = "5"bgcolor = "gray" >COURSE LIST</th> <tr><th>Classes</th><th>Location</th><th>Day of the week</th><th>Time of the day</th><th>Start Date</th></tr> <tr> <td> <select name="Classes"> <option value="Select">SELECT</option> <option value="Simple Steps">Simple Steps</option> <option value="My Business Action Plan">My Business Action Plan</option> </select> </td> ................ </tr> <br></br> <?php while($row = mysql_fetch_row($result)) { echo "<tr><td> $row[1] </td>"; echo "<td> $row[0] </td>"; echo "<td> $row[2] </td>"; echo "<td> $row[4] </td>"; echo "<td> $row[3] </td>"; echo "<td> </td></tr>"; } ?> <tr><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr> <?php include 'closedb.php'; ?> </table> <input type="hidden" name="filterVal" value=""/> <input type="submit" name="submit" value="Search"/> </form> </HTML> ****** query.php *********** <?php if(isset($_REQUEST['submit'])) { $selected_value = $_REQUEST['filterVal']; echo "Filter string value is ". $selected_value; $result = mysql_query("select * from $table where " . $selected_value); } ?> Link to comment https://forums.phpfreaks.com/topic/113109-php-html-select-control/#findComment-587773 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.