1

Hello so I have this form with 3 checkbox:

<form action="send.php">
<input type="checkbox" name="txt_group" value="CID">CID
<input type="checkbox" name="txt_group" value="OSDS">OSDS  
<input type="checkbox" name="txt_group" value="SGO">SGO
<input type="submit">
</form>

And in my send.php I have this code:

$stmt = $dbc->query("SELECT * FROM tblcontactlist WHERE contactGroup=:cgroup");
$stmt->bindParam(':cgroup', $_POST['txt_group']);
$stmt->execute();

I think that if I check only 1 checkbox, this will work. But what if I select 2-3 checkbox? Will this query still work?

1
  • 1. You have to prepare your query 2. Are you sure you didn't wanted radio buttons ? Commented Jun 11, 2015 at 3:59

4 Answers 4

4

You cant have same name for multiple inputs if it is not an array, else they will be overwritten by the last one. Try with -

<form action="send.php">
<input type="checkbox" name="txt_group[]" value="CID">CID
<input type="checkbox" name="txt_group[]" value="OSDS">OSDS  
<input type="checkbox" name="txt_group[]" value="SGO">SGO
<input type="submit">
</form>

With php an example query will be -

$values = implode("','", $_POST['txt_group']);
$values = $mysqli->real_escape_string($values);
"SELECT * FROM tblcontactlist WHERE contactGroup IN ('" . values . "')"
Sign up to request clarification or add additional context in comments.

11 Comments

OP already uses PDO and now you make the code wide open for SQL-Injection.
And now you are writing code where you don't even know what it's doing
Sir you should use prepare not query
@b0s3 Now you removed the code, why? Because it's completely wrong and invalid ?!
@Rizier123 Can you make your own answer then?
|
2

change the input name to array

<input type="checkbox" name="txt_group[]" value="CID">CID

then $_POST['txt_group'] get an array list of you checked

Comments

1

On your code you are given same name for all checkbox like this

<input type="checkbox" name="txt_group" value="CID">CID

Try to change name as array like

<input type="checkbox" name="txt_group[]" value="CID">CID
<input type="checkbox" name="txt_group[]" value="OSDS">OSDS  
<input type="checkbox" name="txt_group[]" value="SGO">SGO

and print it on server side using

print_r($_POST['txt_group']);

Comments

0

yes, a square bracket for the variable name is necessary if you are to post values as an array.. this name="text_group[]" is correct ..

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.