amitkpt Posted April 9, 2009 Share Posted April 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/ Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Change this line to: $emp_id_array = addslashes($_POST['emp_id']); Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-805706 Share on other sites More sharing options...
amitkpt Posted April 10, 2009 Author Share Posted April 10, 2009 Not working and same error. Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-806086 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-806091 Share on other sites More sharing options...
xtopolis Posted April 10, 2009 Share Posted April 10, 2009 His selectbox is not setup to post an array: <SELECT MULTIPLE SIZE=5> would be the syntax he is looking for. Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-806097 Share on other sites More sharing options...
amitkpt Posted April 10, 2009 Author Share Posted April 10, 2009 Maq After declaring array it is not showing the warning but also not printing value of array. xtopolis I don't want multiple option because I want single option to be choose not multiple. Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-806119 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-806122 Share on other sites More sharing options...
amitkpt Posted April 10, 2009 Author Share Posted April 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-806135 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 Hmm that's weird it's working because your options don't have any values... Well, if it works it work Please mark as [sOLVED]. Quote Link to comment https://forums.phpfreaks.com/topic/153356-php-array-with-html-form/#findComment-806348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.