1

I'm a newbie so my apologies if this is just dumb question.

I have been struggling for 2 days using option tag as links on ajax call. I have gone through many tutorials but can't find any solution related to my work. I have OPTION list inside a SELECT tag for pagination...

The code for the SELECT tag looks like something this:

         <select onchange="_change(this);">
                    <?php
                    $i = 1;
                    while ($lastpage >= $i):
                        ?>
                        <option  value="<?php echo $i?>"><?php echo $i; ?></option>
                        <?php
                        $i++;
                    endwhile;
                    ?>
                </select>

The above Html code is working properly and showing the page numbers according to per page record....

Possible jQuery code for the SELECT tag Function is...

     function _change(id)
     {
            alert(id);
            $.ajax({
                Type: "GET",
                url: "index.php?pn="+id.value,
                data: id.value,
                success: function (result) {
                    $("pre").html(result);
                }
            });
        };

I have prompted the id to get to know about it what am actually receiving... I am able to get the id on ajax call but unable to load the data and the path remain same in the address bar..

The php code on ajax call is something like this....

 if(isset($_GET['pn'])){
    $id=$_GET['pn'];
    echo $id." is id";
    my_func();//Calling Php function
  }

I am calling php function which loads the data from the database but that's not working and giving out a fata error saying Call to undefined function my_func();

Thanks in advance

related links I have search so far...

using href links inside <option> tag

Community
  • 1
  • 1
Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50

3 Answers3

2
function _change(id)
    {
        alert(id);
        $.ajax({
            Type: "GET",
            url: "index.php?pn="+$(id).val(),
            data: $(id).val(),
            success: function (result) {
                $("pre").html(result);
            }
        });
    };

Use this modified code. You have to pass selected value using .val() function.

user3136884
  • 161
  • 2
  • 9
1

Change

url: "index.php?pn="+id.value,
data: id.value,

to

url: "index.php?pn=" + $(id).val(),
data: $(id).val(),

Update:

By the way making an ajax call won't change the url, instead of the ajax call you need document.location.href = 'http://' + window.location.hostname + window.location.pathname + "?pn=" + $(id).val()

artm
  • 8,554
  • 3
  • 26
  • 43
  • The address bar still remain same and still getting the error about Fatal error: Call to undefined function my_func() – Basheer Kharoti Nov 14 '14 at 09:34
  • @user2736704 ` Call to undefined function my_func();` is something you need to fix on the server, what is `my_func()`, does it exist? – artm Nov 14 '14 at 09:36
  • that my_func() executes on page load and loads the data from database – Basheer Kharoti Nov 14 '14 at 09:41
  • @user2736704 Try the code in the update, instead of the ajax call. – artm Nov 14 '14 at 09:42
  • I don't want to reload the whole page instead I want to load the specific data from databse and also I want to call a php function onchange execution. – Basheer Kharoti Nov 14 '14 at 09:55
  • @user2736704 Well, you can't change the url without refreshing the page. You need to select one, either you change your server side to work with the ajax call but don't update the url, or you don't use ajax and update your url. – artm Nov 14 '14 at 09:57
  • But why my function isn't working on ajax call...??? I have used the function function_exist that return false n says that function doesn't exists.. I have declared function such like this $_load=true; if($_load){ function my_func() {//Code }} – Basheer Kharoti Nov 14 '14 at 10:08
  • Try setting `$_load = true;` before calling `my_func` – artm Nov 14 '14 at 10:17
  • thanks for helping.... so at last could you please give reply back to me that why Data isn't loaded back in HTML element using foreach loop while function executed successfully..?? – Basheer Kharoti Nov 14 '14 at 11:13
  • @user2736704 I'm not sure what you mean but if you're talking about the php code, that sounds like a new question. I don't know php so can't really help you there. – artm Nov 14 '14 at 11:16
1

No error in your ajax call if you can see it in your developper tools (f12 on chrome / network tab)

And you said :

and giving out a fata error saying Call to undefined function my_func();

So do you have a function named my_func ?

Benjamin Poignant
  • 1,066
  • 8
  • 18
  • I have function that executes when page loads but when I call it by ajax that gives Fatal Error saying Call to undefined function my_func() – Basheer Kharoti Nov 14 '14 at 09:40