dmcoding dmcoding - 6 months ago 21
Ajax Question

PHP, Javascript, mysql, and selection lists

I'm working on a piece of some software that will grab information from a mysql database and throw it onto our form dynamically. I'm running into a couple problems, though. I'll give a quick rundown of some functionality.

When the form loads, we have a ton of selection lists. These are all populated through arrays with various keys/values in php. When I select an option from one list, we'll call it a "customers" list, on-click I need to check if that customer has a special flag (stored in the database), and update another selection list based on that data.

How I understand the core of my solution is I need to have a javascript trigger on-click, which I have. The function that is called references a php page that handles the database query through a class and it's function.

<script>
function setService()
{ // The customer's "id" grabbed from the aforementioned customer selection list
customer = $('#customer').val();
$.get('thePage.php?key=setService?customer='+customer);
}
</script>


This function then talks to my php. The CustomerProvider class works 100%. I have tested that thoroughly on other pages. The problem arises when I try to actually get my selection list to change.

<?
if(isset($_GET['key']) && $_GET['key'] == 'setService')
{
$customer = $_GET['customer'];
$customer = intval($customer);
$s = CustomerProvider::getHasContract($customer);
if ($s != '')
{ ?> <script>var element = document.getElementById('ticket_service');
element.value = 'Contracted Hours';</script> <? }
else return;
}
?>


I'm coding in javascript literally for the first time ever and they kinda just threw me on this project. I know that my portion isn't being read as html or output as I intend. I know that every other part of the php and the first bit of javascript seems to be executing okay. Any help would be incredibly appreciated.

Answer

You seem to be on the right track but just for your own sanity here are a couple pointers. You shouldn't be returning Javascript from PHP for a situation like this. Instead you should be relying on Javascript promises to wait for a response containing just the data and continue the execution of your client code once you have your values returned. Take a look at this:

<script>
  function setService() { // The customer's "id" grabbed from the aforementioned customer selection list
    customer = $('#customer').val();
    $.get('thePage.php?key=setService?customer=' + customer, function(data) {
      console.log(data + ' was returned from your php script!');
      if(data.hasContract=='1')
          $('#ticket_service').val('Contracted Hours');
      else
          $('#ticket_service').val('No Contracted Hours');
    });
  }
</script>

And then your PHP script will just look like this:

<?
if(isset($_GET['key']) && $_GET['key'] == 'setService')
{
    $customer = $_GET['customer'];
    $customer = intval($customer);
    $s = CustomerProvider::getHasContract($customer);
    if ($s != ''){
        $hasContract = 1;
    }
    else 
        $hasContract = 0;

    echo json_encode(array('hasContract' => $hasContract));
} 
?>

Therefore returning only the data needed for the client app to continue... not application logic