Catch 22. Too late for validation?

Discussion in 'PHP' started by jacka, Sep 4, 2007.

  1. #1
    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
     
    jacka, Sep 4, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Brewster, Sep 4, 2007 IP
  3. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    jacka, Sep 4, 2007 IP
  4. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    jacka, Sep 4, 2007 IP
  5. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Can you post the code for validateForm

    Brew
     
    Brewster, Sep 4, 2007 IP
  6. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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):
     
    jacka, Sep 4, 2007 IP
  7. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #7
    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
     
    Brewster, Sep 4, 2007 IP
  8. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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?
     
    jacka, Sep 5, 2007 IP
  9. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Just replace onClick="validateForm(document.testform)" with onClick="validateForm(document.testform); return false;"
     
    MMJ, Sep 5, 2007 IP
  10. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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.
     
    jacka, Sep 6, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    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):
     
    nico_swd, Sep 6, 2007 IP
  12. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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
     
    jacka, Sep 6, 2007 IP
  13. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Make a new thread for that. :)
     
    MMJ, Sep 6, 2007 IP