0

I have a 2 select elements with an id of ticket_customer and ticket_contact

This JQuery code is on the same page:

$("#ticket_contact").change(function() {
    val = $(this).val();
    var html = $.ajax({
        url: "?getCustomerSequence=1&sequence="+val+"",
        async: true,
        success: function(data) {
            $('#ticket_customer').val(data);
        }////////////function html////////
    })/////////function ajax//////////
});

so when i change selection, it will run this code:

if($_GET["getCustomerSequence"] == '1') {
    echo '1';
    exit();
}

which just returns 1 with absolutely nothing else on the page

i want to be able to select the option with the value of 1 (which is returned in the HTML) but its not selecting anything

there is an option in the #ticket_customer select element with the value of 1

I have tried doing an alert(data); in the JQuery in the success part which returns 1 too.

alternatively, rather than returning the HTML, would there be a way to return an array in PHP and read this in using JQuery and then getting the correct value from the PHP array using JQuery

charlie
  • 415
  • 4
  • 35
  • 83
  • You might be better off having the PHP return a JSON string instead of just plain text. You'll also be able to include and parse more information. – Lix Nov 20 '15 at 15:44
  • but why you need to use ajax while you can change the selection of select 2 directly after change selet1 ?? – Mohamed-Yousef Nov 20 '15 at 15:46
  • its getting live data so i dont want to refresh the screen if there is new data available. @Lix i can return a JSON string in PHP but how do i access this from the JQuery? – charlie Nov 20 '15 at 15:47
  • @charlie - you'll want to include a [`dataType `](http://api.jquery.com/jquery.ajax/) attribute to your `$ajax` function and specify 'json' - then in your success callback, you can access it like a normal JSON object: `data.whatEverYouWant`. For the PHP, just echo a `json_encode` function – Lix Nov 20 '15 at 15:50
  • 1
    in the PHP, i guess i will need to echo the json_encode($return) part? – charlie Nov 20 '15 at 15:51
  • can you post an answer please then i can accept! :) also, where does the json need to be specified? (sorry im not great with jquery - more of a PHP person :) ) – charlie Nov 20 '15 at 15:52

1 Answers1

4

This can be seen here: use jquery to select a dropdown option

In your success function, do:

success: function(data) {
    $('#ticket_customer > option:eq(' + data + ')').prop('selected', true));
}
Community
  • 1
  • 1
Twisty
  • 30,304
  • 2
  • 26
  • 45