hi,

I am creating a script where i have 4 combo box collecting the value from database. and i want if some one select the other option from the combo box the text box is enable.

and all 4 combo box have an alternative text box and i want same feature.

when page is load 1st time by default all the text box should
disable.
and as we select the other option from combo boxes the page reload
with appropriate enabled text box.

the problem is when i select any option other then 'others'
it works fine but when i select 'other' option instead of open the text box it still disable.
I cant understand the problem....

thats my code of javascript:

function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value; 
self.location='create_own_inventory.php?cat=' + val ;
}
function def()
{
document.f1.newcat.disabled=true;
document.f1.newsubcat1.disabled=true;
document.f1.newsubcat2.disabled=true;
document.f1.newsubcat3.disabled=true;
}

function makeChoice()

{
 var val = 0;
document.f1.newcat.disabled=true;
document.f1.newsubcat1.disabled=true;
document.f1.newsubcat2.disabled=true;
document.f1.newsubcat3.disabled=true;

for( i = 0; i < document.f1.cat.length; i++ )
{
if( document.f1.cat.options[i].selected == true )
{
val = document.f1.cat.options[i].value;
if(val=='other')
{
document.f1.newcat.disabled=false;
document.f1.newcat.focus();
}
}
}
}

my php combo box is:

echo "&nbsp;&nbsp;&nbsp;<select name='cat' onchange=\"reload(this.form)\" onclick=\"makeChoice()\"><option value=''>Main Catagory</option>";
while($noticia2 = mysql_fetch_array($quer2)) 
	{ 
    if($noticia2['Top_Level_Category']==@$cat)
		{
		echo "<option selected value='$noticia2[Top_Level_Category]'>$noticia2[Top_Level_Category]</option>"."<BR>";
	}
else
	{
	echo  "<option value='$noticia2[Top_Level_Category]'>$noticia2[Top_Level_Category]</option>";
	}
   }
echo  "<option value='other'>Others</option>";
echo "</select>&nbsp;&nbsp; OR Create Own Main Catagory";
echo '<INPUT TYPE="text" NAME="newcat"><BR><BR>';

function def() i am using in body onload
event using

<body onload="def();">

plz help me what is my problem???
in that code in javascript

Dani AI

Generated

— the symptom (selecting "Other" but the textbox stays disabled) usually comes from the enable/disable logic running at the wrong time or from the page reload/server output resetting the field. 's request for a browser View‑Source was on point: common causes are attaching the handler to the select's onclick (it can fire before the new option is applied), using a body onload that always disables everything, or a mismatch between the option value and the string checked in JS (case or extra spaces).

A robust client-side approach is to initialize state on DOMContentLoaded and listen for the select's change event. Example (give each select/input an id):

<script>
document.addEventListener('DOMContentLoaded', function () {
  var sel = document.getElementById('catSelect');
  var input = document.getElementById('newcat');

  function toggle() {
    input.disabled = (sel.value !== 'other');
    if (!input.disabled) input.focus();
  }

  sel.addEventListener('change', function () {
    toggle();
    // if server reload is still desired, use:
    // window.location.href = 'create_own_inventory.php?cat=' + encodeURIComponent(sel.value);
  });

  toggle(); // set correct state after load
});
</script>

Add a small server-side fallback so a reload preserves the enabled state:

<?php
$selected = isset($_GET['cat']) ? $_GET['cat'] : '';
$disabled = ($selected === 'other') ? '' : 'disabled';
echo "<input type='text' name='newcat' id='newcat' $disabled>";
?>

Debug checklist: inspect generated HTML to confirm the option's value is exactly "other" (no capitalization or trailing spaces), ensure the select and input have the ids used in JS, open the browser console for errors, and add temporary console.log statements. For four combos, give each select a data-target that names its paired input and reuse the same handler for all.

Recommended Answers

All 3 Replies

Could you post the HTML from a View Source on your browser? I can't see what your select options look like from the server code.

Could you post the HTML from a View Source on your browser? I can't see what your select options look like from the server code.

I send my code with database structure.
2 files r php file and data1.txt is database file.
plz create your own connection string of php mysql
plz help me.

I send my code with database structure.
2 files r php file and data1.txt is database file.
plz create your own connection string of php mysql
plz help me.

Hi frnd i am waiting for your reply and already sent my file and database structure.

in the current php file i dont use none of the javascript which i was mentioned above.plz give me the way and procedure to use those one or any other to achieve my goal...
thank you.

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.