StefanRSA Posted August 6, 2009 Share Posted August 6, 2009 I am trying to do validate 4 fields... Province and city is compulsory and can't be empty and if price is selected I need the user to input a value in the rand field... All works fine for province if left empty... No error is then given if city field is left empty and the validation for the price and rand working??? Why not? Please help.... My script look like this: <form action="myadin.php" method="post" enctype="multipart/form-data" onsubmit="return checkform(this);"> <script language="JavaScript" type="text/javascript"> <!-- function checkform ( form ) { // ** START ** if (form.province.value == "") { alert( "Please select a province." ); form.province.focus(); return false ; } if (form.city.value == "") { alert( "Please select a town." ); form.city.focus(); return false ; } if (form.getElementById("price").checked) { if (form.getElementById("rand").value.length > 0) { return true } else { alert("You must enter the Price") return false } } // ** END ** return true ; } //--> </script> Quote Link to comment Share on other sites More sharing options...
wemustdesign Posted August 6, 2009 Share Posted August 6, 2009 The error may be in your form. Can you post the code for this Quote Link to comment Share on other sites More sharing options...
StefanRSA Posted August 6, 2009 Author Share Posted August 6, 2009 Hi, I have the complete form as follow: <form action="myadin.php" method="post" enctype="multipart/form-data" onsubmit="return checkform(this);"> <? $links = mysql_query("SELECT * FROM adcat WHERE disabled<>'1' and id ='$catid'") or died("Database Query Error"); $links2 = mysql_query("SELECT * FROM adsubcat WHERE id ='$adsubcat'") or died("Database Query Error"); $db1 = mysql_fetch_array($links); $db2 = mysql_fetch_array($links2); $pictot = $db2[pictot]; if ($pictot <= 0){ $pictot = 4; } Echo '<div id="myadheader" style="background-color:'.$db1[catcol1].'" text-align:"Left";>'.$brand.' '.$_SESSION['s_username'].' Ad '.$adnumber.' > '.$db1[name].' > '.$db2[name].' >> EDIT</div>'; include 'adstyle.php'; ?> <div id="freemyads"> <table class='myadtable' align='center'> <tr> <th colspan="6">TIP: Complete all fields as accurately as possible to attract more visitors to your ad!</th> </tr> <tr> <td>-</td><td>-</td><td>-</td><td>-</td><td>Ad Title</td><td><input name="title" type="text" class="freeadtextfield" id="title" value="<? echo $title;?>"></td> </tr> <!-- Province goes here --> <tr> <td>-</td><td>-</td><td>-</td><td>-</td><td>Province</td><td> <select name="province" value ="" onChange="getCity('../member/townsrc.php?province='+this.value)"> <option value="">Select Province</option> <option value="1" <?if ($province == '1') {echo 'SELECTED';}?>>KwaZulu-Natal</option> <option value="2" <?if ($province == '2') {echo 'SELECTED';}?>>Western Cape</option> <option value="3" <?if ($province == '3') {echo 'SELECTED';}?>>Eastern Cape</option> <option value="4" <?if ($province == '4') {echo 'SELECTED';}?>>Free State</option> <option value="5" <?if ($province == '5') {echo 'SELECTED';}?>>Gauteng</option> <option value="6" <?if ($province == '6') {echo 'SELECTED';}?>>Limpopo</option> <option value="7" <?if ($province == '7') {echo 'SELECTED';}?>>Mpumalanga</option> <option value="8" <?if ($province == '8') {echo 'SELECTED';}?>>North West</option> <option value="9" <?if ($province == '9') {echo 'SELECTED';}?>>Northern Cape</option> </select> </td> </tr> <!-- Province ends here --> <!-- Town goes here --> <? $townquery=("SELECT search_town.townLabel AS town FROM search_town JOIN search_region ON search_region.regionId=search_town.relRegionId JOIN search_prov ON search_prov.provId=search_region.relProvId WHERE search_prov.provId=$province ORDER BY town ASC"); $townresult=mysql_query($townquery); ?> <tr> <td>-</td><td>-</td><td>-</td><td>-</td><td>Town</td><td> <div id="citydiv"><select name="city"> <option value="" >Select Town</option> <? while ($towngroup = mysql_fetch_array($townresult)){ $townresults=$towngroup['town']; if ($town==$townresults){ $selected='SELECTED'; } else { $selected=''; } echo '<option value="'.$townresults.'" '.$selected.'>'.$townresults.'</option>'; } ?> </select> </div> </td> </tr> <!-- Town ends here --> <tr> <td>-</td><td>-</td><td>-</td><td>-</td><td>Price</td><td> <!-- Price Field Start Here --> <script language="JavaScript" type="text/javascript"> <!-- function checkform ( form ) { // ** START ** if (form.province.value == "") { alert( "Please select a province." ); form.province.focus(); return false ; } if (form.city.value == "") { alert( "Please select a town." ); form.city.focus(); return false ; } if (form.getElementById(price).checked) { if (form.getElementById(rand).value.length > 0) { return true } else { alert("You must enter the Price") return false } } // ** END ** return true ; } //--> </script> <input type="radio" name="radio_options" id="price" value="price" <? if ($radio_options == 'price') { echo 'CHECKED'; } ?> /> Price R <input type="text" name="rand" id="rand" value="<?echo $rand;?>"/><br /> <input type="radio" name="radio_options" id="free" value="option_1" <? if ($radio_options == 'option_1') { echo 'CHECKED'; } ?>/> Free<br /> <input type="radio" name="radio_options" id="contact" value="option_2" <? if ($radio_options == 'option_2') { echo 'CHECKED'; } ?>/> Make Contact<br /> <input type="radio" name="radio_options" id="swap" value="option_3" <? if ($radio_options == 'option_3') { echo 'CHECKED'; } ?>/> Swap / Trade<br /> <!-- Price Field END Here --> </td> </tr> <tr> <!-- Comment Box Start HERE --> <td>-</td><td>-</td><td>-</td><td>-</td><td>Text</td><td><textarea class="freeadboxfield" name="comment" rows="8" cols="41"><? echo $comment; ?></textarea></td> </tr> <!-- Comment Box END HERE --> <tr> </table><br> <? $picuser = $_SESSION['s_username']; //////////////////////////////////// IMAGE UPLOAD SECTION START //include 'image_up.php'; //Echo '<div id="myadheader" style="background-color:'.$db1[catcol1].'" text-align:"Left";>Upload your ad images. (Max 4 images allowed)</div>'; $imaq = mysql_query("SELECT * FROM ad_image WHERE ad ='$adnr' AND user = '$picuser'") or died("Database Query Error"); ?> <table class='myadtable' align='center'> <tr> <th colspan="6">TIP: Ads with images attract more visitors! (Up to <?echo $pictot; ?> images Allowed!)</th> </tr> <tr> <td colspan="6"><center> <? while($image_dir = mysql_fetch_array($imaq)){ $pic = $image_dir['image']; //echo $pic; // //echo $adnr; echo '<a href="../ad_images/'.$pic.'"><img src="pic.php?w=150&img=../ad_images/'.$pic.'" border="1px" /></a><br><a href="../member/myadin.php?edit=ad&ad='.$adnr.'&delete=image&pic='.$pic.'">Remove</a><br>'; } ?></center></td> </tr> <? $number=mysql_num_rows($imaq); $newpic = $pictot - $number; for($i=1;$i<=$newpic;$i++){ echo '<tr> <td>-</td><td>-</td><td>-</td><td>-</td><td>Image '.$i.'</td><td><input type="file" name="img'.$i.'"></td> </tr>'; } ////// /////// echo "<input id='adnr' name='adnr' input type='hidden' value='$adnr'>"; echo "<input id='catid' name='catid' input type='hidden' value='$catid'>"; echo "<input id='adsubcat' name='adsubcat' input type='hidden' value='$adsubcat'>"; ?> <tr> <td>-</td><td>-</td><td>-</td><td>-</td><td></td><td><input type="submit" name="submitad" class="submitlarge" value="Submit Ad"></td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 instead of checking if the value is empty by comparing it to an empty string, try checking the length of the value, IE: if (form.city.value.length < 1) { alert( "Please select a town." ); form.city.focus(); return false ; } beyond that it looks fine to me Quote Link to comment Share on other sites More sharing options...
StefanRSA Posted August 6, 2009 Author Share Posted August 6, 2009 Thanks a mil mikesta707. This actually did not fix my problem but put me in the right direction in fixing it. My problem was my default values that change with my AJAX on select function... 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.