Jump to content

[SOLVED] Help ! form, checkbox validation.


sheriff

Recommended Posts

Hi all!

 

I am new to php and i need some help.

 

I have a list of products generated from mysql.

In front of every item is a checkbox.

I want only the selected items to be listed on the next page.

How can I do this ?

 

there is what I do:

 

<?php

 

session_start();

 

?>

 

<HTML>

<HEAD>

<title>List</title>

 

<meta http-equiv="content-type" content="text/html; charset=iso-8859-2">

 

</HEAD>

 

<BODY>

 

 

 

<?php

 

include('include/db.php');

 

//

// The code

//

 

// The query

$query = "select * from products ORDER BY pr_name";

$result = mysql_query($query);

$count = mysql_num_rows($result);

 

$color1 = "#DDDAD4";

$color2 = "#DDD7CC";

$row_count = 0;

 

// keeps getting the next row until there are no more to get

 

// pr_id | pr_type | pr_name | pr_desc | pr_um | pr_price

 

echo "<center><br><font color='#000000' size='4'><strong>Lista Produse</strong></font><br><br>"

."<table align='center' bgcolor='#DDD2BA' cellpadding='1' cellspacing='1' width='78%'>"

."<tr>"

."<td width='6%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>ID</strong></font>"

."<td width='34%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>Name</strong></font>"

."<td width='44%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>Description</strong></font>"

."<td width='6%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>UM</strong></font>"

."<td width='6%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>Price</strong></font>"

."<td width='4%' bgcolor='#475558' align='center'>"

."</tr>";

 

 

// Print out the contents of each row into a table

while($row = mysql_fetch_array( $result )) {

 

 

// switch evry second row color

$row_color = ($row_count % 2) ? $color1 : $color2;

 

// define variables

 

$id = $row['pr_id'];

$name = $row['pr_name'];

$desc = $row['pr_desc'];

$um = $row['pr_um'];

$price = $row['pr_price'];

 

?>

 

<form name="order" action="<?php $_SERVER["PHP_SELF"]; ?>" method="POST" onSubmit="check_sel(order)">

<tr>

<td align="center" bgcolor="<?php echo $row_color; ?>"><?php echo $id; ?>

<td align="center" bgcolor="<?php echo $row_color; ?>"><?php echo $name; ?></td>

<td align="center" bgcolor="<?php echo $row_color; ?>"><?php echo $desc; ?></td>

<td align="center" bgcolor="<?php echo $row_color; ?>"><?php echo $um; ?></td>

<td align="center" bgcolor="<?php echo $row_color; ?>"><?php echo $price; ?></td>

<td align="center" bgcolor="<?php echo $row_color; ?>"><input type='checkbox' name="check[]" value="<?php echo $id; ?>"></td>

</tr>

 

<?php

$row_count++;

}

 

echo "<tr>";

echo "<td colspan='6'><input type='submit' value='submit' name='submit' onclick='check_sel(this);'>";

echo "</form>";

echo "</table>";

 

?>

 

</BODY>

</HTML>

 

Thanks for help in advice.

 

Link to comment
https://forums.phpfreaks.com/topic/45384-solved-help-form-checkbox-validation/
Share on other sites

Just run this example. I think you will get the idea

<?
if(isset($_POST)){
echo "<pre>";
print_r($_POST['chk']);
}
?>

<form name="a" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table>
<input type="checkbox" name="chk[]" value="sel_item_1">
<input type="checkbox" name="chk[]" value="sel_item_2">
<input type="checkbox" name="chk[]" value="sel_item_3">
<input type="checkbox" name="chk[]" value="sel_item_4">
<input type="checkbox" name="chk[]" value="sel_item_5">
<input type="checkbox" name="chk[]" value="sel_item_6">
<input type="checkbox" name="chk[]" value="sel_item_7">
<input type="checkbox" name="chk[]" value="sel_item_8">
<input type="submit" name="submit" value="submit">
</table>
</form>

It's cool !!!

 

But .. i'm noob ...

 

I receive now all the selected checkbox's.

ok ..

 

how can i put this array in a loop to display it in a table ?

 

now i have an output like this:

 

Array ( [0] => 286 [1] => 156 [2] => 179 [3] => 285 [4] => 294 )

 

 

if i use this code:

<?php

 

include('include/db.php');

 

if(isset($_POST)){

 

$chk = $_POST[check];

 

}

 

echo $chk[0];

 

?>

 

it shows me the first value from the array.

 

how can i make a loop in a table ?

and to extract data from mysql !?

 

$query = "select * from products where pr_id='$chk'";

$result = mysql_query($query);

 

how to formulate the querry ?

 

Thank you very very much !

<?
if(isset($_POST)){
echo "<pre>";
print_r($_POST['chk']);

             foreach($_POST['chk'] as $key => $ids){
                    echo "selected id  ".$ids;
                     echo "<br>";
             }

              OR

               for($i=0;$i<count($_POST['chk']);$i++){
                     echo "selected id  ".$_POST['chk'][$i];
                     echo "<br>";
                }
}
?>

for the last time please!

 

this is the code:

 


<?php

include('include/db.php');

echo "<center><br><font color='#000000' size='4'><strong>Lista Produse</strong></font><br><br>"
."<table align='center' bgcolor='#DDD2BA' cellpadding='1' cellspacing='1' width='78%'>"
."<tr>"
	."<td width='6%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>ID</strong></font>"
	."<td width='34%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>Name</strong></font>"
	."<td width='44%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>Description</strong></font>"
	."<td width='6%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>UM</strong></font>"
	."<td width='6%' bgcolor='#475558' align='center'><font color='#FFFFFF' size='2'><strong>Price</strong></font>"
."</tr>";

if(isset($_POST)){

		 foreach($_POST['check'] as $key => $ids){
				$query = "select * from products WHERE pr_id='$ids'";
		 }
           
}

$result = mysql_query($query); 
$count = mysql_num_rows($result);

$color1 = "#DDDAD4"; 
$color2 = "#DDD7CC"; 
$row_count = 0; 

while($row = mysql_fetch_array( $result )) {

$id = $row['pr_id'];
$name = $row['pr_name'];
$desc = $row['pr_desc'];
$um = $row['pr_um'];
$price = $row['pr_price'];




echo	"<tr>"
	."<td>$id"
	."<td>$name</td>"
	."<td>$desc</td>"
	."<td>$um</td>"
	."<td>$price</td>"
	."</tr>";


$row_count++;  
}

echo	"</table>";
?>

 

 

If i check 3 items, only the last is shows up.

 

What i do wrong ?

 

Sorry for my very beginner problem :(

 

Thanks !

if(isset($_POST)){
          if(count($_POST['check'])){
            $list_selected = "";
            foreach($_POST['check'] as $key => $ids){
                   $list_selected .= $ids .",";
            }
            $list_selected = substr($list_selected,0,strlen($list_selected)-1);
            // This will give ur selected ids like this 100,203,125,123
          }else{
               $list_selected = "-1";
          } 
}
$query = "select * from products WHERE pr_id IN (".$list_selected.")";

// I mean the query will be like this  "select * from products WHERE pr_id IN (100,203,125,123)";


 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.