I have a url string being compiled based on existing PHP array values.
The URL is fine however after each time the AJAX call is made I want the variable last
increased by 100.
The console log is showing the correct value yet the URL string has NULL after the offset value
Here is the URL string shown in console log
basictest.php?base=accountancy-jobs&cat=accountancy&kw=&loc=London&distance=10&cattitle=Accountancy&offset=200NULL
The value is being increased yet its adding NULL after it for some reason
Here is my code
let last = 100;
let vars = '<?php foreach($passedVars as $vars=>$key){ if($vars == 'base'){ echo $vars.'='.$key;$pv++;} if($pv < $pvcount){ echo '&'.$vars.'='.$key;$pv++;} else {echo '&'.$vars.'='.$key;}}?><?php echo "&offset=100";?>';
$('#next').click(function(e){
e.preventDefault();
vars = '<?php foreach($passedVars as $vars=>$key){ if($vars == 'base'){ echo $vars.'='.$key;$pv++;} if($pv < $pvcount){ echo '&'.$vars.'='.$key;$pv++;} else {echo '&'.$vars.'='.$key;}}?><?php echo "&offset=";?>'+last;
$.ajax({
dataType: 'html',
url: 'basictest.php?'+vars,
type: 'get',
success: function(html)
{
$("#more").append(html);
last += 100;
console.log(html);
console.log(last);
}
});
});
2
Answers
Do not use PHP because you are using AJAX so the vars are not updated since you are not supposed to reload the page…
Use URL and URL.searchParams to update the offset and $.get to get a new page
It might be easier to introduce your PHP array into Javascript using json_encode. Use parseInt to add
last
to youroffset
and param$.param(vars)
to add the query string to your URL