0

I am having real problems understanding the logic of passing information from PHP to JS and back. I have been up all night working on this and would really appreciate if someone can show me how to pass two variables to an ajax function.

$assetPath and $count in to the lines commented below. I have tried including them by adding php but in the DOM it shows up as an empty string and I cannot get the desired outcome.

var assetPath ='<?php echo $assetPath; ?>';
var num ='<?php echo $count; ?>';

(function($) {
 $(document).ready(function(){
$('.pager-top,.pager-bottom').bootpag({
    total: assetPath,
    page: 1,
    maxVisible: 5,
    leaps: true,
    firstLastUse: true,
    first: '←',
    last: '→',
    wrapClass: 'pagination',
    activeClass: 'active',
    disabledClass: 'disabled',
    nextClass: 'next',
    prevClass: 'prev',
    lastClass: 'last',
    firstClass: 'first'
}).on("page", function(event, num){
    $.ajax({
            url: "assetPath?pageNumber="+num,
            }).done(function(data) {
            $("#productResults").html( data );
             });
});
})( jQuery );
});
2
  • So $assetPath really have a value inside? Commented Feb 13, 2016 at 16:57
  • assetPath has a url path in it. Seriously this has made me feel stupid. I have not slept yet cause I really want to understand this. Please explain it to me man. the basic concepts of passing data back and forth and ajax. I have been through tons of tutorials and once I think I have it I cant get anything to work. Commented Feb 13, 2016 at 16:57

2 Answers 2

2

Basically, if the Php variable really hold the value and data do exist inside it, then by using this kind of code var assetPath ='<?php echo $assetPath; ?>'; would fine. Before testing into js environment, try echo into page the variables send from server side to see the data do exist or not. And for ajax stuff, to be sure, please used data properties provided for sending data into server side end point, see following example :

var assetPath ='<?php echo $assetPath; ?>';
var num ='<?php echo $count; ?>';

$.ajax({
    type : 'POST',
    url: assetPath,//<-- is it this came from variable assetPath??
    data : {
     pageNumber : num,
    }
}).done(function(data) {
    $("#productResults").html( data );
});

And in server side, assume the assetpath holding value process.php :

 $_POST['pageNumber']; //<-- retrieve data sent from ajax

And if you echo $_POST['pageNumber']; after above code, then this value will available inside success/done block of ajax :

.done(function(data) {
  // data is a data sent from server side
  $("#productResults").html( data );
});
Sign up to request clarification or add additional context in comments.

5 Comments

If course, this will only work if this javascript code is in the .php page itself, not in a separate .js file.
@HaukurHaf : of course mate, otherwise the variable assetPath will printed out <?php echo $assetPath; ?> as a string rather than empty string in a DOM like OP said
Just thought it was worth mentioning for others as well, since nothing in his script indicates that it's not in a separate .js file (it might as well have been).
Actually all my data is in a seperate php file which gets all the data from a table in the database. in this page I an display the data in a table and trying to paginate it. Currently the ajax function you can see gets the data but it is not displaying it properly I think its because I am not handling each one of the parameters of the ajax function properly.
You do need to handle them properly, and please do sending correctly data from server side
0

Simply put your vars into your $(document).ready(function(){

(function($) {
 $(document).ready(function(){

var assetPath ='<?php echo $assetPath; ?>';
var num ='<?php echo $count; ?>';

$('.pager-top,.pager-bottom').bootpag({
    total: assetPath,
    page: 1,
    maxVisible: 5,
    leaps: true,
    firstLastUse: true,
    first: '←',
    last: '→',
    wrapClass: 'pagination',
    activeClass: 'active',
    disabledClass: 'disabled',
    nextClass: 'next',
    prevClass: 'prev',
    lastClass: 'last',
    firstClass: 'first'
}).on("page", function(event, num){
    $.ajax({
            url: "assetPath?pageNumber="+num,
            }).done(function(data) {
            $("#productResults").html( data );
             });
});
})( jQuery );
});

Hope this will work :)

1 Comment

If course, this will only work if this javascript code is in the .php page itself, not in a separate .js file.

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.