Jump to content

PHP Array with HTML Form


amitkpt

Recommended Posts

Hi All,

 

I am using mysql to fetch some data and then putting it in the form. And I am doing it several times using for loop in a single form. Now I am not sure how to send data to submit form. Here is the code I am using.

 

    for($i=0; $i<$proj_users; $i++)

  {

 

    echo "<select name='emp_id[]'>";

   

        $result_emp = mysql_query("SELECT * FROM emp_details");

        $num_results_emp = mysql_num_rows($result_emp);

       

    for($x=0; $x <$num_results_emp; $x++)

    {     

        $row_emp = mysql_fetch_array($result_emp);

        echo "<option value='$i'>".$row_emp['emp_f_name']." ".$row['emp_l_name']."</option>";

    }

    echo "</select>";

  }

 

 

In other page it is

 

for ($i=0; $i< count($_POST['emp_id']); $i++)

{

$emp_id_array = addslashes($_POST['emp_id'][$i]);

}

foreach ($emp_id_array as $value)

{

    echo "Value: $value<br />\n";

}

 

 

Now I am not able to understand where I am wrong. Because it is giving me error.

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\project\page.php on line 38

 

Do anyone have idea about this.

Link to comment
https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/
Share on other sites

Are you declaring $emp_id_array as an array?  Forget about the code I suggested before and try replacing this block with this:

 

$emp_id_array = array();
for ($i=0; $i{
   $emp_id_array = addslashes($_POST['emp_id'][$i]);
}
foreach ($emp_id_array as $value)
{
    echo "Value: $value
\n";
}

 

Your first page should look like: (I assume this is already in a form)

 

for($i=0; $i  echo "";
  $result_emp = mysql_query("SELECT * FROM emp_details");
     while($row = mysql_fetch_assoc($result_emp)) {     
       echo "".$row['emp_f_name']." ".$row['emp_l_name']."";
     }
  echo "";
}
?>

 

Second page, try this:

 

foreach ($_POST['emp_id'] as $value)
{
    echo "Value: $value
\n";
}

Maq

 

Its working now. Actually there were two mistakes. We were specifying vaule in

 

echo "<option value='$i'>".$row['emp_f_name']." ".$row['emp_l_name']."</option>";

Actually it should come like this.

echo "<option>".$row['emp_f_name']." ".$row['emp_l_name']."</option>";

 

Other you told

 

foreach ($_POST['emp_id'] as $value)

 

Now its working and Thanks for such help.

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.