Jump to content


Photo

Creating a button and using Drop Down Value

buttonand drop

Best Answer Ch0cu3r, 09 October 2013 - 03:43 AM

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

?>
Go to the full post


  • Please log in to reply
4 replies to this topic

#1 Icewolf

Icewolf

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 08 October 2013 - 08:02 PM

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


#2 fastsol

fastsol

    Advanced Member

  • Members
  • PipPipPip
  • 491 posts
  • LocationMinnesota

Posted 08 October 2013 - 09:38 PM

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/ar...-Jquery-and-PHP


You never know everything, I learn everday!

http://amecms.com

http://amewebdesigns.com


#3 Icewolf

Icewolf

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 08 October 2013 - 11:03 PM

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

}

?>


#4 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,304 posts

Posted 09 October 2013 - 03:43 AM   Best Answer

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

?>


#5 Icewolf

Icewolf

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 09 October 2013 - 08:30 AM

Thank you so much Ch0cu3r that worked.

 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com