Jump to content

Getting a selected ID


gerkintrigg

Recommended Posts

I'm making an ajax query to allow my select box to parse a selected id to an ajax query using this code:

 

<select name="top_cat<?php echo $r['product_id'];?>" id="top_cat<?php echo $r['product_id'];?>" onchange="javascript: MyAjaxRequest('sub_cat_selector<?php echo $r['product_id'];?>','sub_cat_selector.php?id='+top_cat<?php echo $r['product_id'];?>.value);">

 

It doesn't work and I think I may be making a very silly mistake.

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/274056-getting-a-selected-id/
Share on other sites

this.value is easier.

'sub_cat_selector.php?id='+this.value

Have you checked that sub_cat_selector.php is being invoked? With the right ID? And is correctly doing whatever it's supposed to be doing?

 

[edit] Oh, and remove the "javascript:" from the onchange. That's only for link hrefs. onchange contains Javascript code already, you don't have to tell it again.

And actually that may be the problem...

 

[edit 2] Oh, and try to avoid inline Javascript. If you have something like jQuery then you can quite easily do a

<select name="top_cat<?php echo $r['product_id'];?>" id="top_cat<?php echo $r['product_id'];?>" class="top_cat">

$(function() {
    $("select.top_cat").change(function() {
        MyAjaxRequest(this.id.replace(/top_cat/, "sub_cat_selector"), "sub_cat_selector.php?id=" + this.value);
    });
});

the sub cat selector is being invoked... I ended up doing it by href in the end (it's only for a back end system) but it's rather annoying that I can't get it working as a jump menu... I did it before with another form, but this is playing up.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.