Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:51:29+00:00 2026-06-04T16:51:29+00:00

I have a PHP based form. It has a drop down list which fetches

  • 0

I have a PHP based form. It has a drop down list which fetches values from the mysql database.

<select name=cat onchange="AjaxFunction(this.value);" style="width=600"> <br>
<option value='' Select One</option> 
<br>
<? 
  require "config.php";// connection to database  
  $q=mysql_query("select cat_id from categories"); 
  while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
  } 
?> 
</select> 

The drop down list works 100%. I then have another text input field,

<input type=text name=copycat>

I want the value of the copycat input box to be equal to the selected value of the drop down list and to update in real time.

Can this be done? I should imagine quite easily. I am thinking something like:

<input type=text name=copycat onBlur="document.getElementById('cat').value=this.value;">

Any help would be appreciated.

Thanks and Regards,
Ryan

UPDATE

Code to get the javscript sendValue working with value of copycat.

catalin87, please assist with getting sendvalue working, currently, clicking the select button has no response fromthe browser.

Thanks again,
Ryan

<? 

  $con = mysql_connect('localhost', 'username', 'password'); 
 if (!$con) 
   { 
   die('Could not connect to server: ' . mysql_error()); 
   } 
   $db=mysql_select_db("database", $con); 

    if (!$db) 
   { 
   die('Could not connect to DB: ' . mysql_error()); 
   } 


$sql="select packcode,category,description,grouping,packconfig,sellingunits,eottpoints from skudata order by category, packcode";
$result=mysql_query($sql);

 ?> 

<script type="text/javascript"> 

  function AjaxFunction(cat_id) { 
    var httpxml; 
    try { 
      // Firefox, Opera 8.0+, Safari 
      httpxml = new XMLHttpRequest(); 
    } catch (e) { 
      // Internet Explorer 
      try { 
        httpxml = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
        try { 
          httpxml = new ActiveXObject("Microsoft.XMLHTTP"); 
        } catch (e) { 
          alert("Your browser does not support AJAX!"); 
          return false; 
        } 
      } 
    } 
    function stateck() { 
      if (httpxml.readyState == 4) { 
        var myarray = eval(httpxml.responseText); 
        // Before adding new we must remove previously loaded elements 
        for (j = document.testform.subcat.options.length - 1; j >= 0; j--) { 
          document.testform.subcat.remove(j); 
        } 
        for (i = 0; i < myarray.length; i++) { 
          var optn = document.createElement("OPTION"); 
          optn.text = myarray[i]; 
          optn.value = myarray[i]; 
          document.testform.subcat.options.add(optn); 
        }  
      } 
    } 
    var url="dd.php"; 
    url = url+"?cat_id="+cat_id; 
    url = url+"&sid="+Math.random(); 
    httpxml.onreadystatechange = stateck; 
    httpxml.open("GET",url,true); 
    httpxml.send(null); 
  } 

</script> 




 <script type="text/javascript"> 
function updateinput(){ 
var e = document.getElementById("subcat"); 
var catSelected = e.options[e.selectedIndex].value; 

document.getElementById("copycat").value=catSelected; 
} 
</script> 


<script type="text/javascript"> 
function sendValue(value)
 { 
 value = e.options[e.selectedIndex].value; 
 var parentId = <?php echo json_encode($_GET['id']); ?>; 
 window.opener.updateValue(parentId, value); 
 window.close(); 
 } 
 </script> 


<form name="testform">
Category: &nbsp; <select name=cat id=cat onchange="AjaxFunction(this.value);" style="width=600"> <br>
<option value='' style="width=600">Select One</option> 
<br>
<? 

  require "config.php";// connection to database  
  $q=mysql_query("select * from categories"); 
  while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
  } 

?> 
</select> 
 <br><br>
 Pack Code:
<select name=subcat onchange="updateinput();" > 
 <br><br>
