Icewolf Posted October 9, 2013 Share Posted October 9, 2013 Hi I was wondering is it possible to have a query ran after an item is selected from a dropdown menu? If not how do I create a button to run the query. Also how do I use the value from the drop down in my where clause? <?php //create_cat.php include 'connect.php'; include 'header.php'; echo '<h2>Update Rewards</h2>'; if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1 ) { //the user is not an admin echo 'Sorry, you do not have sufficient rights to access this page.'; } else { $sql = "Select user_name from users"; $result = mysql_query($sql); if(!$result) { //the query failed, uh-oh :-( echo 'Error while selecting from database. Please try again later.'; } else { $dropdown = "<select name='mem'>"; while($row = mysql_fetch_assoc($result)) { $dropdown .= "\r\n<option value='{$row['user_name']}'>{$row['user_name']}</option>"; } $dropdown .= "\r\n</select>"; echo $dropdown; } $post_sql = "select cat_name, point_earn, max_point from rewards where member = '" . mysql_real_escape_string($_POST['mem']) . "'"; $result_post = mysql_query($post_sql); if(!$result_post) { //the query failed, uh-oh :-( echo 'Error while selecting rewards from database. Please try again later.'; } else { echo '<table border="1"> <tr> <th>Category</th> <th>Points Earned</th> <th>Max Points</th> </tr>'; while($row = mysql_fetch_assoc($result_post)) { echo '<tr>'; echo '<td>' . $row['cat_name'] . '</td>'; echo '<td>'. $row['point_earn']. '</td>'; echo '<td>' . $row['max_point']. '</td>'; echo '</tr>'; } } } ?> Quote Link to comment Share on other sites More sharing options...
fastsol Posted October 9, 2013 Share Posted October 9, 2013 Are you trying to do this without a page refresh? If so you will need to use ajax. Check out this tutorial I have, it has a bit more than what you are asking about but it uses the methods you will need to do it. http://amecms.com/article/Building-Chained-Select-Boxes-with-Jquery-and-PHP Quote Link to comment Share on other sites More sharing options...
Icewolf Posted October 9, 2013 Author Share Posted October 9, 2013 I think I am just going to use a button. I have revised the code to add a button. However that is not showing up. Plus I am not sure if I have the code set up correctly to use the item from the drop down. Right now this code shows the dropdown and the row with the titles. I dont see the button nor does it shows any return values. <?php //create_cat.php include 'connect.php'; include 'header.php'; echo '<h2>Update Rewards</h2>'; if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1 ) { //the user is not an admin echo 'Sorry, you do not have sufficient rights to access this page.'; } else { $sql = "Select user_name from users"; $result = mysql_query($sql); if(!$result) { //the query failed, uh-oh :-( echo 'Error while selecting from database. Please try again later.'; } else { $dropdown = "<select name='mem'>"; while($row = mysql_fetch_assoc($result)) { $dropdown .= "\r\n<option value='{$row['user_name']}'>{$row['user_name']}</option>"; } $dropdown .= "\r\n</select>"; echo $dropdown; } $post_sql = "select cat_name, point_earn, max_point from rewards where member = '" . mysql_real_escape_string($_POST['mem']) . "'"; $result_post = mysql_query($post_sql); if(!$result_post) { //the query failed, uh-oh :-( echo 'Error while selecting rewards from database. Please try again later.'; } else { echo '<form action="" method="post"> <table border="1"> <tr> <th>Category</th> <th>Points Earned</th> <th>Max Points</th> </tr>'; while($row = mysql_fetch_assoc($result_post)) { echo '<tr>'; echo '<td>' . $row['cat_name'] . '</td>'; echo '<td>'. $row['point_earn']. '</td>'; echo '<td>' . $row['max_point']. '</td>'; echo '</tr>'; } '<input type="submit" value="Get Results"> </form>'; } } ?> Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 9, 2013 Solution Share Posted October 9, 2013 First problem is you're not echo'ing the dropdown menu inside your form. The second problem is you're not echo'ing the submit button '<input type="submit" value="Get Results"> </form>'; You have just defined a string. Your code cleaned up <?php //create_cat.php include 'connect.php'; include 'header.php'; echo '<h2>Update Rewards</h2>'; if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1 ) { //the user is not an admin echo 'Sorry, you do not have sufficient rights to access this page.'; } else { $sql = "Select user_name from users"; $result = mysql_query($sql); if(!$result) { //the query failed, uh-oh :-( echo 'Error while selecting from database. Please try again later.'; } else { $dropdown = "<select name='mem'>"; while($row = mysql_fetch_assoc($result)) { $dropdown .= "\r\n<option value='{$row['user_name']}'>{$row['user_name']}</option>"; } $dropdown .= "\r\n</select>"; echo ' <form action="" method="post">' . $dropdown . ' <input type="submit" value="Get Results"> </form>'; } // only qeury the rewards table when the form above has been submitted if(isset($_POST['mem'])) { $post_sql = "select cat_name, point_earn, max_point from rewards where member = '" . mysql_real_escape_string($_POST['mem']) . "'"; $result_post = mysql_query($post_sql); if(!$result_post) { //the query failed, uh-oh :-( echo 'Error while selecting rewards from database. Please try again later.'; } else { echo '<table border="1"> <tr> <th>Category</th> <th>Points Earned</th> <th>Max Points</th> </tr>'; while($row = mysql_fetch_assoc($result_post)) { echo '<tr>'; echo '<td>' . $row['cat_name'] . '</td>'; echo '<td>'. $row['point_earn']. '</td>'; echo '<td>' . $row['max_point']. '</td>'; echo '</tr>'; } echo '</table>'; } } } ?> Quote Link to comment Share on other sites More sharing options...
Icewolf Posted October 9, 2013 Author Share Posted October 9, 2013 Thank you so much Ch0cu3r that worked. 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.