?
Solved

populate textarea and file input from a dropdown -- Ajax and jQuery

Posted on 2010-08-26
7
Medium Priority
?
1,060 Views
Last Modified: 2012-05-10
I need to query a mysql db for two fields: content_value and image_path, depending on the col_name  selected from a dropdown; then I need to populate a textarea and file input with the retrieved values.

Can anyone explain how to manage this using jQuery?
0
Comment
Question by:Glen Gibb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:mstrelan
ID: 33539166
It is not possible to populate a file input via javascript. This is a well documented security precaution.
0
 

Author Comment

by:Glen Gibb
ID: 33539187
How about the textarea then?
0
 
LVL 10

Expert Comment

by:mstrelan
ID: 33539212
Something like this ...
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<textarea id="myTextArea"></textarea>
<select id="myDropDown">
<option value="option_1">Option 1</option>
<option value="option_2">Option 2</option>
</select>
<script>
var myArray = {
  option_1: 'the content value goes here',
  option_2: 'some more content value goes here'
}

$(document).ready(function(){
  $('#myDropDown').change(function(){
    var selection_option = $(this).val();
    $('#myTextArea').val(myArray[selection_option]);
  });
});
</script>

Open in new window

0
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 

Author Comment

by:Glen Gibb
ID: 33539264
That's more what I needed.  My dropdown has the names of the different rows I'm searching for (col_name).  

The SQL needs to be something like:  "SELECT content_value FROM page_content WHERE col_name = (dropdown selected value)".

Where does the db query go in the whole process, and how do I get the data back in the textarea?
0
 
LVL 10

Accepted Solution

by:
mstrelan earned 1500 total points
ID: 33539274
you can use php (or another backend language) to build up the myArray variable before the page load. this can become problematic if there are large amounts of text you need to store. alternatively you need to perform an ajax request. to learn about ajax requests follow this tutorial - http://www.talkphp.com/vbarticles.php?do=article&articleid=58&title=simple-ajax-with-jquery
0
 

Author Comment

by:Glen Gibb
ID: 33543861
I appreciate the model.  However, the example shows how to POST.

How do I retrieve data from the php file:  return $row? echo $row?

Sorry to be so dense but I just don't see the big picture yet.
0
 

Author Closing Comment

by:Glen Gibb
ID: 33544808
The link had the necessary info, along with your example.  Thanks.

(By the way, the result needs to be echoed on the php page.)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question