Jump to content

pull data from select box and fill in a input field


rdkurth

Recommended Posts

I'm trying to learn jquery and ajax and not doing so well

I am trying to pass the id from this dropdown when it is selected to the next ajax script and it is not working.

 

Both scripts pull there data from the same table so if there is a way to do this with one script I would be interested.

 <select name="customer"id="customer" class="form-control input-sm responsetext" >
 </select>
$( document ).ready(function() 
{
	
  $.ajax({    //create an ajax request to load_page.php
        type: "GET",
        url: "customer.php",
        dataType: "html",   //expect html to be returned
        success: function(response){                    
            $(".responsetext").html(response); 
            //alert(response);
        }

    });	
	

});
<?php
include("connect.php");
$id = $_SESSION['profile']['id'];
echo "<option value=''>SELECT A CUSTOMER</option>";
foreach($db->query("SELECT * FROM customers WHERE pid = '$id'") as $row) {
   echo "<option value=" . $row['id'] . ">" .$row['name'] . "</option>";
 }

The above script creates the select box and it works.

Now I need the $row['id'] To be picked up by the next script to fill in the input field.

 <input name="contact"  class="form-control input-sm contactresults" id="contact">
$(function() {  //  document.ready
    $("#id").on("change", function() {
        $.ajax({
            url: "contact.php",
            type: "GET",
            data: {
                id: $(this).val()
            },
            success: function(data) {
                $(".contactresults").html(data);
            }
        });
    });
});
<?php
include("connect.php");
$id= $row['id'];
echo "<option value=''>SELECT A CUSTOMER</option>";
foreach($db->query("SELECT * FROM customers WHERE id = '$id'") as $row) {
    echo  $row{contact"};
}
  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.