Solved

make a controle in dynamic checkboxes

Posted on 2009-07-06
20
219 Views
Last Modified: 2012-05-07
Hi
i have a form with name,date,account then a display of product database table displayed as checkboxes and near each checkbox there is a field where i put a quantity
my question is about controle
i can make a controle on name,date..means
if(empty($name) or (empty($date) here the form will exit because he must put a name or date and tick one or more checkboxes ..
but i cannot do it for checkboxes.

Regards
A.S
0
Comment
Question by:asaidi
  • 7
  • 7
  • 5
20 Comments
 
LVL 5

Expert Comment

by:kingofnines
ID: 24784286
Use isset() instead of empty()...

if(isset($_POST['checkBoxName']))
{
   // valid(checked)
}
else
{
  // invalid(not checked)  
}


Alan
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24784473
If checkboxes are not checked, they are not present in $_POST - the same goes for radio buttons.

You can use var_dump($_POST) to see what is coming into your action script from the form script.

If you want to post the scripts, we may be able to provide more concrete advice.

best, ~Ray
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24784499
Ray,

Checkboxes have a value of "on" in $_POST when checked. That is why I suggested isset(). Are you saying my suggestion is invalid?


Alan
0
 

Author Comment

by:asaidi
ID: 24784562
Hi Kingofnines

i did that but the problem if i have 200 checkboxes i must tick them all  
if i tick only two or more and not the all does not work
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24784574
Sounds to me like you may have all your checkboxes named the same. As ray said, if you post your script it would be easier for us to help find the issue.


Alan
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24784590
@kingofnines:

"Are you saying my suggestion is invalid?"

No, of course not.  But I am trying to lead the Asker in the direction of using data visualization techniques to see the contents of the $_POST array.  From the recent comment about 200 checkboxes, it would seem that there is a design flaw or logic error that goes beyond a simple question of whether a checkbox is set.

;-)

Best to all, ~Ray
0
 

Author Comment

by:asaidi
ID: 24784596
the first snippet it a bit of the code of the form.php or if you want i can post you the two scripts
<?php

$i=0;

$que ="SELECT * FROM product where type='$type' ORDER BY cat_id";

$rs=mysql_query($que);

echo "<id='h4'>Product</h4>";

echo "<table width='100%' border=1 bgcolor='#ffccff'>";

$num=0;

while ($row = mysql_fetch_array($rs))

{

 

         if($i==5)

         {

           echo "</tr>";

           $i=0;

         }

         echo "

         <td>$row[product]</td>

         <td><input type='checkbox' name='boxes[]' value='$row[id]'></td>

         <td><input type=\"hidden\" name=\"qte[$row[id]]\" value=\"$row[prod_id]\"><input type=\"text\" name=\"value[$row[id]]\" size=\"4\"></td>

         <td><input type=\"hidden\" name=\"id[$row[id]]\" value=\"$row[id]\"><input type=\"hidden\" ></td>

         <td><input type=\"hidden\" name=\"catid[$row[id]]\" value=\"$row[cat_id]\"><input type=\"hidden\" ></td>";

 

$i++;

}
 
 

****************

insert section
 

<?php

  if(!isset($ref)|| !isset($idv)) {

    die("All fields must be filled in.Please go back and try again");

    echo "<tr><td id='td1'><a href='form.php'>Return to Main Menu!!!!!</a></td></tr>";

   

}

foreach ($_POST['boxes'] as $id){

            $itemId = $_POST['qte'][$id];

            $product = $_POST['value'][$id];

            $id1=$_POST['id'][$id];

            $cat_id1=$_POST['catid'][$id];

 
 

        

           $req="select count(*) from command where ref='".$ref."' and prod_id='".$itemId."' and

            date_order='".$date1."'";

            $res=mysql_query($req);   

            if (mysql_result($res,0)>0){

             echo"<p id='p'>$p1</p>";

                echo"<p id='p'>$p2</p>";

                                  $p3=$p1;

                                  if ($p1==$p3){

                                      $p1=' ';

                                  }

                                  $p4=$p2;

                                  if($p2==$p4){

                                    $p2=' ';

                                  }

                         exit;

 }

         else{

              $queries=array();

              $queries[]="('$id1','$idc','$account','$itemId','$cat_id1','$idv','$date1','$ref','$product','$type','$note')";

              $piece=implode(", ", $queries);

              $query="INSERT INTO command

              (idd,idc,account,prod_id,cat_id,idv,date_order,ref,qte,type_id,note) VALUES $piece";

               $ok=@mysql_query($query);

                      if($ok){  

                                  echo"<p id='p'>$p</p>";

                                  $p1=$p;

                                  if ($p==$p1){

                                      $p=' ';

                                  }

                           

                          

                       } 

                        

Open in new window

0
 

Author Comment

by:asaidi
ID: 24784607
My values in checkboxes are inserted inti database when i ticked them
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24784763
Wow, there is a lot of work needed here!

It looks like you may be depending on undefined variables or worse, on "register_globals".   Add this to the top of the script:
error_reporting(E_ALL);

Also, it looks like the PHP code may generate invalid HTML, so you might want to run the output through a W3C validator.

You might want to consider adding comments to the code and following the userland coding guidelines outlined in the PHP online manual.  Consistent indenting and good variable naming conventions make a big difference in the readability of the scripts.

You need to test for the success of your queries.  A statement like this suppresses the error messages that you REALLY NEED TO SEE while you are debugging the code.
$ok=@mysql_query($query);

Here is the correct way to run a query, test for success and show the error if it did not work.

HTH, ~Ray
// CREATING AND SENDING A SELECT QUERY AND TESTING THE RESULTS

// MAN PAGE:http://us2.php.net/manual/en/function.mysql-query.php

$sql = "SELECT id FROM my_table WHERE username='$username'";

$res = mysql_query($sql);
 

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS

if (!$res)

{

   $errmsg = mysql_errno() . ' ' . mysql_error();

   echo "<br/>QUERY FAIL: ";

   echo "<br/>$sql <br/>";

   die($errmsg);

}

// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24784772
Here is how to print out what is present in the data posted to your script from a form.  Using <pre> makes it much more readable.

Best of luck with your project, ~Ray
// WHAT DID THE CLIENT SEND ME?

echo "<pre>\n";

var_dump($_POST);

echo "</pre>\n";

Open in new window

0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24838822
It would seem to me that Ray not only answered asaidi question, he went to to take the time to explain how to visualize the problem. Ray also pointed out other issues he saw in the script and supplied some sample code of the proper way to do it. asaidi never responded to Rays posts saying they did or didnt help, he simply left the post hanging. Also, asaidi has a history of doing this. This is the second post in a week that Ive received delete requests for. asaidi needs to understand that you cant just post questions, gather the offered information and then request refunds of points.

Anyway, I feel strongly that Ray deserves the full points for this post.


Alan
0
 

Author Comment

by:asaidi
ID: 24839425
Sorry about that but ray he did not answer my question and the code he gave me it is was not the question i have asked..
because everything was working my question was how to make a control in checkboxes

when i ticked them they are inserted into database means $_post was not empty ..and to say that i do this a lot of times no Mr a lot of problems i got the solutions from other sites and another examples then please understand me i sent a lot of times the scripts and they cannot understand them..
simple question it was asked how to make user tick the checkboxes simple as that


0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24840373
Perhaps the misunderstanding comes from this phrase: "how to make a control in checkboxes"

What exactly does that mean?  How did we misinterpret your needs?

Also, when you "got the solutions from other sites" it is useful to post those solutions here for the benefit of the EE community.
Thanks and regards, ~Ray
0
 

Author Comment

by:asaidi
ID: 24840436
Hi Ray Passeur
may be it is misunderstanding
for this question i did not get any solution i left the control for name and date i did not put a control for the checkboxes..
the solution i had for other questions one it was from xcr old question asked from another user and it is in EE
THE OTHER ONE it was a simple solution that i use update query not really something difficult
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24840458
"it was from xcr old question asked from another user and it is in EE"

Good.  Then post it here.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24840809
Hi, M.O.  

This dialog shows why it would be useful to have a "plonk" capability so I could drop questions from sight.  No offense meant to asaidi or anybody else in the community here, but I would be able to help others with their questions if I were not wasting my time on questions that are just going to get deleted.

Just a thought, ~Ray
0
 

Author Comment

by:asaidi
ID: 24840821
Hi modus_operandi

for the question about make a controle in dynamic checkboxes
i did not find any solution i close the question because no answer to my query.
in my script i did not make a control on checkboxes as there is no solution

Regards

A.S
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24840850
I think http:#24784763 should be accepted as the answer. There are several issues with his script and Ray explained that to him and explained how to get a better understanding of the issues. asaidi never replied to Rays post saying it didnt help him or state any further issues not covered in rays example. He simply waited a week and then tried to delete it.

As far as asaidi saying no answer was given, that simply isnt true. I said...

"Sounds to me like you may have all your checkboxes named the same."

Then he posts his code and what do we see?

"<td><input type='checkbox' name='boxes[]' value='$row[id]'></td>"

He is in fact naming all of his checkboxes the same.

Weather or not it is the "whole" problem isnt known because he never replied saying it was, wasnt, or otherwise.


Alan        
0
 

Author Comment

by:asaidi
ID: 24840862
Hi Ray_Paseur

that is why i delete this question to don't let you waste your time.i know you are busy helping php developer then i decide to close my question ,and also me i need my program finish and let user using it without the control i loved to do it to keep the user under controle

sorry about that and i will never do it again i keep it open and it will close automatically

really sorry Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now