0

I want to use the value of limit and offset into my PHP code but I can't.

Here is my code:

var maxData = 0;
var limitt=6;
var offsett=1;

$.ajax({
     url: "../model/conn.php",
                    type: 'POST',
                    data: 'getData='+'&limit='+limitt+'&offset='+offsett,
                }).done(function( data ) {
                    $("#d1 ").html(data);
                    while (limitt<maxData){
limitt= limitt+6;
offsett=offsett+6;

}
}); 

<?php
if(isset($_POST['getData'])) {
$serv = "localhost";
$user = "root";
$psrd = "";
$db = "nonc";

$conn = mysqli_connect($serv, $user, $psrd, $db);

$limit=$_POST['&limit'];

$offs=$_POST['&offset'];

$sql = "SELECT * FROM non_confor limit $offs, $limit;";
$resltt = mysqli_query($conn, $sql);
$checkk = mysqli_num_rows($resltt);
?>

When I run my PHP page they show me that I have errors on $limt and $offs, because they don't receuve the data from AJAX.

3
  • are you trying to send a post call ? Commented Mar 22, 2019 at 16:04
  • the javascript and the Php code are in two separate files, are they? Commented Mar 22, 2019 at 17:35
  • yes they are in seperate files Commented Mar 26, 2019 at 13:11

2 Answers 2

1

First thing, you are using POST method for form submission and passing data as a query string which is making an error. Correct that as below:

$.ajax({
    url: "../model/conn.php",
    type: 'POST',
    data: { 
        'getData': 1, // passing 1 as you are using getData in php.
        'offset': offsett,
        'limit': limitt
    },
}).done(function(data) {
    $("#d1 ").html(data);
    while (limitt<maxData){
        limitt= limitt+6;
        offsett=offsett+6;    
    }
});

After this, you need to make modification in your PHP code as below:

$limit=$_POST['limitt'];
$offs=$_POST['offsett'];

After this, your code should work fine. Hope it helps you.

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

1 Comment

Great, hope you will upvote and accept my answer :)
0

This is a syntax problem , here is the correction :

$.ajax({
    type: 'POST',
    url: '../model/conn.php',
    data: { 
        'getData':limit,
        'offset':offsett
    },
    success: function(msg){
       // rest of your code 
    }
});

Data attribute should have as value a json format

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.