3

I have the following view:

<form action=<?=$target;?> method="post" name="editForm">
    <div id="page1">
        <select id="categories" name="category" onmouseover="populateCategory(<?=$result?>, <?=$id?>)" >
        </select>
    </div>
</form>

which utilizes the following javascript function on the select tag:

function populateCategory(subCategories, id) {

    var select = document.getElementById(id);

    var opt = document.createElement("option");

    opt.text = "--Please make a selection--";

    for (i = 0; i < subCategories.length; i++) {
        var opt = document.createElement("option");

        opt.value = i;
        opt.text = subCategories[i];

        select.add(opt);
    }
}

The arguments passed to the function are the following variables:

 $result = $this->getCategories(0);
$id = 'categories';

which calls the following function

public function getCategories($parent, $published = 1) {

        //Sanitize Params...    

        //$sanParent = $this->db->getEscaped($parent);
        //$sanPublished = $this->db->getEscaped($published);

        $query = 
        "SELECT * FROM #__adsmanager_categories 
        WHERE parent = " . $parent .
        " AND published = " . $published;

        $this->db->setQuery($query);
        $result = $this->db->loadAssocList();

        var_dump($parent);

        if (!$result) {
            return false;
        }

        return $result;

    }

The low-down:

The last function I posted makes a request to a database, obtaining a list of categories which are intended to be used to create a dropdown list. I thought that if I passed the obtained categories to the JavaScript function, that I may be able to populate it the easy way. Am I doing this wrong? I have a feeling it's a JavaScript issue, as the function call doesn't even seem to happen. When I open the browser debugger, for example, there is no indication of whether or not it was called.

update:

Here is the what the html-source lists:

<script type="text/javascript">

function populateCategory(subCategories, id) {



    var select = document.getElementById(id);



    var opt = document.createElement("option");



    opt.text = "--Please make a selection--";



    for (i = 0; i < subCategories.length; i++) {

        var opt = document.createElement("option");



        opt.value = i;

        opt.text = subCategories[i];



        select.add(opt);

    }

}

</script>



<form action=/index.php?option=com_adsmanager&amp;task=write&amp;Itemid=1 method="post" name="editForm">

    <div id="page1">

        <select id="categories" name="category" onmouseover="populateCategory(Array, categories)" >

        </select>

    </div>

</form>
7
  • Where are you declaring this variable? subCategories It is in your JS function. Commented Nov 22, 2011 at 5:06
  • What does your question do with AJAX? Or the tag was added by mistake? Commented Nov 22, 2011 at 5:07
  • I have a feeling it's a JavaScript issue - if you have such a feeling, you have to poist here a JS code, not PHP code. Commented Nov 22, 2011 at 5:09
  • I did post the JS code. If you look at the first function I listed, and read it, you'll notice it's JavaScript. ;) As far as the AJAX reference, that was a mistake on my part. Editing for clarity. Commented Nov 22, 2011 at 5:11
  • @Lance that's fixed. I tried rectifying that in my code, and it still didn't do anything unfortunately. Commented Nov 22, 2011 at 5:14

1 Answer 1

2

I't not a javascript issue but rather understanding issue.

which calls the following function <- here is your mistake.

JS cannot call PHP functions. As it's run in the browser, while PHP being run on the server, a thousand miles away.
Quite contrary, its indeed PHP creates all the JS along with HTML and stuff.

So, your PHP code have to create the JS function call already populated with data.
Have you checked the HTML source? Does it contain proper JS function call?

Update See, as soon as you posted the actual JS, it become as simple as an egg

<select id="categories" name="category" onmouseover="populateCategory(Array, categories)" >

it says just "Array" instead of actual data.
Means you are trying to print an array as a strings. Which obviously won't work.
Use some loop to iterate that array and print it's contents in the appropriate (for JS) format.

Sign up to request clarification or add additional context in comments.

6 Comments

I never said JS was calling php functions. I said the variables, which I declared at the top of the view, are calling php functions. If you read what I said more closely, you'll notice it's the html which is calling the JavaScript, which in turn is accepting two php arguments
okay, okay. So, did you check the HTML source? Does it contain proper JS function call?
Well, it seems you still don't quite understand the difference between PHP and JS. In fact, it's different programs and you simply can't pass PHP arguments to a JS function. You have to create whole JS from PHP. Including arguments as well
whatever JS format you like - JSON, new Array... BTW, why don't you want to populate this list already in PHP, without employing JS at all?
The reason why is that I have to populate a secondary list which is based on whichever category the user chooses. The secondary list is a list of subcategories. For example, if the user chooses "Vehicle" some subcategories listed would be "automotive", "airplane", etc. Anyway, thanks for the help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.