Ekate Posted March 28, 2007 Share Posted March 28, 2007 Hello,I'm a bit disappointed,I can't solve the problem,but the solution seems to be quite easy I have two forms with checkboxes on the page,method=post Then I'm scanning the checkboxes and if they're checked I insert data to database A piece of code echo "<form method='post'><div align='center'><table border='1'><tr><td></td><td>Number of group</td><td>Group Name</td><td>Definition</td></tr>"; $g=0; while($g<$num){ $id_group=mysql_result($result,$g,"id_group"); $group_name=mysql_result($result,$g,"group_name"); $definition=mysql_result($result,$g,"definition"); if ($g%2==0){$col='#FFFFFF';}else{$col='#CCCCFF';}; echo "<tr bgcolor='$col'><td><input type='checkbox' name='gname$id_group'></td><td>$id_group</td><td>$group_name</td><td>$definition</td></tr>"; $gnumber[]=$id_group; $g++; }; echo " <input type='reset' name='reset'></div></form>" ; $result =mysql_query( "select * from users") or die('Query failed. ' . mysql_error()); $num= mysql_num_rows($result); echo "<form method='post'><div align='center'><table border='1'><tr><td></td><td>User id</td> <td>First Name</td><td>Last Name</td><td>Additional information</td></tr>"; $u=0; while($u<$num){ $id_user=mysql_result($result,$u,"id_user"); $first_name=mysql_result($result,$u,"first_name"); $second_name=mysql_result($result,$u,"second_name"); $add_inform=mysql_result($result,$u,"add_inform"); if ($u%2==0){$col='#FFFFFF';}else{$col='#CCCCFF';}; echo "<tr bgcolor='$col'><td><input type='checkbox' name='uname$id_user'></td><td>$id_user</td><td>$first_name</td> <td>$second_name</td><td>$add_inform</td></tr>"; $unumber[]=$id_user; $u++; }; include 'closedb.php'; echo " <input type='reset' name='reset'></div></form>" ; if(isset($_POST['assign'])) { include 'config.php'; include 'opendb.php'; $result2 =mysql_query("select * from users") or die('Query failed.'. mysql_error()); $unum= mysql_num_rows($result2); $result3 =mysql_query("select * from groups") or die('Query failed.'. mysql_error()); $gnum= mysql_num_rows($result3); $i=0; $g=0; for ($i=0;$i<$unum;$i++) { $ucheckname = $_POST['uname'.$unumber[$i]]; if($ucheckname=='on') { echo "hello"; /*for ($g=0;$g<$gnum;$g++) { $gcheckname = $_POST['gname'.$gnumber[$g]]; if($gcheckname=='on') { $inserted=mysql_query("insert into user_group (id_user,id_group) values ('$unumber[$i]','$gnumber[$g]')"); echo $gnumber[$g]; $gcheckname='null'; }; }; */ $ucheckname='null'; }; }; include 'closedb.php'; //echo "<script> document.location.href='groupassign.php';</script>"; } But problem is that I can't get $_POST['uname'.$unumber[$i]]; Thank you in advance Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/ Share on other sites More sharing options...
trq Posted March 28, 2007 Share Posted March 28, 2007 Just use an array for your checkbox name, then loop through it. eg; <input type='checkbox' name='uname[]'> Then.... <?php foreach($_POST['uname'] as $val) { echo $val; } ?> Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/#findComment-216730 Share on other sites More sharing options...
Ekate Posted March 28, 2007 Author Share Posted March 28, 2007 Sounds good, but It doesn't work foreach($_POST['uname'] as $ucheckname ) and we have the following message Invalid argument supplied for foreach() in C:\phpFiles\web\ga2.php on line Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/#findComment-216776 Share on other sites More sharing options...
trq Posted March 28, 2007 Share Posted March 28, 2007 Post you revised code. And please, use the [ php ] [ /php ] tags. Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/#findComment-216780 Share on other sites More sharing options...
Ekate Posted March 28, 2007 Author Share Posted March 28, 2007 according to your advise there are not so many changes <?php include 'config.php'; include 'opendb.php'; $result =mysql_query( "select * from groups") or die('Query failed. ' . mysql_error()); $num= mysql_num_rows($result); echo "<form method='post'><div align='center'><table border='1'><tr><td></td><td>Number of group</td><td>Group Name</td><td>Definition</td></tr>"; $g=0; while($g<$num){ $id_group=mysql_result($result,$g,"id_group"); $group_name=mysql_result($result,$g,"group_name"); $definition=mysql_result($result,$g,"definition"); if ($g%2==0){$col='#FFFFFF';}else{$col='#CCCCFF';}; echo "<tr bgcolor='$col'><td><input type='checkbox' name='gname[]'></td><td>$id_group</td><td>$group_name</td><td>$definition</td></tr>"; $gname[g]=$id_group; $g++; }; echo " <input type='reset' name='reset'></div></form>" ; $result =mysql_query( "select * from users") or die('Query failed. ' . mysql_error()); $num= mysql_num_rows($result); echo "<form method='post'><div align='center'><table border='1'><tr><td></td><td>User id</td> <td>First Name</td><td>Last Name</td><td>Additional information</td></tr>"; $u=0; while($u<$num){ $id_user=mysql_result($result,$u,"id_user"); $first_name=mysql_result($result,$u,"first_name"); $second_name=mysql_result($result,$u,"second_name"); $add_inform=mysql_result($result,$u,"add_inform"); if ($u%2==0){$col='#FFFFFF';}else{$col='#CCCCFF';}; echo "<tr bgcolor='$col'><td><input type='checkbox' name='uname[]'></td><td>$id_user</td><td>$first_name</td> <td>$second_name</td><td>$add_inform</td></tr>"; $uname[u]=$id_user; $u++; }; include 'closedb.php'; echo " <input type='reset' name='reset'></div></form>" ; if(isset($_POST['assign'])) { include 'config.php'; include 'opendb.php'; $result2 =mysql_query("select * from users") or die('Query failed.'. mysql_error()); $unum= mysql_num_rows($result2); $result3 =mysql_query("select * from groups") or die('Query failed.'. mysql_error()); $gnum= mysql_num_rows($result3); $i=0; $g=0; foreach($_POST['uname'] as $ucheckname ) { if($ucheckname=='on') { echo "hello"; /*for ($g=0;$g<$gnum;$g++) { $gcheckname = $_POST['gname'.$gnumber[$g]]; if($gcheckname=='on') { $inserted=mysql_query("insert into user_group (id_user,id_group) values ('$unumber[$i]','$gnumber[$g]')"); echo $gnumber[$g]; $gcheckname='null'; }; }; */ $ucheckname='null'; }; }; include 'closedb.php'; //echo "<script> document.location.href='groupassign.php';</script>"; } else { ?> Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/#findComment-216784 Share on other sites More sharing options...
trq Posted March 28, 2007 Share Posted March 28, 2007 You need to setup your form stuff within an if() so it only runs when the form has been submitted. Also, note that only checked checkboxes actually show up so there is no need to check for 'on'. <?php if (isset($_POST['submit'])) { foreach($_POST['uname'] as $val) { echo $val; } } ?> Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/#findComment-216815 Share on other sites More sharing options...
Ekate Posted March 29, 2007 Author Share Posted March 29, 2007 but I do it in my code <?php if(isset($_POST['assign'])) {...} ?> Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/#findComment-217253 Share on other sites More sharing options...
Ekate Posted March 29, 2007 Author Share Posted March 29, 2007 ok,thank you very much. I've already solved it. The problem vas that I had 2 forms,and both methods post btw,thank you thorpe Link to comment https://forums.phpfreaks.com/topic/44630-solved-_post-problem/#findComment-217266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.