Using jQuery serialize to put ajax values into a database select query

466 views Asked by At

I have the following code:

<?php
$allform = $_POST['allform'];
parse_str($allform, $output);

$allquery = "SELECT * FROM wp_users";
$names    = array();
$allresult = mysql_query($allquery) or die(mysql_error()); ?>

...

<?php

while ($rows = mysql_fetch_array($allresult)) {

    $names[] = $rows['user_email'];

}
?>

The allform variable is a jQuery serialize string:

var allform = $('form#all').serialize();

Basically, I want to put the values from the form in the front end into a mysql select query in the back end.

The form is a bunch of checkboxes so the idea is that the SELECT something will have different number of values depending on what the user checks. Any ideas?

Thanks

1

There are 1 answers

0
Nicola Peluchetti On BEST ANSWER

The best thing to do could be something like this. Your checkboxes should be like this

<input type="checkbox" name="checkboxes[]" value="cream" />
<input type="checkbox" name="checkboxes[]" value="choco" />
<input type="checkbox" name="checkboxes[]" value="lime" />

server side you receive an array

$flavours = $_POST["checkboxes"];
$sql = "SELECT ".implode(',', $flavours)." FROM FLAVOURTABLE";