1

What I want to be able to do is to:

--- have an "on change" on drop list (containing the table data "titles")

--- this on change runs the javascript function

--- the javascript populates a text area with a "message" column of data in the same table as the corresponding title that is being selected in the drop list.

<li><label for="frm_precan">Canned Response</label>
            <span class="input">                        
                    <select id="frm_precan" name="precan" onchange="updateText();">
                            <option value="">--Please Select--</option>
                            <?php foreach($precan_list as $precan) : ?>
                            <option value="<?=$precan['id'];?>"><?=$precan['name'];?></option>
                            <?php endforeach; ?>            
                    </select>
            </span>
        </li>
        </ul>   
        <textarea style="width: 100%; margin: 0; padding: 0; border-width: 1; font-family: courier;" name="message" rows="10" id="text_area"></textarea>


        <script type="text/javascript">
        function updateText()
            {
            var value = $("#frm_precan option:selected").text();
            $('#text_area').val(value);;
            }
        </script>

This is the code, and it currently can put the selected title in the text area. However, I need to get data from database according to selected index of the dropdown and fill the textbox with it. How can I do this?

3
  • your question isn't clear, can you further explain what you want is? Commented Jun 29, 2012 at 11:03
  • sorry im having trouble explaining what i need to do. the function i am building is essentially a precanned response function. so you would select a drop down list that currently contains a for each loop populated with an sql tables name field. as it is, the code can populate the textarea with this field, however i wish to use the selected name field as a WHERE condition i guess and display a different table column such as message, WHERE name or title = the title selected in the drop down box?? sorry if this doesnt make sense. Commented Jun 29, 2012 at 11:09
  • so you want to fill a textbox according to selected index of the dropdown menu. also, this filling is done with the matching database row. am i right? Commented Jun 29, 2012 at 11:12

1 Answer 1

1

You cannot fill the textbox with pure JavaScript. JavaScript works client-side and you need to access the database on a change of selection in the dropdown menu. For that, you need to recall the same page on a dropdown selection change. After you get the data from database, it's easy to create a textbox with the data with "echo" command.

<?
if (isset($_POST['my_selection'])) {
    $result=mysql_query("SELECT * FROM `table` WHERE `column`='".mysql_real_escape_string($_POST['my_selection'])."'");
    $row=mysql_fetch_array($result);
    echo "<input type='text' value='".$row['column']."'>";
    echo "<input type='text' value='".$row['column1']."'>";
}
?>
<select name='my_selection'>
Your existing select box here...
Sign up to request clarification or add additional context in comments.

2 Comments

OK thanks. I am sorry but i am still not following how to call this from an on change property on the drop list, and i am aware that this wants to go to a javascript function that presumably calls the php method obtaining the SQL stuff.
place the "select" into a form and enter the file's name into it's "action" tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.