PHP, Javascript, mysql and selection lists

I am working on some software that will capture information from a mysql database and dynamically output it to our form. However, I have a password problem. I will briefly talk about some of the features.

When the form loads, we have a ton of pick lists. All of them are filled with arrays with different keys / values ​​in php. When I select an option from one list, we will call it a "list of clients", when I click, I need to check whether this client has a special flag (stored in the database) and updates another selection list based on this data.

As far as I understand the essence of my solution, I need to have the javascript trigger that I have. This function is called a link to the php page, which processes the database request through the class and its function.

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

This function then goes to my php. The CustomerProvider class works 100%. I checked this on other pages. The problem occurs when I try to change the selection list.

<?
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 am literally coding in javascript for the first time, and they just threw me into this project. I know that my part is not readable as html or output, as I assume. I know that every other part of php and the first bit of javascript seem to work fine. Any help would be incredibly appreciated.

+4
source share
4

, , , . Javascript PHP . Javascript promises, , , . :

<script>
  function setService() { // The customer "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 !');
      if(data.hasContract=='1')
          $('#ticket_service').val('Contracted Hours');
      else
          $('#ticket_service').val('No Contracted Hours');
    });
  }
</script>

PHP script :

<?
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));
} 
?>

, , ...

+4

PHP script. , - DOM, .load(), $.get.

$("#someelement").load('thePage.php?key=setService?customer='+customer);

<div id="someelement">. <script>, script.

, - script, $.getScript() $.get. Javascript, HTML, <script>.

0

, . JavaScript , . :

test.php, .

$.get( "test.php" );

, $.getJSON(). , . JS. .

PHP

<?
if(isset($_GET['key']) && $_GET['key'] == 'setService')
{
    $customer = $_GET['customer'];
    $customer = intval($customer);
    $s = CustomerProvider::getHasContract($customer);

    // Default output
    $output = array('hasContract' => false);

    // Customer has contract
    if ($s != '')
        $output['hasContract'] = true;

    echo json_encode($output)

    // Alternative: PHP just returns getHasContract, JS determines action
    // (this would replace $ouput, conditional, and echo)
    // echo json_encode(array("hasContract" => $s));
} 
?>

JavaScript

function setService() 
{   // The customer "id" grabbed from the aforementioned customer selection list
    customer = $('#customer').val();
    $.getJSON('thePage.php?key=setService?customer='+customer, function(result) {
        // Alternative
        // if (result.hasContract != "")
        if (result.hasContract)
        {
            var element = document.getElementById('ticket_service');
            element.value = 'Contracted Hours';
        }
    });
}
0

, GET. "ticket_service" , , , , / , /, JS/Jquery. GET pro POST, , POST:

JS:

function postSomthing(customerID){
       $.post(
        'thePage.php',
        {key:'setService',customer:customerID},
        function(data){ 
         if(data!='x'){
         $('#ticket_service').val(data)
         }
         else{alert('no ticket');/*whatever you want to do*/}
        });
}

PHP (thePage.php):

if(isset($_POST['key']) && $_POST['key'] == 'setService'){
    $customer = intval($_POST['customer']);
    $s = CustomerProvider::getHasContract($customer);
    if ($s != ''){echo 'x';/* false, or whatever you want*/}
    else{echo 'Contracted Hours';}
} 

:

  • "ticket_service" , .
0

All Articles