hi i have a php dropdownlistbox (select button of html) which having three values on is all and second is activated and third one is deactivated. i want when use select all then show all values of database and when user click on activated then show only activated values and if user select deactivated then only show deactivated values while i manage in database a status field which values are 0 & 1. 0 for deactivated and 1 for activated.
how can i use php dropdownlistbox event with php to do this type of work.
thanks
regards
umesh daiya

Recommended Answers

All 5 Replies

put dropdownlistbox in form tag, and post it on change event. get dropdownlistbox value in php code and apply the search from database related to your value (value get from dropdownlistbox).

this is most simple way to handle search with dropdownlistbox.

Javascript

function limiterOnChange()
  {
  document.limiterForm.submit();
  }

PHP

// near the top
$val='2';
if ($_POST['limiter']) {
  $val=$_POST['limiter'];
  if ($val==2) {
    $db_data=mysql_query("select * from yourtable");
    } else {
    $db_data=mysql_query("select * from yourtable where activated='$val'");
    }

// right above the area that displays the database info (is in HTML)
$lsel=array("","","");
$lsel[$val]='selected';
?>

<form name='limiterForm' action='' method='post'>
<select name='limiter' onchange='limiterOnChange()'>
<option value='2' <?php echo $lsel['2']; ?> >All</option>
<option value='1' <?php echo $lsel['1']; ?> >Activated</option>
<option value='0' <?php echo $lsel['0']; ?> >De-Activated</option>
</select>
</form>
<!-- now use PHP to display the mysql_fetch_array from $db_data -->

I haven't tested this, but I think it's generally the right direction. The javascript simply allows it to automatically submit the change when you change the dropdown. You may have to correct for errors and add code to make it work. No warranties expressed or implied. ;)

David

This is just quick code, UNTESTED, but I think that this will get you in the direction that you are looking to go:

thispage.php

<?php

// Assuming you have set up the connection, let's play the get game

if(isset($_GET['listtype'])) {
	$type = $_GET['selection'];
} else {
	$type = "all";
}

if($type=="all") {
	$sql = "SELECT * FROM `whatever`";
}

switch($type) {
	case "all":
		$sql = "SELECT * FROM `whatever`";
		break;
	case "active":
		$sql = "SELECT * FROM `whatever` WHERE `status`='1'";
		break;
	case "inactive":
		$sql = "SELECT * FROM `whatever` WHERE `status`='0'";
		break;
	default:
		$sql = "SELECT * FROM `whatever`";
		break;
}

?>
<html>
<head>
</head>
<body>
<form action="thispage.php" method="get">
<select name="selection">
	<option value="all">All</option>
    <option value="active">Active</option>
    <option value="inactive">Inactive</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
<?php

  $result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());
  while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
	  
	  // output and format your data from the database here
  }
?>
</body>
</html>

Hope this helps. :)

<?
if (isset($_POST['limiter'])){

    $val = $_POST['limiter'];
        if ($val==2) {
            echo "All";
        } else if ($val==1){
            echo "Activated";
        } else if ($val==0){
            echo "De-Activated";
        }
}
?>

<form name='limiterForm' action='' method='post'>
<select name='limiter' onChange="javascript: limiterForm.submit()" >
<option value='2' <?php if(@$val == '2') echo "selected"; ?> >All</option>
<option value='1' <?php if(@$val == '1') echo "selected"; ?> >Activated</option>
<option value='0' <?php if(@$val == '0') echo "selected"; ?> >De-Activated</option>
</select>
</form>

Simple code, just apply your query in conditions in php code

Member Avatar for diafol

Just a note on 'autosubmit'. Although this technique is now widely used, especially for ajax functionality. WHat about non-js users? Will there be graceful degradation, or do you ignore them?

There are accessibility issues with these sort of implementations. Just my 2p.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.