LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2004, 03:38 PM   #1
Silent1
Member
 
Registered: Jul 2002
Location: Davie, FL
Distribution: Debian
Posts: 33

Rep: Reputation: 15
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.
 
Old 08-30-2004, 06:46 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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;
        }

 
Old 08-31-2004, 08:52 AM   #3
Silent1
Member
 
Registered: Jul 2002
Location: Davie, FL
Distribution: Debian
Posts: 33

Original Poster
Rep: Reputation: 15
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?
 
Old 08-31-2004, 03:03 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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"

Last edited by Cedrik; 08-31-2004 at 03:15 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL results to html forms using PHP xemous Programming 3 08-15-2005 03:27 PM
Forms in HTML J_K9 Programming 5 06-30-2005 02:21 PM
passing an array from html-forms to php prabhatsoni Linux - Software 2 06-04-2005 12:11 AM
HTML Forms belorion Programming 1 02-01-2005 11:41 PM
Breaking up html forms logicdisaster Programming 4 06-18-2004 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration