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 8776497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:04:35+00:00 2026-06-13T19:04:35+00:00

I have a form which has a select tag; on selection of an item

  • 0

I have a form which has a select tag; on selection of an item from the drop down rest of the form fields should get filled accordingly.Note that all the form data comes from mysql database. Below is my code :

    <script language="javascript" type="text/javascript">
            function populateData($id)
            {
                alert('in populateData');
                y = document.getElementById("selectCard");
                document.getElementById("to_address").value = to_address[y.selectedIndex];
                document.getElementById("subject").value = subject[y.selectedIndex];
                document.getElementById("email_content").value = email_content[y.selectedIndex];
            }
    </script>
    <body>
        <?php
            mysql_connect("localhost", "someUser", "password") or die("Connection Failed");
            mysql_select_db("mns")or die("Connection Failed");
            $query = "SELECT * FROM table";
            $result = mysql_query($query);
        ?>              
        <select id="selectCard" class="dropdown" onchange="populateData($id)">
            <?php
                while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
            ?>  
            <option value="<?php echo $line['source_name'];?>"> <?php echo $line['source_name'];?>
            <?php
                $id = $line['source_id'];           
                $source_name = $line['source_name'];
                $source_email = $line['source_email'];
                $phone_no = $line['source_id'];
                $disteller_function = $line['disteller_function'];
                }
            ?>
        </select>               
        <P><INPUT ID="to_address" TYPE="TEXT" NAME="to_address" SIZE="25"></P>    
        <P><INPUT ID="subject" TYPE="TEXT" NAME="subject" SIZE="25"></P>         
        <P><INPUT ID="email_content" TYPE="TEXT" NAME="email_content" SIZE="500"></P>         
        <P><INPUT TYPE="SUBMIT" VALUE="Submit" NAME="B1"></P>                       
    </body>

When am passing ‘$id’ as parameter, the ‘populateData($id)’ function is not getting called.And When i remove the parameter the code works but i get ‘undefined’ in rest of the form fields.

Any help is much appreciated.

Nanashi

  • 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-13T19:04:36+00:00Added an answer on June 13, 2026 at 7:04 pm

    First of all, it seems weird that you pass the parameter $id to populateData() since you are not using it anywhere inside that function. Maybe you should recheck your code and see if it matches your design, what you really want to do and how it should be done.

    The reason your call is not working is PHP variables must start with $ but Javascript variables can’t and most importantly: You can’t just have a reference to a server side PHP script variable on your client side Javascript code. You need to have your PHP server side script echoing (outputting) the value of your PHP script variable $id as the parameter id on a function call of the HTML+Javascript code which the user’s browser will receive and run client side (can be done without the single quotes if $id is a number and you want to pass it as a numeric value to populateData()).

    Just remove the $ from your parameter $id on the function declaration and set it between PHP tags inside single quotes to echo it as a string parameter to the function calling on the HTML output:

    function populateData(id){
    ...
    }
    ...
    <select id="selectCard" class="dropdown" onchange="populateData('<?php echo $id; ?>')">
    

    Also I recommend you testing with Javascript console opened in the browser (i.e. Web Developer -> Web Console or CTRL+SHIFT+K in Firefox), which can help you track future bugs like this on the fly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form which has a select box with name of cities from
I have written a simple CRUD form which has one select list. However the
I have a form which has multiple input fields, and 3 main options. depending
I have a UserForm class which has a select list populated from a related
I have a form in which the user can either select from a pre-existing
I have a page with a form that contains five selects (drop-down lists) which
I have a form with set of dropdown fields, each of which has a
I have a lightbox/modal that displays a form, but in select tag has a
I have a form which has several tags. is this possible: <select name=select1 id=select1>
I have a form to add contacts into a database, which has a select

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.