this lesson came from my senior, Uncle Muji :)…

If you are going to build a web with dynamic radio button (radio button for each row from database query result), there are few ways to design the solution. My senior’s design is by giving the radio button name with ‘radio_idFromDatabase’. the problem here is, how to get the radio button value ?,,,

the solution is by looping the $_POST array, do selection for array members that start with “radio”, then do the main business, like below code:

the html code :


<form method=”POST”>

	<!– this is sample for generated radio button

	<input type=”radio” name=”radio_0″ value=”M” /> Masuk

	<input type=”radio” name=”radio_0″ value=”K” /> Keluar

	<input type=”radio” name=”radio_0″ value=”S” /> Sakit

	<input type=”radio” name=”radio_1″ value=”M1″ /> Masuk

	<input type=”radio” name=”radio_1″ value=”K1″ /> Keluar

	<input type=”radio” name=”radio_1″ value=”S1″ /> Sakit

	<input type=”submit” value=”sub” />

</form>

the PHP looping code :


foreach ($_POST as $field => $value)

{

	$inputName=substr($field,0,5);

	if ($inputName == "radio")

	{

		$id=substr($field,6,1);

		echo "Id : $id";

	}

}

end.

Advertisements