Jump to content

[SOLVED] $_POST problem


Ekate

Recommended Posts

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

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

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

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.