Jump to content

Forms produced and validated in While Loop


elmas156
Go to solution Solved by Ch0cu3r,

Recommended Posts

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;
	}
	
}
Link to comment
Share on other sites

  • Solution

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.