</select>
<br><br>
<input type=text name=copycat id=copycat > 
<br><br>
<td><input type=button value="Select" onClick="sendValue(document.getElementById(copycat))" /></td>
</form> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:51:30+00:00Added an answer on June 4, 2026 at 4:51 pm

    1`st set an id to the select box and to the input, and add an onChange event:

    <select name=cat id=cat onchange="updateinput();" style="width=600">
    <input type=text name=copycat id=copycat >
    

    Then put this function somewhere:

    <script type="text/javascript">
    function updateinput(){
    var e = document.getElementById("cat");
    var catSelected = e.options[e.selectedIndex].text;
    
    document.getElementById("copycat").value=catSelected;
    }    
    </script>   
    

    This will populate withe the label of the selected item, if u want the value of the selected item use this function:

    <script type="text/javascript">
    function updateinput(){
    var e = document.getElementById("cat");
    var catSelected = e.options[e.selectedIndex].value;
    
    document.getElementById("copycat").value=catSelected;
    }
    </script>   
    

    here is your full code:

         <? 
    
      $con = mysql_connect('localhost', 'username', 'password'); 
     if (!$con) 
       { 
       die('Could not connect to server: ' . mysql_error()); 
       } 
       $db=mysql_select_db("dbname", $con); 
    
        if (!$db) 
       { 
       die('Could not connect to DB: ' . mysql_error()); 
       } 
    
    
    $sql="select packcode,category,description,grouping,packconfig,sellingunits,eottpoints from skudata order by category, packcode";
    $result=mysql_query($sql);
    
     ?> 
    
    <script type="text/javascript"> 
    
      function AjaxFunction(cat_id) { 
        var httpxml; 
        try { 
          // Firefox, Opera 8.0+, Safari 
          httpxml = new XMLHttpRequest(); 
        } catch (e) { 
          // Internet Explorer 
          try { 
            httpxml = new ActiveXObject("Msxml2.XMLHTTP"); 
          } catch (e) { 
            try { 
              httpxml = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e) { 
              alert("Your browser does not support AJAX!"); 
              return false; 
            } 
          } 
        } 
        function stateck() { 
          if (httpxml.readyState == 4) { 
            var myarray = eval(httpxml.responseText); 
            // Before adding new we must remove previously loaded elements 
            for (j = document.testform.subcat.options.length - 1; j >= 0; j--) { 
              document.testform.subcat.remove(j); 
            } 
            for (i = 0; i < myarray.length; i++) { 
              var optn = document.createElement("OPTION"); 
              optn.text = myarray[i]; 
              optn.value = myarray[i]; 
              document.testform.subcat.options.add(optn); 
            }  
          } 
        } 
        var url="dd.php"; 
        url = url+"?cat_id="+cat_id; 
        url = url+"&sid="+Math.random(); 
        httpxml.onreadystatechange = stateck; 
        httpxml.open("GET",url,true); 
        httpxml.send(null); 
      } 
    
    </script> 
    
    
    <script type="text/javascript"> 
    function sendValue(value) 
    { 
        var parentId = <?php echo json_encode($_GET['id']); ?>; 
       window.opener.updateValue(parentId, value);
        window.close(); 
    } 
    </script> 
    
     <script type="text/javascript"> 
    function updateinput(){ 
    var e = document.getElementById("cat"); 
    var catSelected = e.options[e.selectedIndex].value; 
    
    document.getElementById("copycat").value=catSelected; 
    } 
    </script> 
    
    <form name="testform">
    Category: &nbsp; <select name=cat id=cat onchange="updateinput();" style="width=600"> <br>
    <option value='' style="width=600">Select One</option> 
    <br>
    <? 
    
      require "config.php";// connection to database  
      $q=mysql_query("select * from categories"); 
      while($n=mysql_fetch_array($q)){ 
        echo "<option value=$n[cat_id]>$n[category]</option>"; 
      } 
    
    ?> 
    </select> 
     <br><br>
     Pack Code:
    <select name=subcat > 
     <br><br>
    </select>
    <br><br>
    <input type=text name=copycat id=copycat >
    <br><br>
    <td><input type=button value="Select" onClick="sendValue('<?php echo $rows['packcode']; ?>')" /></td>
    </form> 
    

    I changed:

    Put : onchange=”updateinput();”

    <select name=cat id=cat onchange="updateinput();" style="width=600">
    

    and

    <input type=text name=copycat id=copycat >
    

    remove:

     onBlur="document.getElementById('cat').value=this.value;"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which has a drop list inside a form. The drop
I have different php output in jQuery-based tabs. This output is formed from database
I have a PHP document that query's a MYSQL database based on the input
I have a complex input form within a PHP based web application. To structure
I have typical web application based on PHP, HTML, and javascript. From an HTML
I have a form where the user has to enter a list of choices
I have a PHP file which produces an Xml sitemap based on data which
I have a form-1 which has 4 fields. when the user inputs data in
Is it possible to have selective queries in PostgreSQL which select different tables/columns based
I have a problem in creating an AJAX-based form submission which if its response

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.