hi,
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

thanks

Recommended Answers

All 6 Replies

Had you tried? If so, then post the code.

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

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

<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">
<table>
<td class="textbox">select category to change</td>
<td>
<select onchange="fillForm()"  id="cmbcat" name="cmbcat">

<option  value="0">none</option>
<?php 
include_once("../include/myconnect.php");
$sqlq = "SELECT * FROM category";
$result = mysql_query($sqlq);
while ($row = mysql_fetch_array($result))
{
	$id = $row['idcat'];
	$name = $row['catname'];
	$image=$row['catimg'];
	echo "<option value=\"{$id}\">{$name}</option>\n";
}
?>
</select>

</td>

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

<tr>
	<td class="label">Category Image</td>
	<td class="textbox"><input type="file" name="cat_image" id="cat_image" size="50" /></td>
</tr>
<tr>
	<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="" />
	</td>
</tr>
</table>
</form>
</div>
</div>

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;
alert(dropdownValue);
 document.forms[0].cat_name.value = obj.options[obj.selectedIndex].text;
}
</script>

ofcourse i changed this

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

to

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

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

Thank you Zido85. Nice code.

This code might help to you to solve this problem.

<?php
  $cn=mysql_connect("localhost","root","root");
  $db=mysql_select_db("test",$cn);
  $sql="select * from CATEGORY";
  $res=mysql_query($sql);
  
  $catid=$_REQUEST["catid"];
  $category=$_REQUEST["category"];
  $catimage=$_FILES["catimage"];
  
  if(isset($catid,$category,$catimage)) {
           // Write your statement to update a record..
   }

  $cat=$_REQUEST["cat"];
  if(!isset($cat))  $cat="";
  
  $str_cat="<select name=\"cat\"  onchange=\"submitForm()\">";
  $str_cat="$str_cat <option value=\"\">Select</option>";
  while($row=mysql_fetch_assoc($res))
   {
      if($row["catid"]==$cat)  
         $str_cat="$str_cat  <option value=\"$row[catid]\" selected >$row[category]</option>";
     else
         $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";
       $res=mysql_query($sql);
       $row=mysql_fetch_assoc($res);
       $catid=$row["catid"];
       $category=$row["category"];
       $catimage=$row["catimage"];
       
  }
?>
<script type="text/javascript">
      function submitForm() {
           form1.submit();
      }
</script>

<body>
  <form name="form1" method="post" action="a2.php" enctype="multipart/form-data">
    <br/>Select Category to Edit <?php echo $str_cat; ?>
    <?php
        if($cat!="") {
$form=<<<POST
<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"/>
POST;
    echo $form;
        }
    ?>
 </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

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.