Jump to content

Creating a button and using Drop Down Value


Icewolf
Go to solution Solved by Ch0cu3r,

Recommended Posts

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>'; 
		 
	
		 
				
				}
   }
  
}
?>
Link to comment
Share on other sites

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>';
	}

}

?>
Link to comment
Share on other sites

  • Solution

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>';
		}
	}
}

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.