1

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

8
  • do the checkboxes all have the same name? Commented Mar 2, 2012 at 16:47
  • @Colin Not an INSERT, a SELECT because ill loop through the results and add them to a json_encode and display the results on the client-side. I just not sure how to go about getting the checkbox values in the select query. Commented Mar 2, 2012 at 16:49
  • @ZackMacomber, They have different names at the moment because I thought ild be able to distinguish them that way... Commented Mar 2, 2012 at 16:50
  • Setting your checkboxes to the same name will allow you to loop easily through them in the backend Commented Mar 2, 2012 at 16:54
  • Actually...just came across some posts on looping through checkboxes in PHP - have you checked out stackoverflow.com/questions/6930627/…? Commented Mar 2, 2012 at 17:12

1 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

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.