2

I have a simple load more style script that works fine on the index page, where only one parameter is sent via ajax

    $(function() {//When the Dom is ready
    $('.load_more').live("click",function() {//If user clicks on hyperlink with class  name = load_more
        var last_msg_id = $(this).attr("id");//Get the id of this hyperlink this id indicate the row id in the database
        if(last_msg_id!='end'){//if  the hyperlink id is not equal to "end"
            $.ajax({//Make the Ajax Request
                type: "POST",
                url: "index_more.php",
                data: "lastmsg="+ last_msg_id,
                beforeSend:  function() {
                    $('a.load_more').html('<img src="loading.gif" />');//Loading image during the Ajax Request
                },
                success: function(html){//html = the server response html code
                    $("#more").remove();//Remove the div with id=more
                    $("ul#updates").append(html);//Append the html returned by the server .
                }
            });
        }
        return false;
    });
});

With this HTML/PHP

<div id="more">
 <a  id="<?php echo $msg_id; ?>" class="load_more" href="#">more</a>
</div>

However, I want to add another php variable so that it can also work with particular categories, I have no problems writing the HTML and PHP but I am new to Jquery and struggling to edit the script to include the additional parameter if it is set. This is the HTML that I am thinking of using, just struggling with editing the JQuery

<div id="more"class="<?php echo $cat_id;?>">
<a id="<?php echo $msg_id;?>" class="load_more2" href="#">more</a>
</div>

As always any help is much appreciated!

2
  • What additional parameter are you trying to add? Commented Aug 16, 2011 at 18:35
  • @user866190 Is using an additional class tag in your HTML the only way to pass a second variable? What would you do if you needed to pass three, or more? Commented Jun 19, 2013 at 23:03

4 Answers 4

6

You can set

data = {onevar:'oneval', twovar:'twoval'}

And both key/value pairs will be sent.

See Jquery ajax docs

If you look under the data section, you can see that you can pass a query string like you are, an array, or an object. If you were to use the same method you already are using then your data value would be like "lastmsg="+ last_msg_id + "&otherthing=" + otherthing,

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

Comments

5

You can pass multiple URL params in the data portion of your ajax call.

data: "lastmsg="+ last_msg_id +"&otherparam="+ other_param

On the PHP side, you'd just process these as you already are.

2 Comments

Thanks for the response, I am wondering if I have to create a variable for the additional parameter that is set just like the first one..?
In my answer, you'd have to create other_param and set its value. You can also just pass it in the string like this: data: "lastmsg="+ last_msg_id +"&otherparam=tacos".
3

You can use this code:

$.ajax({//Make the Ajax Request
                type: "POST",
                url: "index_more.php",
                data: {var1: "value1", var2: "value2"},
                beforeSend:  function() {
                    $('a.load_more').html('<img src="loading.gif" />');//Loading image during the Ajax Request
                },
                success: function(html){//html = the server response html code
                    $("#more").remove();//Remove the div with id=more
                    $("ul#updates").append(html);//Append the html returned by the server .
                }
            });

Comments

-1

Try It:

data: JSON.stringify({ lastmsg: last_msg_id, secondparam: second_param_value});

You can add more parameters separating them by comma (,).

Comments

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.