JavaScript ajax call to populate form fields from database query when select value changes ajax call populate dropdown,ajax call populate table,ajax call

I have been looking through the questions on here and cant find an exact answer to what i am after :( but i have managed to get something.

i have a form select field which i populate from a db query

<select style="width:100%;" class="quform-tooltip chosen-select" id="company_select" name="company_select" title="Company Select" onChange="showUser(this.value)">
<option value="">Please select</option>
<?php
$userID = $user->getUserID();
$query = $user->database->query("SELECT * FROM tbl_businesses as business LEFT JOIN tbl_user_businesses as biz_user ON business.businessID = biz_user.businessID WHERE biz_user.userID ='$userID'");

while($row=$user->database->fetchArray($query))
{
    $bizID = $row['businessID'];
    $bizName = $row['businessName'];
    echo "<option value='$bizID'>$bizName</option>";
}?>
</select>

and then there are currently 2 other textboxes (might increase eventually) which i want to populate when the above select box value is changed/selected

<input id="company_name" type="text" name="company_name" value="" />
<input id="company_email" type="text" name="company_email" value="" />

so i have an onchange function on my select box which is this

<script>
function showUser(str)
{
if (str=="")
{
    document.getElementById("company_name").innerHTML="";
    return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var data = JSON.parse(xmlhttp.responseText);
        for(var i=0;i<data.length;i++) 
        {
          document.getElementById("company_name").innerHTML += data[i].id + ' - ' + data[i].name + ' - ' + data[i].web;
        }
    }
}
xmlhttp.open("GET","formdata.php?q="+str,true);
xmlhttp.send();
}
</script>

and my formdata.php file is like so

    <?php
include("include/user.php");

$q = intval($_GET['q']);

$sql="SELECT * FROM tbl_businesses WHERE businessID = '".$q."'";

$result = $user->database->query($sql);
$info = array();
while($row=$user->database->fetchArray($result))
{
    $cID = $row['bussinessID'];
    $cName = $row['businessName'];
    $cWeb = $row['businessWebsite'];
    $info[] = array( 'id' => $cID, 'name' => $cName, 'web' => $cWeb );
}
echo json_encode($info);?> 

which is making the ajax call correctly and returning the data expected but i now need help to populate the textbox values?

can anyone please help me with this, have literatly spent ages trying to figure it out, im not familiar with javascript/json so not sure where to begin

i want the company_name textbox value to be set to $cName; and company_email textbox value to be set to $cWeb;

appreciate any help

Luke

Answer:1

ok the solution that i used, for anyone else wanting to know how i solved it is

my index.php which contains the javascript and the form code

javascript code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>

<script>
function showUser(str)
{
if (str=="")
{
    document.getElementById("company_name").value="";
    return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var data = JSON.parse(xmlhttp.responseText);
        for(var i=0;i<data.length;i++) 
        {
          document.getElementById("company_name").value = data[i].name;
          document.getElementById("company_email").value = data[i].web;
        }
    }
}
xmlhttp.open("GET","formdata.php?q="+str,true);
xmlhttp.send();
}
</script>

and the form code

    <select style="width:100%;" class="quform-tooltip chosen-select" id="company_select" name="company_select" title="Company Select" onChange="showUser(this.value)">
<option value="">Please select</option>
<?php
$userID = $user->getUserID();
$query = $user->database->query("SELECT * FROM tbl_businesses as business LEFT JOIN tbl_user_businesses as biz_user ON business.businessID = biz_user.businessID WHERE biz_user.userID ='$userID'");

while($row=$user->database->fetchArray($query))
{
    $bizID = $row['businessID'];
    $bizName = $row['businessName'];
    echo "<option value='$bizID'>$bizName</option>";
}?>
</select>

<input id="company_name" type="text" name="company_name" value="" />
<input id="company_email" type="text" name="company_name" value="" />

then my formdata.php

    $q = intval($_GET['q']);

$sql="SELECT * FROM tbl_businesses WHERE businessID = '".$q."'";

$result = $user->database->query($sql);
$info = array();
while($row=$user->database->fetchArray($result))
{
    $cID = $row['businessID'];
    $cName = $row['businessName'];
    $cWeb = $row['businessWebsite'];
    $info[] = array( 'id' => $cID, 'name' => $cName, 'web' => $cWeb );
}
echo json_encode($info);?> 

thats it, thanks to charlietfl for your help!

hope this helps someone :)

Answer:2

This is how I am getting current date, dd-MMM-yyyy format. How do I subtract 1 week. var m_names = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "...

This is how I am getting current date, dd-MMM-yyyy format. How do I subtract 1 week. var m_names = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "...

  1. subtract week from date
  2. subtract week from date excel
  3. subtract week from date python
  4. subtract week from date sql
  5. subtract week from date javascript
  6. subtract week from datetime c#
  7. subtract week from date r
  8. subtract week from date c#
  9. subtract week in excel
  10. subtract a week from datetime python
  11. mysql subtract week from date
  12. php subtract week from date
  13. java subtract week from date
  14. subtract one week from date excel
  15. subtract one week from date javascript
  16. vba subtract week from date
  17. subtract 1 week from datetime python
  18. javascript subtract week from current date
  19. excel subtract 1 week from date
  20. javascript subtract 1 week from date

I am trying to get a attribute value from my dom element, but i am getting result as: Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr' I am not able to get the attribute ...

I am trying to get a attribute value from my dom element, but i am getting result as: Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr' I am not able to get the attribute ...

  1. unable to return dimension attribute value

I have the following; var room = function(){ this.entities = new Array(); } var myRoom = new room(); I also have a bunch of entites like this; var entity = function(){ this.title = "A ...

I have the following; var room = function(){ this.entities = new Array(); } var myRoom = new room(); I also have a bunch of entites like this; var entity = function(){ this.title = "A ...

  1. filter array objects javascript
  2. filter array objects angular 6
  3. filter array objects
  4. filter array objects js
  5. filter array objects swift
  6. filter array objects jquery
  7. filter array objects angularjs
  8. filter array objects lodash
  9. filter array objects angular2
  10. array filter objects php
  11. filter array of objects by property
  12. filter array of objects typescript
  13. filter array of objects javascript es6
  14. filter array of objects by property javascript
  15. filter array of objects javascript by key
  16. filter array of objects by key
  17. filter array of objects by key value
  18. filter array of objects javascript by value
  19. filter array of objects based on property
  20. filter array of objects javascript based on property

Apologies if this sounds totally stupid but I'm a total dunce when it comes to coding! I'm trying to make a higher or lower guessing game using a deck of cards (i.e. The player guesses if the next ...

Apologies if this sounds totally stupid but I'm a total dunce when it comes to coding! I'm trying to make a higher or lower guessing game using a deck of cards (i.e. The player guesses if the next ...