SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do I call another program (page) from inside Javascript?

    Hello,

    I have read the book by Kevin Yank called simply Javascript and am having problem with 2 things that I need my code to do. And unfortunately Kevin's book does not cover these 2 very important things!

    So 1, I have a form named "frm". This form is going to, as all forms do, collect a bunch of data and then I want these data submitted to the next page, lets call it next.jsp. That is the form action method is:
    action="next.jsp?type=<%=type%>"

    However I 1st want to check all the form fields that have been marked as
    required to make sure that they have a value, if not then display an alert message to the user and stay in current page until they fill all required fields. If yes, that is the Javacsript code tells me that all the fields are filled, then I want the action part of the method to fire.

    How do I best do these 2 things using Javascript?

    And FYI, my Javascript code that checks to make sure all form fields are filled is this:

    Code JavaScript:
    <script language="JavaScript">
     
    function checkInput(input)
    {
        if (input!=null && input.length>0) return true; 
        else 
    	return false;
    }
     
    function CheckFrm()
    {
    	var error_message="";
              var data = document.frm;
        if(!checkInput(data.namev.value)) { error_message = " - V name\n"; }
    	if(!checkInput(data.namen.value)) { error_message = error_message + " - N name\n"; }
    	if(!checkInput(data.email.value)) { error_message = error_message + " - Email\n"; }	
    	}
     
    	if(error_message != "")
             {
    		alert("Please check these fields:\n"+error_message);
    		return false;
    	}
     
        return true;
    }
    </script>

    I appreciate your help.
    Regards,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    However I 1st want to check all the form fields that have been marked as
    required to make sure that they have a value, if not then display an alert message to the user and stay in current page until they fill all required fields. If yes, that is the Javacsript code tells me that all the fields are filled, then I want the action part of the method to fire.
    Umm! I am not sure you mean this or not:
    HTML Code:
    <form name="frm" action="next.jsp?type=<&#37;=type%>" method="post" onSubmit="return CheckFrm();">
    .......
    </form>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Raju,

    Your suggestion partially works.
    That is next.jsp is only called if checkfrm() returns true.
    However there is a problem: that is any form field that is Radio
    based in considered as not having a value, even thought the user
    has selected a value!
    So how do I get around this bug in Javascript?
    And check for values of Radio fields having been assigned or not?

    Regards,



    Quote Originally Posted by rajug View Post
    Umm! I am not sure you mean this or not:
    HTML Code:
    <form name="frm" action="next.jsp?type=<%=type%>" method="post" onSubmit="return CheckFrm();">
    .......
    </form>

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    And one more related question

    Hello Raju,

    And one more question:
    how do I check for a condition regarding the form data and based on the condition met send the form data via post to one file or another.
    I mean like this:

    1- when user clicks Submit button on the form then call a Javascript program named checkfrm()

    2- this Javascript program then is to check on Condition 1 & Condition 2,
    IF Condition 1 is true then call page1.php via method post with the data of the form that the user filled,
    IF Condition 2 is true then call page2.php via method post with the data of the form that the user filled,

    Regards,

    Quote Originally Posted by rajug View Post
    Umm! I am not sure you mean this or not:
    HTML Code:
    <form name="frm" action="next.jsp?type=<%=type%>" method="post" onSubmit="return CheckFrm();">
    .......
    </form>

    Anoox search engine volunteer

    www.anoox.com

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you may want like this:
    Code javascript:
    function doSubmit(frm){
    	if(frm.sex[0].checked == false && frm.sex[1].checked == false){
    		alert('Please select sex.');
    		return false;
    	}
    	else{
    		if(frm.sex[0].checked == true){
    			frm.method = "post";
    			frm.action = "page1.php?val=1";
    			frm.submit();
    		}
    		else if(frm.sex[1].checked == true){
    			frm.method = "post";
    			frm.action = "page2.php?val=1";
    			frm.submit();
    		}
    	}
    }
    HTML Code:
    <form name="frm1" id="frm1">
    	Sex : <input type="radio" name="sex" value="Male" /> Male <input type="radio" name="sex" value="Female" /> Female
        <br>
    	<input type="button" name="submit" id="submit" value="Submit" onClick="doSubmit(this.form)" />
    </form>
    The radio buttons are treated as array in javascript so you need to check them likewise.

    Hope this will help you to understand and to solve the problem as well.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello rajug,

    Thanx for your insight in this regard.
    I will try it in the next day or so.
    However I am having a more urgent problem with a MySQL issue
    which I need to address 1st.
    BTW, I am going to post this under MySQL, if you would like
    to reply to it there that would great.

    Best regards,

    Quote Originally Posted by rajug View Post
    So you may want like this:
    Code javascript:
    function doSubmit(frm){
    	if(frm.sex[0].checked == false && frm.sex[1].checked == false){
    		alert('Please select sex.');
    		return false;
    	}
    	else{
    		if(frm.sex[0].checked == true){
    			frm.method = "post";
    			frm.action = "page1.php?val=1";
    			frm.submit();
    		}
    		else if(frm.sex[1].checked == true){
    			frm.method = "post";
    			frm.action = "page2.php?val=1";
    			frm.submit();
    		}
    	}
    }
    HTML Code:
    <form name="frm1" id="frm1">
    	Sex : <input type="radio" name="sex" value="Male" /> Male <input type="radio" name="sex" value="Female" /> Female
        <br>
    	<input type="button" name="submit" id="submit" value="Submit" onClick="doSubmit(this.form)" />
    </form>
    The radio buttons are treated as array in javascript so you need to check them likewise.

    Hope this will help you to understand and to solve the problem as well.

    Anoox search engine volunteer

    www.anoox.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •