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

Dani AI

Generated

Short roadmap and practical notes that build on question and the replies by , and .

Wrap the <select> in a form and submit (auto-submit with JS is fine), but prefer method="get" for filtering so results are bookmarkable/shareable and have no side-effects. See the HTML form guidance. MDN: <form> element. (developer.mozilla.org)

Server-side: always treat the selected value as untrusted input, whitelist it, then run a parametrized query. Example (safe, minimal):

// read and whitelist
$status = filter_input(INPUT_GET, 'status', FILTER_DEFAULT) ?? 'all';
$allowed = ['all','active','inactive'];
if (!in_array($status, $allowed, true)) $status = 'all';

// build query and params
$sql = 'SELECT * FROM items';
$params = [];
if ($status !== 'all') {
  $sql .= ' WHERE status = :status';
  $params[':status'] = $status === 'active' ? 1 : 0;
}

// prepared statement with PDO
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Use server-side input helpers like filter_input() to read user input and then enforce a whitelist. [PHP: filter_input]. (php.net) Use prepared statements (PDO or mysqli) rather than inserting values into SQL strings. [PHP: PDO::prepare]. (php.net)

Security: do not use the old mysql_* extension — it was deprecated and removed from modern PHP; switch to MySQLi or PDO and follow the OWASP guidance on preventing SQL injection. PHP: mysql_query deprecation note. (php.net) [OWASP SQL Injection Prevention Cheat Sheet]. (cheatsheetseries.owasp.org)

Accessibility and progressive enhancement: autosubmit via onchange is convenient (as shown by ), but provide a non-JS fallback (a visible submit button or a <noscript> block) so users without JavaScript can still use the filter. Follow progressive-enhancement / WCAG techniques for client-side scripting fallbacks. MDN: <noscript> element. (developer.mozilla.org) [W3C WCAG techniques]. (w3.org)

Those points cover the common gaps: prefer GET for filters, validate/whitelist server-side, use prepared statements and modern drivers, and add a noscript fallback for accessibility.

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 Member #120589

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.