user1037839
user1037839

Reputation:

Getting datas from a generated form (php/ajax)

I'm generating a form with php/mysql. I'm using checkbox that looks like that:

<input type="checkbox" id="my2_3" name="my2_3" />
<input type="checkbox" id="my2_4" name="my2_4" />
<input type="checkbox" id="my2_5" name="my2_5" />

My issue is to retrieve those data (whether the checkbox is checked or not and the id). How can I retrieve that with php without knowing in advance what will be the $_POST[""] to request?

Upvotes: 0

Views: 68

Answers (4)

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9508

<input type="checkbox" id="my2_3" name="my[]" value="my2_3" />
<input type="checkbox" id="my2_4" name="my[]" value="my2_4" />
<input type="checkbox" id="my2_5" name="my[]" value="my2_5" />

I changed the name attribute to an array,

foreach($_POST['check'] as $value) {
   $check_msg .= "Checked: $value\n";
}

Upvotes: 1

Arjan
Arjan

Reputation: 9884

Checkboxes are only posted when they are ticked. So you need to inspect $_POST and use isset() to determine whether or not the key you are looking for (the name attribute of a checkbox) is present. If it is, the checkbox was ticked. If not, the checkbox was unticked.

Upvotes: 0

Lumbendil
Lumbendil

Reputation: 2916

It'd be easier for you if you changed the name attribute to create a php array. Check the documentation on creating arrays in HTML form.

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318748

You can use foreach($_POST as $key => $value) { ... } to iterate over all POST vars.

Upvotes: 0

Related Questions