Reputation: 1174
Here i just want to discus about the following:
My HTML From like the fllowing code:
<html>
<head>
<title>My Form</title>
</head>
<body>
<form id="sample" method="post" action="saveData.php">
Courses:
<input type="checkbox" name="check[]" Value="C++" />C++
<input type="checkbox" name="check[]" Value="PHP"/>PHP
<input type="checkbox" name="check[]" Value="MYSQL" />MYSQL
<input type="checkbox" name="check[]" Value=".Net"/>.Net
Gender:
<input type="radio" name="gen[]" Value="male"/>male
<input type="radio" name="gen[]" Value="female"/>Female
</form>
</body>
</html>
And I Want the OutPut Like The Following:
foreach ($_POST as $key => $val) {
$actVal .= "'".strtolower($key)."|".strtolower($val)."',";
$sqlin .= " ".strtolower($key)." VARCHAR(255) , ";
}
But I got The Output like which one clicked in that options:
Like the following:
-----------------------------------------
male
C++
but I need it like the following:
male,female
C++,PHP,MYSQL,.Net
Upvotes: 1
Views: 380
Reputation: 2543
POST will send all values if you mark them as selected using javascript right before submitting. $_POST["check"] is an array. use a foreach and get all values from that array.
Upvotes: 1
Reputation: 8976
I can't figure out a way around that one. But there is an alternative:
<input type="checkbox" name="check" value="php" />PHP
<input type="hidden" name="checklist" value="php" />
<input type="checkbox" name="check" value="MySQL" />MySQL
<input type="hidden" name="checklist" value="MySQL" />
The idea is to store the list of all the values of a checkbox/radio button, in a hidden input so that you get the list of those values server-side, when the form is submitted.
BTW, why do you need it anyway?
Upvotes: 1
Reputation: 4301
As you're loop through post data which is going to be an array I believe that is why only one of their elements are being returned.
You might want to try something like this:
foreach ($_POST as $key => $val) {
if ($key == "check" || $key == "gen") { // If this is an array post field
foreach ($val as $val2) { // We need to loop through again since they're array post fields
$actVal .= "'" . strtolower($val2) . "'";
}
} else {
$actVal .= "'".strtolower($key)."|".strtolower($val)."',";
}
//$sqlin .= " ".strtolower($key)." VARCHAR(255) , "; // Worry about this separately, should be the same process
}
Upvotes: 1