i want to ask if anybody knows :
i have a combobox where the values are obtained from MySql database.
i want the user to choose one of those value to edit its field.
for example if the user choose X from the combobox iwant all fields related to X from the database to be posted in the form where the user can edit it


the script just gives me "none" in the alert and if i check to index it gives me 0.
i tried too many things, but there is something missing:S
im new in php,mysql and javascript
i apreciate ur help

function fillForm()
{	var cb = document . getElementById("cmbcat");
alert(cb . options[cb . selectedIndex] . text);

<div id="editcategory_div" class="main_area">

<div id="editcategoryform_div" class="main_area">

<form id="editcategory_form" name="editcategory_form" action="" method="post" enctype="multipart/form-data">
<td class="textbox">select category to change</td>
<select onchange="fillForm()"  id="cmbcat" name="cmbcat">

<option  value="0">none</option>
$sqlq = "SELECT * FROM category";
$result = mysql_query($sqlq);
while ($row = mysql_fetch_array($result))
	$id = $row['idcat'];
	$name = $row['catname'];
	echo "<option value=\"{$id}\">{$name}</option>\n";


	<td class="label">Category Name</td>
	<td class="textbox"> <input type="text" name="cat_name" id="cat_name"  value=""></td>

	<td class="label">Category Image</td>
	<td class="textbox"><input type="file" name="cat_image" id="cat_image" size="50" /></td>
	<td colspan="2" class="buttons_area">
		<input type="submit" id="editcategory_submit" name="editcategory_submit" value="submit"/>
        <input type="reset" id="editcategory_reset" name="editcategory_reset" value="reset" /> 
		<input type="hidden" id="do" name="do" value="editcategory"/>
		<input type="hidden" id="cat_id" name="cat_id" value="" />

thanks adatapost for your concern

this is what i did and it worked only when i test it without include

function fillForm(obj)
var dropdownIndex = document.getElementById('cmbcat').selectedIndex;
var dropdownValue = document.getElementById('cmbcat')[dropdownIndex].text;
 document.forms[0].cat_name.value = obj.options[obj.selectedIndex].text;

ofcourse i changed this

<select onchange="fillForm()"  id="cmbcat" name="cmbcat">


<select onchange="fillForm(this)"  id="cmbcat" name="cmbcat">

but when i try to include it , it doesn't work, and ideas?

Thank you Zido85. Nice code.

This code might help to you to solve this problem.

  $sql="select * from CATEGORY";
  if(isset($catid,$category,$catimage)) {
           // Write your statement to update a record..

  if(!isset($cat))  $cat="";
  $str_cat="<select name=\"cat\"  onchange=\"submitForm()\">";
  $str_cat="$str_cat <option value=\"\">Select</option>";
         $str_cat="$str_cat  <option value=\"$row[catid]\" selected >$row[category]</option>";
         $str_cat="$str_cat  <option value=\"$row[catid]\" >$row[category]</option>";
  $str_cat= $str_cat . "</select>";        
  if($cat!="") {
       $sql="select * from CATEGORY where catid=$cat";
<script type="text/javascript">
      function submitForm() {

  <form name="form1" method="post" action="a2.php" enctype="multipart/form-data">
    <br/>Select Category to Edit <?php echo $str_cat; ?>
        if($cat!="") {
<br/>Cat ID <input type="text" name="catid" value="$catid"/>
<br/>Cat ID <input type="text" name="category" value="$category"/>
<br/>Image <img src="$catimage" width="100px"/>
<br/>Upload Image <input type="file" name="catimage"/>
<br/><input  type="submit" value="Update" name="cmd"/>
    echo $form;

thanks bro, i will try it
my code worked i just had many PHP files to include, and i was putting this file the last, when i put it first one, it worked:D don't know y, but it worked.
thanks again

you can do this by pressing the submit button or by using the Ajax. by using Ajax you can do it easily without reloading the page. just write the code to get the selected values in $_POST, or in $_GET and pass the value using ajax. and retrieve the result

This question has already been answered. Start a new discussion instead.