Jump to content

I want to fill the form from MySQL Data using a combo selection ?


etdsbastar

Recommended Posts

hello there,

 

please have a look in the below given code:

 

<link rel=stylesheet HREF="/css/wards.css" type="text/css" media="screen">
<?php include_once '../header.php'; ?>
<?php include_once '../config/config.php'; ?>
<?php include_once '../functions/functions.php'; ?>
<?php extract($GLOBALS); ?>

<div id="content">
	<div id="labelopt"><div class="box"></div> jax ds lHkh dkWye vfuok;Z gSa A  'kwU; ls Hkjs gq;s fdlh Hkh dkWye dks [kkyh ugha NksM+sa] vxj dksbZ izfo"Bh ugha gks rks 0 M+kysa A</div>
	<div id="labelopt"></div>
	<h2>iqjkus okMZ dk la'kks/ku</h2>
	<div id="dp-sp">
		<form name="wardeditform" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return ValidateFormWardsNew()">
			<table border="1">
				<tr>
					<td id="labelmust">okMZ dzekad %</td>
					<td>
						<select name="wardno" id="wardno" onchange="fillform()">
							<?php populatecombo(); ?>
						</select>
					</td>
					<td id="labelmust">okMZ dk uke %</td>
					<td><input class="hindi" type="text" size="30" name="name" maxlength="30" />
					<td id="labelmust">t+ksu dzekad %</td>
					<td colspan="3"><input class="english" type="text" size="10" name="zone" maxlength="4" />
				</tr>
			</table>
			<table cellspacing="0" cellpadding="2">
				<tr>
					<th>vkoklh; ok"kZd HkkM+s dk C;ksjk</th>
					<th>fuekZ.k oxZ 01</th>
					<th>fuekZ.k oxZ 02</th>
					<th>fuekZ.k oxZ 03</th>
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ij</th>
					<td align="center"><input class="english" type="text" size="10" name="a1" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b1" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c1" maxlength="5" value="0" />
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ls gVdj</th>
					<td align="center"><input class="english" type="text" size="10" name="a2" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b2" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c2" maxlength="5" value="0" />
				</tr>
				<tr>
					<th>O;kolkf;d@vkS|ksfxd ok"kZd HkkM+s dk C;ksjk</th>
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ij</th>
					<td align="center"><input class="english" type="text" size="10" name="a3" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b3" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c3" maxlength="5" value="0" />
				</tr>
				<tr>
					<th id="thhead">eq[; lM+d ;k ckt+kj ls gVdj</th>
					<td align="center"><input class="english" type="text" size="10" name="a4" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="b4" maxlength="5" value="0" />
					<td align="center"><input class="english" type="text" size="10" name="c4" maxlength="5" value="0" />
				</tr>
			</table>
		</div>
		<div id="menu">
		<table>
			<tr>
				<td>
					<input type="submit" name="btnsave" value="Edit" />
		</form>
					<input type="button" value="View List" name="btnwardslist" onclick="location.href='/sp/spnew.php';" />
					<input type="button" value="Back" name="btnspback" onclick="location.href='/wards.php';" />
				</td>
			</tr>
		</table>
		</div>
	</div>
<?php include_once '../footer.php'; ?>

<?php
if(isset($_POST['btnsave']))
{
        $wardno = "'".$_POST['wardno']."'";
        $name = "'".$_POST['name']."'";
        $zone = "'".$_POST['zone']."'";
        $a1 = "'".$_POST['a1']."'";
        $a2 = "'".$_POST['a2']."'";
        $a3 = "'".$_POST['a3']."'";
        $a4 = "'".$_POST['a4']."'";
        $b1 = "'".$_POST['b1']."'";
        $b2 = "'".$_POST['b2']."'";
        $b3 = "'".$_POST['b3']."'";
        $b4 = "'".$_POST['b4']."'";
        $c1 = "'".$_POST['c1']."'";
        $c2 = "'".$_POST['c2']."'";
        $c3 = "'".$_POST['c3']."'";
        $c4 = "'".$_POST['c4']."'";
        
	if(mysql_num_rows(mysql_query("SELECT * FROM wards WHERE wardno=".$wardno)) > 0){
		echo "<script type='text/javascript'>alert('Refresh check, the record already exists.')</script>";
	}else{
		$sql = "insert into wards (wardno, name, zone, a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4) values (".$wardno.", ".$name.", ".$zone.", ".$a1.", ".$a2.", ".$a3.", ".$a4.", ".$b1.", ".$b2.", ".$b3.", ".$b4.", ".$c1.", ".$c2.", ".$c3.", ".$c4.")";
		mysql_query($sql) or die("Error querying table.");
		mysql_close($db);
		unset($wardno); unset($name); unset($zone); unset($a1); unset($a2); unset($a3); unset($a4);	unset($b1);
		unset($b2);	unset($b3); unset($b4); unset($c1); unset($c2); unset($c3); unset($c4);

		echo "<script type='text/javascript'>alert('Successfully saved the data.')</script>";
	}
	exit();
}

function populatecombo()
{
	$dropdown = "";
	$sqlcmb = "select distinct wardno from wards";

	$result = mysql_query($sqlcmb) or die(mysql_error('Unable to query the table'));

	while($row = mysql_fetch_assoc($result)) {
		$dropdown .= "\n<option value='{$row['wardno']}'>{$row['wardno']}</option>";
	}
	echo $dropdown;
}
?>
<script type='text/javascript'>
function choice()
{

}
</script>

 

please have a look on the line:

 

<select name="wardno" id="wardno" onchange="fillform()">

 

and the function:

 

<script type='text/javascript'>
function choice()
{

}
</script>

 

Actually I want to populate the rest of the form fields with the data selected by the combo box i.e. wardno.

 

I am a newbie, I am getting confused where to start and where to end. Any help will be greatly appreciated.

Link to comment
Share on other sites

you need to start by assigning ID's you your form elements.  Then you need to perform a SELECT query on the database that you are looking up - would suggest calling a seporate query PHP page with the Jscript using URL fed variables - then have the Jscript feed the results into the form elements using the ID's that you gave them. 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.