JohnG
JohnG

Reputation: 497

Ajax is not passing values to php file

I've been trying to solve this for hours - any help would be appreciated! I've got a (long) array of checkboxes in a form. Omitting most, let's say my html is:

<form name="SelectCategories" method="post">
    <input type="checkbox" name="category[A]" value="A" id="A" /><label for="A">A</label>
    <input type="checkbox" name="category[B]" value="B" id="B" /><label for="B">B</label>
    <input type="checkbox" name="category[C]" value="C" id="C" /><label for="C">C</label>
    <input type="submit" value="Submit" id="submit">
</form>

<div id="display">Select something</div>

I have some PHP beneath this:

<?php 
error_reporting(0);
$categoriesarray=implode(',',$_POST['category']);
echo $categoriesarray;
?>

Then I have some JQuery, to transfer the categoriesarray to a php page. (For some reason the echo above doesn't always work when the JQuery is pasted in, but sometimes does).

$(document).ready(function(){
    $("form").on("submit", function(e){
        e.preventDefault();
        $ajax.({url:"Data.php", type:"POST", data:{data1 : '<?php echo $categoriesarray; ?'>}, success: function(result){
            $("#display").html(result);
        }});
    });
}) 

Finally, my Data.php file (ignoring all the password stuff) is:

    <?php
    $list= $_POST['data1'];
    $cxn=mysqli_connect($host, $username, $password, $database)
    or die("Not connected");
    $query = "SELECT * FROM table WHERE 1 in ($list) ORDER BY RAND() LIMIT 10";
    $result = mysqli_query($cxn,$query)
    or die("Select something");
    while($row = mysqli_fetch_assoc($result)){
        echo $row['columnname'];

    }
?>

The idea is that it pulls out 10 random database entries which have the value "1" in the checked categories. However, I only get "Select something" returned in my display div.

Weirdly, if I delete something on the html page, refresh the html page, put the code back in and refresh it again, the whole thing works perfectly. But if I connect to the page by going through the address bar - instead of refreshing - I just get "select something" returned when I press submit, instead of data from the database.

Any ideas what's going wrong?!

Upvotes: 0

Views: 124

Answers (1)

Voltlight
Voltlight

Reputation: 136

It's hard to understand what exactly is being done here, but here are some things to consider:

<?php 
error_reporting(0);
$categoriesarray=implode(',',$_POST['category']);
echo $categoriesarray;
?>

Are you POSTing anything at this stage?

$(document).ready(function(){
$("form").on("submit", function(e){
e.preventDefault();
$ajax.({url:"Data.php", type:"POST", data:{data1 : '<?php echo 
$categoriesarray; ?'>}, success: function(result){
$("#display").html(result);
}});
});
})

With this AJAX, you are putting a PHP echo directly into the object it's sending.. You should instead be collecting which checkboxes are ticked by the user, no?

Something like:

$("form").on("submit", function(e){
var data = $( "form" ).serialize();
e.preventDefault();
$ajax.({url:"Data.php", type:"POST", data: data}, success: function(result){
$("#display").html(result);
}});
});

Now, you're getting an array in PHP which you can mess with and get the relevant data. And generally, you want to just do category[] and let it work itself out.

If you think I'm misunderstanding your intentions, I do apologize, but I just can't make sense of it.

Upvotes: 1

Related Questions