LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   HTML, PHP, forms, submitting data Problem (https://www.linuxquestions.org/questions/programming-9/html-php-forms-submitting-data-problem-224438/)

Silent1 08-30-2004 03:38 PM

HTML, PHP, forms, submitting data Problem
 
i have a form with multiple input text fields along with a checkboxes. Problem is not displaying the data but inserting the data into the database. See with checkbox only the values that are checked are submitted. With the text field every value is submitted. So the post data looks like this

[MasterBehaviorPlanFrequency] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
skip a few inputs
[127] => 3
[128] => 3
[129] => 3
)

[MasterBehaviorPlanAddDB] => Array
(
[0] => 61
[1] => 91
[2] => 75
)

How can i match up the data? Thanks in advance.

Cedrik 08-30-2004 06:46 PM

You could name your <input> accordingly, say check_1 and text_1, check_2 and text_2...

Then, you parse the post array like :
PHP Code:

$texts = Array();
$checks = Array();

foreach(
$_POST as $key => $val) {
        if(
ereg("text_"$key)) {
                
$tmp explode("_"$key);
                
$texts[$tmp[1]] = $val;

        } else if(
ereg("check_"$key)) {
                 
$tmp explode("_"$key);
                 
$checks[$tmp[1]] = $val;
        }



Silent1 08-31-2004 08:52 AM

see the problem is that php does not see checkboxes that aren not checked. But your code has solved a few things in other chunks of code i have. Do you have any idea how i can get php to see unchecked boxes?

Cedrik 08-31-2004 03:03 PM

You could test if the checkbox variable name is set in $POST

PHP Code:

if(!isset($_POST["mycheck"])) {
        echo 
"checkbox myckeck was not checked\n";


or from the previous example (with text_1, check_1; text_2, check_2...)
say for 8 couples of text input/checkbox beginning at number 1

PHP Code:

// store all in one array
$infos = Array();

for(
$i 0$i 8$i++) {
        
$key $i 1
        
$infos[$i]["text"] = $_POST[$key];
        if(isset(
$_POST["check_$key"])) {
                
$infos[$i]["checkbox"] = $_POST["check_$key"];
        } else {
                
$infos[$i]["checkbox"] = "no-checked";
        }
}
echo 
"<pre>\n";
print_r($infos);
echo 
"</pre>\n"



All times are GMT -5. The time now is 03:43 PM.