Breaking up a single var value into 2 parts

Im sending 2 values as 1 to a ajax script using the code below.

<span id="<?php echo "id=".$tutorial_id. "&in=".$in; ?>" class="show_more" title="Load more posts">Show more</span>

To the AJAX below

var ID = $(this).attr('id');
$('.show_more').hide();
$('.loding').show();
$.ajax({
type:'POST',
url:'ajax-search-demock_3.php',
data:ID,
success:function(html){
$('#show_more_main'+ID).remove();
$('.tutorial_list').append(html);
}
}); 

I echo’d out ID and I got the follwoing line, which is spot on - id=186&in=Cancun

But I need to change the ajax above so that Var ID only has id and say Var IN only has in, and then using POST as above to send both ID and IN, and to then collect them both as I was doing with ID in a line as below

if(isset($_POST["id"]) && !empty($_POST["id"])){

don’t do that. esp. with IDs. rather use data attributes:

<span data-id="<?= $tutorial_id; ?>" data-in="<?= $in; ?>" class="show_more" title="Load more posts">Show more</span>
var id = this.dataset.id;
$.post('ajax-search-demock_3.php', { id: id }, function (html) {
    $('#show_more_main'+id).remove();
    $('.tutorial_list').append(html);
});

Hi Dormilich,

Got the first bit fine and yes that makes more sense.

The AJAX though is new to me as I’m literally finding my feet with it in this project.

Could you help by editing mine below in full to include yours and to be able to send the second value ‘in’ to

$(document).ready(function(){
$(document).on('click','.show_more',function(){
var id = this.dataset.id;
$('.show_more').hide();
$('.loding').show();
$.ajax({
type:'POST',
url:'ajax-search-demock_3.php',
data:ID,
success:function(html){
$('#show_more_main'+ID).remove();
$('.tutorial_list').append(html);
}
}); 
});
});

I had to change it to id2 in the line below

<span data-id="<?= $tutorial_id; ?>" data-id2="<?= $in; ?>" class="show_more" title="Load more posts">Show more</span>

And then added it to the ajax below, but not sure how I post the value

$(document).ready(function(){
$(document).on('click','.show_more',function(){
var id = this.dataset.id;
var id2 = this.dataset.id2;
$('.show_more').hide();
$('.loding').show();
$.post('ajax-search-demock_3.php', { id: id }, function (html) {
$('#show_more_main'+id).remove();
$('.tutorial_list').append(html);
});
});
});

To collect it and use it below

if(isset($_POST["id"]) && !empty($_POST["id"])){
$msg="";
$sqlAll="select COUNT(*) as num_rows from tbl_hotels LEFT JOIN tbl_resorts ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) where tbl_resorts.Nom_Rsrt like '%$in%' AND Act_Hot=1 WHERE tbl_hotels.Id_Hot < ".$_POST['id']." ORDER BY tbl_hotels.Id_Hot DESC";

you see that part with $.post ?

I think I got it -

$.post('ajax-search-demock_3.php', { id: id, id2 : id2 }, function (html) {

Then posts to the other php page and gathered as below

if(isset($_POST["id2"]) && !empty($_POST["id2"])){
$ID2 = $_POST['id2'];
}

Seems to work, but what you think?

I think you can simplify that a bit more:

$.post('ajax-search-demock_3.php', this.dataset, function (html) {

should that not work use:

$.post('ajax-search-demock_3.php', $(this).data(), function (html) {

better use filters:

$ID2 = filter_input(INPUT_POST, 'id2', FILTER_VALIDATE_INT);

The first simplification worked fine, and where does the filter go?

within the POST function is it?

$.post('ajax-search-demock_3.php', this.dataset, function (html) {
$('#show_more_main'+id).remove();
$('.tutorial_list').append(html);
});

Or outside and above it

into the PHP script.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.