0

Hello i am new to javascript. I need to return multiple values from the table. Currently jscript returning only the first value from the table.

My javascript

<script>
jQuery(document).ready(function($){
    $("#countryDropDown").change(function()
    {
        var country=$(this).val();
        function fillStateDropDown(countryStateOption)
        {
            $.each(countryStateOption, function(val, text) {
                $('#stateDropDown').append(
                $('<option></option>').val(val).html(text));
            });
        }
        function clearStateDropDown()
        {
            $('#stateDropDown option:gt(0)').remove();
        }
        if(country=='Art1')
        {
            clearStateDropDown();
            var indiaStateOption={
                '<?=$row['service']?>' : '<?=$row['service']?>',
            };
            fillStateDropDown(indiaStateOption);
        }

php code

<?php
    $select=mysql_query("select * from service where status=1") or die(mysql_error());
    while($row=mysql_fetch_array($select)){
?>

Table values

A | b | v | v |

Currently returning the first value A but I need to print all the values. Thanks

Mörre
  • 5,699
  • 6
  • 38
  • 63
user2468312
  • 361
  • 2
  • 7
  • 21
  • 2
    Javascript only allows you to return 1 value, however that value could be an array which contains all your results – atmd Jan 28 '15 at 12:50
  • Realy, I don't know programming language, that allow to return more than one value from function. – degr Jan 28 '15 at 12:51
  • Looking at that mix of PHP and JS I'm not even sure the question is correct... after indenting the code it looks even worse. Is it possible he's actually asking about PHP??? – Mörre Jan 28 '15 at 12:52

4 Answers4

0

You are trying to output options in a select html element based on the results of a php query?

Firstly here is a rough mockup of what I think you are trying to do http://jsfiddle.net/Lj4rqkpy/

<body>
<select id="countryDropDown">
<option value="Art1">this shows first</option>
<option value="Art1">select me to trigger on change</option>
</select>

<select id="stateDropDown">
<option value="">This will be cleared</option>
</select>
</body>

jQuery(document).ready(function($)
{
    $("#countryDropDown").change(function()
    {
        var country = $(this).val();

        function fillStateDropDown(countryStateOption)
        {
            $.each(countryStateOption, function(val, text)
            {
                $('#stateDropDown').append(
                    $('<option></option>').val(val).html(text)
                );
            });
        }
        function clearStateDropDown()
        {
            $('#stateDropDown').html('');
        }

        if(country == 'Art1')
        {
            clearStateDropDown();
            var indiaStateOption = {
                'testService' : 'testService', 'testService2' : 'testService2'
            };
            fillStateDropDown(indiaStateOption);
        }
    });

    // 
});

Assuming the query returns multiple rows, and you want them to be output inside the indiaStateOption object in your javascript, you can use

<?php
    $serviceJSOutput = '';
    $select=mysql_query("select * from service where status=1") or die(mysql_error());
    while($row=mysql_fetch_array($select)){
        $serviceJSOutput .= "'" . $row['service'] . "' : '" . $row['service'] . "',";
    }
    // remove the last comma
    $serviceJSOutput = rtrim($serviceJSOutput, ",");
?>

and then in the js replace the if statement with

if(country == 'Art1')
{
    clearStateDropDown();
    var indiaStateOption = {
         <?=$serviceJSOutput?>
    };
    fillStateDropDown(indiaStateOption);
}

As an aside I would recommend you move away from using mysql_query in your php if possible, it is depreciated. Use PDO or mysqli.

My answer is filled with assumptions and I haven't had time to test it fully so please do correct me where I have got them wrong.

Ad Wicks
  • 300
  • 3
  • 10
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Nikos Paraskevopoulos Jan 28 '15 at 13:23
  • I have tried to read the code and get a better understanding of what they need. Admittedly I might be wide of the mark but I think it's significantly better than my previous answer. – Ad Wicks Jan 28 '15 at 15:12
0

You can easily use JSON objects return multiple objects in javascript. Return a JSON object from .js script file and retrieve corresponding values in javascript using jquery.

0

return an object.

var result = {};
result.a=20;
result.x='ABC';
return result;

then you can access returned object properties like this:

alert(result.x);
alert(result.a);
Mike
  • 447
  • 4
  • 15
0

You can easily return an array or an object with named keys, javascript's inline creation syntax makes it very easy.

Array example:

return [value1, value2, finalValue];

Object example:

return {
    value1Name: value1,
    value2Name: value2,
    finalValueName: finalValue
}
doldt
  • 4,466
  • 3
  • 21
  • 36