A JM Posted June 12, 2009 Share Posted June 12, 2009 I'm trying to loop through the items of a dropdown and I'm not having any luck. I'm using the following to create my dropdown: $theusers = ''; $theusers .= '<select name=\"adj[]\"><option value="">Please Select</option>'; while ($row = mysql_fetch_assoc($rst_adjusters)) { $theusers .= '<option value="' . $row['ID'] . '">' . $row['firstname'] . ' ' . $row['lastname'] . '</option>'; } $theusers .= '</select>'; The items appears correctly on my form but after I submit the form using $_POST the $_POST['adj'][$i]; variable doesn't contain anything? Can anyone help with this? <?php if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { $i = 0; while ($row = mysql_fetch_assoc($rstassign)) { $adjID = $_POST['adj'][$i]; if($adjID<>'Please Select') { $updateSQL = sprintf("UPDATE claimcue SET adj_id=555"); mysql_select_db($database_maxdbconn, $maxdbconn); $Result1 = mysql_query($updateSQL, $maxdbconn) or die(mysql_error()); } $i++; } } ?> A JM, Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 13, 2009 Share Posted June 13, 2009 The select box you created will only have one value submitted. http://www.tizag.com/phpT/forms.php If you were intending to make is a MULTI select box, you need to change your code. I suggest using a checkbox submit system instead, sending userIDs or something. Quote Link to comment Share on other sites More sharing options...
A JM Posted June 13, 2009 Author Share Posted June 13, 2009 Thanks for the post xtopolis. I think the intent is to a have a single item selected per record on the form. After submitting the form which has an method of "post" I'm expecting to have an array of option items namely "name=\"adj[]\"" but I'm not getting anything. The loop as listed below correctly loops through the records in my form using while ($row = mysql_fetch_assoc($rstassign)) which there are 10 items, just nothing in the array when trying to access via $_POST['adj'][$i]. I'm really struggling with this as it appears that everything is coded correctly just no variables in the expected array. Thanks for any help you can give. A JM, Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 13, 2009 Share Posted June 13, 2009 try. <?php if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { print_r($_POST['adj']); exit(); } ?> and see what you get.. and also, check in the generated HTML to see that the SELECT values are showing.. i.e. $row['id'] isn't returning a NULL value. Quote Link to comment Share on other sites More sharing options...
A JM Posted June 13, 2009 Author Share Posted June 13, 2009 I get a blank page. When pasting in your suggestion and changing the value to "Christmas" that is what prints. print_r("Christmas"); exit(); When viewing the source on the HTML form this is what it looks like: <td rowspan="2" valign="top"><select name=\"adj\"><option value="Please Select">Please Select</option><option value=29>Steve Jobs</option><option value=34>Bill Gates</option><option value=37>Santa Claus</option><option value=38>Jack Rabbit</option><option value=32>Roger Rabbit</option></select></td> Quote Link to comment Share on other sites More sharing options...
MikeDXUNL Posted June 13, 2009 Share Posted June 13, 2009 Not sure if this helps at all, but you don't need to escape the quotations when using single quotes $theusers .= '<select name="adj[]"><option value="">Please Select</option>'; Also, in your second piece of code, $i has no limit. something like while($i < count($someVAR) add this to your code and tell me what you get print_r($_POST['adj']); Quote Link to comment Share on other sites More sharing options...
A JM Posted June 14, 2009 Author Share Posted June 14, 2009 Not sure if this helps at all, but you don't need to escape the quotations when using single quotes $theusers .= '<select name="adj[]"><option value="">Please Select</option>'; BINGO! thank you - been looking for that for a while... arrgh.. Does "escape the quotations" mean if I had $adjID = $_POST["adj"][$i]; instead of $adjID = $_POST['adj'][$i]; that it would have worked? Shouldn't the while ($row = mysql_fetch_assoc($rstassign)) only loop through the number of rows in the recordset? I'm also seeing a new problem - the form is being re-drawn with only a single record after SUBMIT there were originally 10 records on the form. The 1st record is all that exists the other 9 are gone, does that make any sense to you? A JM, Quote Link to comment Share on other sites More sharing options...
A JM Posted June 14, 2009 Author Share Posted June 14, 2009 I'm also seeing a new problem - the form is being re-drawn with only a single record after SUBMIT there were originally 10 records on the form. The 1st record is all that exists the other 9 are gone, does that make any sense to you? Sorry to reply to my own post. I think I've finally figure out the routine but am still a little perplexed on the form re-draw issue but I think I know the problem with it, maybe? <?php $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { //print_r($_POST['adj']); //exit(); $i = 0; Do { $adjID = $_POST['adj'][$i]; if($adjID<>"Please Select") { $updateSQL = sprintf("UPDATE claimcue SET adj_id='" . $adjID . "' WHERE maxclaimnum=" . $row_rstassign['maxclaimnum']); mysql_select_db($database_maxdbconn, $maxdbconn); $Result1 = mysql_query($updateSQL, $maxdbconn) or die(mysql_error()); } $i++; } while ($row_rstassign = mysql_fetch_assoc($rstassign)); } ?> Does the form get redrawn because I'm calling the PHP function using the form action? action="<?php echo $editFormAction; ?>" Quote Link to comment Share on other sites More sharing options...
A JM Posted June 15, 2009 Author Share Posted June 15, 2009 I'm still trying to solve this problem and hoping someone can help... I'm wondering if using $editFormAction in my form is the way to go with this since my intent is to update the records in the database and remove those items from the users view on the form. The ideal for me would be to submit the form, update the records in the DB with the users selections of the dropdowns , then have the form redrawn. Is there a better way to do this? A JM, Quote Link to comment Share on other sites More sharing options...
A JM Posted June 15, 2009 Author Share Posted June 15, 2009 Can someone answer this for me. <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1"> <input type="submit" value="Post Assignments" /> When I use the above action on my form and click the submit button does that cause the form to re-run the entire page of PHP code? Since my form consists of multiple sections of PHP code I'm wondering how to handle just running certain sections? Thanks. Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 15, 2009 Share Posted June 15, 2009 you can post the form to the page it is on, and the corresponding php if statement (if isset($_POST[ETC])) will be executed... and then put header('location: '.basename($_SERVER['PHP_SELF'])); exit(); at the end which will get the browser to reload the page without any $_GET or $_POST variables...? i.e. <?php $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { //print_r($_POST['adj']); //exit(); $i = 0; Do { $adjID = $_POST['adj'][$i]; if($adjID<>"Please Select") { $updateSQL = sprintf("UPDATE claimcue SET adj_id='" . $adjID . "' WHERE maxclaimnum=" . $row_rstassign['maxclaimnum']); mysql_select_db($database_maxdbconn, $maxdbconn); $Result1 = mysql_query($updateSQL, $maxdbconn) or die(mysql_error()); } $i++; } while ($row_rstassign = mysql_fetch_assoc($rstassign)); //get page to reload so all user POST variables etc are flushed header('location: '.basename($_SERVER['PHP_SELF'])); exit(); } ?> is that what you were after? Quote Link to comment Share on other sites More sharing options...
A JM Posted June 15, 2009 Author Share Posted June 15, 2009 Thanks for the post joel24, Let me see if I somewhat understand... I can use the Submit button on the form to Post - so the $_POST['adj'] variables are populated and then stop the page from ??? (not really sure what it was doing) by using - header('location: '.basename($_SERVER['PHP_SELF'])); exit(); I'm still pretty new to PHP can you explain this a little more for me? From what I can tell - when submitting the form it was running my SQL UPDATE and then refreshing some portion of the page but I don't understand why it was only generating a partial recordset. I believe that fixed my problem, many thanks!!! Quote Link to comment 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.