Hi I have a php file that passes some values to the next page, after visitor clicks on a link. But my problem is that I would like to make sure that the visitor has chosen a value from a drop down list before I redirect the visitor to the next page. If I check the result before visitor clicks on the link, it could be that he/she was going to select an option from list any way. If I check after clicking the link, by then its too late and the page gets diverted, even if I display a message. Can some one please help me in this matter. Your help is much appreciated. jacka
Do the validation on the same page as the drop down list, if it validates then redirect them using the header() function to the next page. Look at the code on this page to see how this can be achieved: http://forums.digitalpoint.com/showthread.php?t=462514 Brew
Hi Brew many thanks for replying to my message so quickly. I htought I better post some of my code so you see excatly what I am trying to do. I have a simple drop down list and a validation test in javascript. Then I have a image link that transfers some values to the another page. How can I combine these two actions together, so it tests the javascript and if it is ok, passes the values in php. <FORM ACTION="test.asp" NAME="testform"> <SELECT NAME="Make"> <OPTION VALUE="0" SELECTED>Select One</OPTION> <OPTION VALUE="1">Ford</OPTION> <OPTION VALUE="2">Chevy</OPTION> <OPTION VALUE="3">Pontiac</OPTION> <OPTION VALUE="4">Dodge</OPTION> </SELECT> Code (markup): and further down ----- <td colspan="3"> <a href="../HiQFM2-0/test15.php?totalCost=<?php echo number_format($totalCost, 2, ".", ","); ?>&vat=<?php echo number_format($vat, 2, ".", ","); ?>& delivery=<?php echo number_format($delivery, 2, ".", ","); ?>& Gt=<?php echo number_format($Gt, 2, ".", ","); ?>"><img src="../images/checkoutoff.gif" alt="finished ordering" id="Image2" onMouseOver="MM_swapImage('Image2','','../images/checkouton.gif',1)" onMouseOut="MM_swapImgRestore()"></a> <INPUT TYPE="BUTTON" VALUE="Send form" onClick="validateForm(document.testform)"> </td> </tr> </FORM> </table> Code (markup): thanks jacka
I suppose I can combine the two lines together like so: <a onClick="validateForm(document.testform)" href="../HiQFM2-0/test15.php?totalCost=<?php echo number_format($totalCost, 2, ".", ","); ?>&vat=<?php echo number_format($vat, 2, ".", ","); ?>& delivery=<?php echo number_format($delivery, 2, ".", ","); ?>& Gt=<?php echo number_format($Gt, 2, ".", ","); ?>"><img src="../images/checkoutoff.gif" alt="finished ordering" id="Image2" onMouseOver="MM_swapImage('Image2','','../images/checkouton.gif',1)" onMouseOut="MM_swapImgRestore()"></a> Code (markup): but when a selection has not been made, an alert is displayed and the rest of the instruction is carried out. What I really want is after the alert (that no selction has been made) for the program to stop carriing on the rest of the instruction, such as displaying the next page. Any ideas plz? thanks
Hi Sure, here it is: function validateForm(objForm) { var returnStatus = 1; if (objForm.Make.selectedIndex == 0) { alert("Please select a car make"); returnStatus = 0; }; if (returnStatus) { objForm.submit(); } } Code (markup):
Try this: function validateForm(objForm) { var returnStatus = 1; if (objForm.Make.selectedIndex == 0) { alert("Please select a car make"); returnStatus = 0; }; if (returnStatus) { objForm.submit(); } return false; } Code (markup): Brew
Hi tried that but it still doens't work. It displays the alert and then goes to the next page. thanks jacka p.s. I am thinking if may be a better idea to load the country list in a database and use it that way, beause I will be needing to examine which option they have selected later and take action accordingly.. what do you think?
Just replace onClick="validateForm(document.testform)" with onClick="validateForm(document.testform); return false;"
Hi MMJ You got it spot on. One simple statement was all that was needed and it got me puzzled for days. Thats one problem solved. May I please ask you another question. At the moment all I do is pass some php values to the next page, when visitor clicks on the checkout link. With this drop down addition, what now I have done is make sure that the visitor doesn't forget to make a choice from the new list. The question is how can I find out what choice the visitor has made using php. , because all my other calculatiosn are in php. But i do not want to pass this new value from the drop down list to the next page.I just want to use it for internal calculation in this page. I am prepared to make the list in php form using values stored in a mysql database. ( If you really wnat to know its a list of countries I display and I want to know which country the vistior has selected form list. I have kept the list simpel by displaying a simple car list) Many thanks.
Note that using the submit() method isn't a good practice. Better would be: function validateForm(objForm) { if (objForm.Make.selectedIndex == 0) { alert("Please select a car make"); return false; } return true; } Code (javascript): And in the attribute: onClick="return validateForm(document.testform);" Code (markup):
Hi nico_swd I was just about to post a message saying that MMJ code did not work properly, becuase I think the reason is that it always returned a "return false". But you code worKed a treat, I am grateful. Can you please advise of my next stage of work which is, I want to replace that simple car list with a country list and would like to use the country that a visitor choses to make some calculation in this page. I prefer to use php if possible, as I know slightly more than javascript (which is none). Once again, your help is much appreciated. jacka