Jump to content


Photo

Getting a selected ID

ajax

  • Please log in to reply
2 replies to this topic

#1 gerkintrigg

gerkintrigg

    Book Publisher

  • Members
  • PipPipPip
  • 817 posts
  • LocationPlymouth, UK
  • Age:33

Posted 05 February 2013 - 07:39 AM

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?
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,238 posts
  • LocationWA

Posted 05 February 2013 - 03:34 PM

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 "j&#097;v&#097;script:" from the onchange. That's only for link hrefs. onchange contains J&#097;v&#097;script 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);
    });
});

Edited by requinix, 05 February 2013 - 03:37 PM.


#3 gerkintrigg

gerkintrigg

    Book Publisher

  • Members
  • PipPipPip
  • 817 posts
  • LocationPlymouth, UK
  • Age:33

Posted 06 February 2013 - 02:50 PM

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.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com