PHP, Javascript, mysql, and selection lists -
i'm working on piece of software grab information mysql database , throw onto our form dynamically. i'm running couple problems, though. i'll give quick rundown of functionality.
when form loads, have ton of selection lists. these populated through arrays various keys/values in php. when select option 1 list, we'll call "customers" list, on-click need check if customer has special flag (stored in database), , update selection list based on data.
how understand core of solution need have javascript trigger on-click, have. function called references php page handles database query through class , it's function.
<script> function setservice() { // customer's "id" grabbed aforementioned customer selection list customer = $('#customer').val(); $.get('thepage.php?key=setservice?customer='+customer); } </script>
this function talks php. customerprovider class works 100%. have tested thoroughly on other pages. problem arises when try selection list 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 first time ever , kinda threw me on project. know portion isn't being read html or output intend. know every other part of php , first bit of javascript seems executing okay. incredibly appreciated.
you seem on right track own sanity here couple pointers. shouldn't returning javascript php situation this. instead should relying on javascript promises wait response containing data , continue execution of client code once have values returned. take @ this:
<script> function setservice() { // customer's "id" grabbed aforementioned customer selection list customer = $('#customer').val(); $.get('thepage.php?key=setservice?customer=' + customer, function(data) { console.log(data + ' returned php script!'); if(data.hascontract=='1') $('#ticket_service').val('contracted hours'); else $('#ticket_service').val('no contracted hours'); }); } </script>
and php script 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 data needed client app continue... not application logic