I choose something from selectbox and click on the button and i get list of the data from database but, whatever i choose from selectbox doesn't remain what i have chosen. It sets itself to first value. How can i do "selected" ?

<?php
$mysqli = new mysqli("localhost","root","","inantest");
if (mysqli_connect_errno()) {
	printf ("Connection failed : %s\n", mysqli_connect_error());
	exit();
	}

	if ("Ara" == $_POST["iller"]) {
		//$select = "selected=\"Selected\"";
		$aranacakIl=$_POST["selectIl"];
		
		$sql = "SELECT * FROM plaka WHERE ilName= '$aranacakIl' "; 
		$calistir = mysqli_query($mysqli, $sql);

		if ($calistir) {
			while ($newArray = mysqli_fetch_array ($calistir, MYSQLI_ASSOC)) {
				$plaka = $newArray['ilPlaka'];
			}
		}

		$sql = "SELECT * FROM ilce WHERE ilPlaka= '$plaka' "; 
		$calistir = mysqli_query($mysqli, $sql);

		if ($calistir) {
			while ($newArray = mysqli_fetch_array ($calistir, MYSQLI_ASSOC)) {
				$ilce = $newArray['ililce'];
				print "$ilce<br>";
			}
		}	
	}
?>


<form method="post" action="">
  <select name="selectIl">
<?php
	$sql = "SELECT * FROM il";
	$calistir = mysqli_query($mysqli, $sql);

	if ($calistir) {
		while ($newArray = mysqli_fetch_array ($calistir, MYSQLI_ASSOC)) {
			$il = $newArray['ilName'];
		    printf("<option value=\"$il\">$il</option>");
		}
		//mysqli_free_result($calistir);
		//mysqli_close($mysqli);
	}
?>
  </select>
  <input type="submit" name="iller" value="Ara" />
</form>

Use a hidden text field named submitted and the value as true.

Then before your mysql populates the drop-down write

<option value="'.$value.'">'.$value.'</option>

$value being the value which was previously selected.

You just have to check if $_POST is equal to the value of the option. If it is equal, echo selected, else echo nothing..

You just have to check if $_POST is equal to the value of the option. If it is equal, echo selected, else echo nothing..

If you want to do that this way, you have to pass the value to the other page. use $_SESSION or $_GET ...

We can have an onchange event for the select box to submit the page.

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.