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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:49:18+00:00 2026-06-09T04:49:18+00:00

I am working on this project in which I am trying to get a

  • 0

I am working on this project in which I am trying to get a returned value so I can autofill my input boxes according to what the client selects.

This code however is not executing and I do not know why. When I remove the src="jquery area" $(#dropdown).on is an undefined method; not to sure what to do.

<script  type="text/javascript"  src="http://code.jquery.com/jquery-latest.min.js">
//$.post(url, [data], [callback], [callback type])

    ("#dropdown").on('change', function() {//when you select something from the dropdown function run and will switch the data
        $.post("backgroundScript.php", {
                uid: $(this).val()
            },
             function(data) {
                 $("#first").val(data.first);
               $("#last").val(data.last);
               // etc.;
            }, 'json'
        );

    });
</script>

Here’s my full code

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH->query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult->setFetchMode(PDO::FETCH_ASSOC);

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option value='{$row['first']}'>{$row['first']}</option>";
 // echo getLN();

}

$dropdown .= "\r\n</select>";

echo $dropdown;

//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?>
<!-- javascript on client-side -->
<script  type="text/javascript"  src="http://code.jquery.com/jquery-latest.min.js">
//$.post(url, [data], [callback], [callback type])

    ("#dropdown").on('change', function() {//when you select something from the dropdown function run and will switch the data
        $.post("backgroundScript.php", {
                uid: $(this).val()
            },
             function(data) {
                 $("#first").val(data.first);
               $("#last").val(data.last);
               // etc.;
            }, 'json'
        );

    });
</script>


<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">

/*("#dropdown").on('connection', function (stream) {
  console.log('Ah, we have our first user!');
});*/</script>

<form action="insert.php" method="post">
First Name: <input type="text" id="first" name="first"><br>
Last Name: <input type="text" id="last"><br>
Phone: <input type="text" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>

here is my new edited script on output page =

<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>    

//$("#dropdown-parent").on('change','#dropdown', function() { // replace dropdown-parent
    $("#contacts").on('change','#dropdown', function() {

        $.post("backgroundScript.php", { 
                uid: $(this).val() 
            }, 
             function(data) { 
                 $("#first").val(data.first); 
                 $("#last").val(data.last); 
                 // etc.; 
            }, 'json' 
        ); 
    }); 
</script>

here is the php file for backgroundScript.php =

<?

// background script

// retrieve data based on $_POST variable, set to $returnArray

$returnArray = $_POST[array(
         'first' => firstName,
         'last' => lastName,
)];

/****************************
 * the structure of returnArray should look something like
     array(
         'first' => firstName,
         'last' => lastName,

     )*/
echo json_encode($returnArray);
?>

this file will send in info so the javascript will then replace form fields with what ever is held in the areas appointed

  • 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-09T04:49:20+00:00Added an answer on June 9, 2026 at 4:49 am

    You need to include your jQuery prior to using it:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
      // Your Code Here
    </script>
    

    Better yet would be to use external JS:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" src="js/site.js"></script>
    

    And if you’re using HTML5 the type="text/javascript" isn’t even needed so:

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="js/site.js"></script>
    

    Even better still would be to use a jQuery CDN:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="js/site.js"></script>
    

    Also, as others have noted, be sure to use $ at the beginning of your jQuery factories. i.e. $('#dropdown')

    — Update —

    Further clarification on project tree, most basic project trees look like this:

    root/
     |--css/
     |--images/
     |--js/
        |--site.js
     |--index.html
    

    — Update 2 —

    Example of a $.post

    $.post({
        'somescript.php', // Script your posting to
        { 
            someParam1: someData1, // $_POST['someParam1']
            someParam2: someData2
            // etc etc
        },
        function(response){
            // Do something with JSON response upon successful post
            alert(response);
        },
        'json' // Tells the script that JSON will be returned
    });
    

    — Update 3 —

    Okay so basically you want to do is…

    Javascript:

    var dropdown = $('#dropdown');
    
    dropdown.bind('change', function(){
        $post.(
            'backgroundScript.php', 
            { 
                first: dropdown.val() 
            },
            function(response) {
                $('#first').val(response.first);
                $('#last').val(response.last);
                // Repeat for all of your form fields
            },
            'json'
        );
    });
    

    Receive POST param:

    $firstName = $_POST['first'];
    

    MySQL query would be something like the following:

    $sth = $dbh->prepare('SELECT *
        FROM contacts
        WHERE first = :first');
    $sth->bindParam(':first', $first, PDO::PARAM_STR);
    $sth->execute();
    

    Then add all of your MySQL fields into associative array array(key => value) and then json_encode and return array.

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

Sidebar

Related Questions

I'm working on this project where the client has a virtual server setup. I
I've been trying to get NHibernate into development for a project I'm working on
I'm trying to get our build scripts (which use MSBuild) working correctly on Vista
I have been trying to get xapian working django haystack for a project im
I am working on a mini-project which consists of doing a client-server chat application.
I am trying to get a simple helloworld test project going so I can
I'm working my first internship, still trying to get this horrible thing to compile
Basically trying to get the default sample project (which is linked to here )
I'm trying to get started with an iPhone project. The book I am working
So I'm working on a personal project (trying to get better at c++), and

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.