elmas156 Posted November 19, 2013 Share Posted November 19, 2013 Hey everyone, I have a page that I created for users to select certain products to reserve. Each product is listed and a form is created using a while loop. For some of the products, the user selects to use it wet or dry using a drop down menu. I also have a javascript function that validates the forms that give the option to select to use the product wet or dry, so that if the user does not select one of these options, a popup will apear prompting them to do so. The problem that I'm having is that ON ALL EXCEPT THE FIRST FORM/PRODUCT PRODUCED FROM THE WHILE LOOP, the form validation produces a popup prompting the user to select the wet or dry option even if the option is selected. THE FIRST FORM VALIDATION ON THE PAGE WORKS AS IT SHOULD. I'm obviously doing something wrong, but I have no idea what. I've worked on it for two days so now I'm asking for help. My code is listed below. Thanks in advance. PHP code: <?php $resultprod = mysql_query("SELECT `inservice`,`prodid`,`prodname` FROM products ORDER BY `order` ASC") or die (mysql_error()); while ($rowprod = mysql_fetch_row($resultprod)) { $inservice = $rowprod[0]; $prodid = $rowprod[1]; $prodname = $rowprod[2]; $resultres = mysql_query("SELECT * FROM reservations WHERE `prodid` = '$prodid' AND `resdate` = '$resdate'") or die (mysql_error()); $checkres = mysql_num_rows($resultres); if ($checkres == 0) { $smallprod = $prodid ."small"; if(substr($prodid,0,2) == 'WD') { echo "<div id='test'>"; echo "<img src='images/$smallprod.jpg' width='470' height='250' border='0' title='Reserve the $prodname now!' />"; echo "<div id='test3'>"; //this is the beginning of the form(s) that I'm having problems with validating echo "<form name='reserve' action='custform.php' method='GET' onsubmit='return validateWetDry()'>"; echo "<select style='height:20px; width:175px; font-size:13px;' name='water'>"; echo "<option value='0' selected>- Choose Wet or Dry -</option>"; echo "<option value='y'>Wet Option</option>"; echo "<option value='n'>Dry Option</option>"; echo "</select><font size='-2'><br /> <br /></font>"; echo "<input type='hidden' name='resdate' value='$resdate' />"; echo "<input type='hidden' name='prodid' value='$prodid' />"; echo "<input type='submit' style='height:30px; width:175px; font-size:14px;' name='submit' value=' Reserve This Unit Now ' />"; echo "</form>"; //this is the end of the form(s) that I'm having problems with validating echo "</div>"; echo "</div>"; } else { echo "<div id='test'>"; echo "<img src='images/$smallprod.jpg' width='470' height='250' border='0' title='Reserve the $prodname now!' />"; echo "<div id='test2'>"; echo "<form name='reserve' action='custform.php' method='GET'>"; echo "<input type='hidden' name='water' value='n' />"; echo "<input type='hidden' name='resdate' value='$resdate' />"; echo "<input type='hidden' name='prodid' value='$prodid' />"; echo "<input type='submit' style='height:30px; width:175px; font-size:14px;' name='submit' value=' Reserve This Unit Now ' />"; echo "</form>"; echo "</div>"; echo "</div>"; } } else { $smallprodres = $prodid . "smallres"; echo "<img src='images/$smallprodres.jpg' width='470' height='250' border='0' title='The $prodname is not available for $resdate' />"; } ?> Javascript Code: function validateWetDry() { var water=document.forms["reserve"]["water"].value if (water==null || water=="0") { alert("Please tell us if you will need to use this unit wet or dry."); return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/284063-forms-produced-and-validated-in-while-loop/ Share on other sites More sharing options...
Solution Ch0cu3r Posted November 19, 2013 Solution Share Posted November 19, 2013 (edited) Your validateWetDry jhavascript function will have to loop through all forms named reserve and validate the water value for that field An easier way would be to change the function to function validateWetDry(target_form) { var water = target_form['water'].value; if (water==null || water=="0") { alert("Please tell us if you will need to use this unit wet or dry."); return false; } } And then change the onsubmit to return validateWetDry(this) Edited November 19, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284063-forms-produced-and-validated-in-while-loop/#findComment-1459040 Share on other sites More sharing options...
elmas156 Posted November 19, 2013 Author Share Posted November 19, 2013 Thanks so much! It finally works. I would have been working on this for days. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/284063-forms-produced-and-validated-in-while-loop/#findComment-1459050 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.