SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    change form action URL on-the-fly

    Hey, I want to change a form action url based on user input, and I used hyperlink to submit a form (since I have lots of rows like this and each has different values passed to javascript select_claim() function.). But it doesn't work! I have very simple php code here, but the it is the javascript problem. Can anyone help? Thanks very much!

    HTML Code:
    <form action="" method="post" name="smForm" id="smForm">
    <table>
    <tr>
            <td><a href="" onclick="select_claim('<?=$claimNumber?>', '<?=$suffix?>', '<?=$claimDiag1?>', '<?=$claimDiag2?>', '<?=$claimClass?>', '<?=$claimProvider?>'); return getclaim('myclaimForm'); " title="view claim detail"><?=$claimNumber?></a></td>
    </tr></table>
    <input type="hidden" name="claimNumber" id="claimNumber" value="<?=$claimNumber?>" />
    <input type="hidden" name="suffix" id="suffix" value="<?=$selectedSuffix?>" />
    <input type="hidden" name="claimDiag1" id="claimDiag1" value="<?=$claimDiag1?>" />
    <input type="hidden" name="claimDiag2" id="claimDiag2" value="<?=$claimDiag2?>" />
    <input type="hidden" name="claimClass" id="claimClass" value="<?=$claimClass?>" />
    <input type="hidden" name="providerName" id="providerName" value="<?=$providerName?>" />
    </form>
    <script language="Javascript">
    <!--
    	function select_claim(claimnumber, suffix, claimdiag1, claimdiag2, claimclass, providername)
    {
    	getSection('claimNumber').value = claimnumber;
    	getSection('suffix').value = suffix;
    	getSection('claimDiag1').value = claimdiag1;
    	getSection('claimDiag2').value = claimdiag2;
    	getSection('claimClass').value = claimclass;
    	getSection('providerName').value = providername;
    }
    function getclaim(formName)
    {
    	if(getSection('claimClass').value == 'M')
    		document.forms[formName].action = 'claimDetailMed.php';
    	else if(getSection('claimClass').value == 'D')
    		document.forms[formName].action = 'claimDetailDen.php';
    	else if(getSection('claimClass').value == 'V')
    		document.forms[formName].action = 'claimDetailVis.php';
    	else(getSection('claimClass').value == 'P')
    		document.forms[formName].action = 'claimDetailPha.php';
    	document.forms[formName].submit();	
    	return true;
    	}
    }
    
    //-->
    </script>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's imperative that you employ some basic debugging techniques if you're going to write code. Otherwise you will struggle forever with simple things like this, when you should be able to find your problem and fix it in a matter of seconds. Some of the most basic ways to do this are to sanity check the values of your variables, and read the browsers error console. In your case, the error message will point you right at your problem.

    Once you find the problem line of code, you can start inspecting the values of your variables. You can use alert() to inspect the value of a variable, expression, or even the return value of a function.

    Why don't you go ahead and give that a shot. If you get stuck again, I can give you another nudge.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey, Crmalibu,

    Thanks for helping. I found that the if/else clause is wrong. and I added the last else statement:

    else
    document.forms[formName].action = 'home.php';

    but the form still doesn't submit. Any other hint? Thanks!

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What error do you get? I'll help you understand it.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    What error do you get? I'll help you understand it.

    The firebug didn't give me any error. However, the form just doesn't submit and stays in the current page (home.php) after I clicked the hyperlink.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be getting an error unless you have changed the code from what you posted.

    Do you have any other forms in this webpage?

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, let me post my code again. I don't have any other forms on this page, and this table contains more than one row just like this, "<tr></tr>" was put in a for loop, so for each hyperlink, it has different parameter values. And this page is called "home.php". So when I click any hyperlink, it still stays in home.php page.

    Code:
    <form action="" method="post" name="myclaimForm" id="myclaimForm">
    <table>
    <tr>
            <td><a href="" onclick="select_claim('<?=$claimNumber?>', '<?=$suffix?>', '<?=$claimDiag1?>', '<?=$claimDiag2?>', '<?=$claimClass?>', '<?=$claimProvider?>'); return getclaim('myclaimForm'); " title="view claim detail"><?=$claimNumber?></a></td>
    </tr></table>
    <input type="hidden" name="claimNumber" id="claimNumber" value="<?=$claimNumber?>" />
    <input type="hidden" name="suffix" id="suffix" value="<?=$selectedSuffix?>" />
    <input type="hidden" name="claimDiag1" id="claimDiag1" value="<?=$claimDiag1?>" />
    <input type="hidden" name="claimDiag2" id="claimDiag2" value="<?=$claimDiag2?>" />
    <input type="hidden" name="claimClass" id="claimClass" value="<?=$claimClass?>" />
    <input type="hidden" name="providerName" id="providerName" value="<?=$providerName?>" />
    </form>
    <script language="Javascript">
    <!--
    	function select_claim(claimnumber, suffix, claimdiag1, claimdiag2, claimclass, providername)
    {
    	getSection('claimNumber').value = claimnumber;
    	getSection('suffix').value = suffix;
    	getSection('claimDiag1').value = claimdiag1;
    	getSection('claimDiag2').value = claimdiag2;
    	getSection('claimClass').value = claimclass;
    	getSection('providerName').value = providername;
    }
    function getclaim(formName)
    {
    	if(getSection('claimClass').value == 'M')
    		document.forms[formName].action = 'claimDetailMed.php';
    	else if(getSection('claimClass').value == 'D')
    		document.forms[formName].action = 'claimDetailDen.php';
    	else if(getSection('claimClass').value == 'V')
    		document.forms[formName].action = 'claimDetailVis.php';
    	else(getSection('claimClass').value == 'P')
    		document.forms[formName].action = 'claimDetailPha.php';
                 else
    		document.forms[formName].action = 'home.php';
    	document.forms[formName].submit();	
    	return true;
    	}
    }
    
    //-->
    </script>

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where is you getSection() function defined? Does it work properly?

    In any case, you have an error here
    Code:
    document.forms[formName]
    alert(document.forms[formName])
    alert(formName) // no such form!

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is my getSection function, and it works properly as tested on other pages.

    function getSection (sectionID)
    {
    if (document.getElementById)
    return document.getElementById(sectionID);
    else if (document.all)
    return document.all[sectionID];
    else if (document.layers)
    return document.layers[sectionID];
    }

    How to fix the problem you mentioned? I tried to use:
    document.getElementById('myclaimForm').submit();
    but still don't work.

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found something weird. If I still keep using:

    document.forms[formName].submit();

    AND added after it:

    document.forms[formName].submit();
    alert(document.forms[formName]);

    the form submitted correctly with alert box show. "[object HTMLFormElement]"

    But if I comment alter out, like this:

    document.forms[formName].submit();
    //alert(document.forms[formName]);

    the form won't submit and stays in home.php page.

    ???????

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed the problem though I don't understand why. I used green color to highlight the difference. If you could explain to me why this works, I'll really appreciate it!

    Here is my fix:

    Code:
    <form action="" method="post" name="myclaimForm" id="myclaimForm">
    <table>
    <tr>
            <td><a href="" onclick="select_claim('<?=$claimNumber?>', '<?=$suffix?>', '<?=$claimDiag1?>', '<?=$claimDiag2?>', '<?=$claimClass?>', '<?=$claimProvider?>'); getclaim('myclaimForm'); return fasle;" title="view claim detail"><?=$claimNumber?></a></td>
    </tr></table>
    <input type="hidden" name="claimNumber" id="claimNumber" value="<?=$claimNumber?>" />
    <input type="hidden" name="suffix" id="suffix" value="<?=$selectedSuffix?>" />
    <input type="hidden" name="claimDiag1" id="claimDiag1" value="<?=$claimDiag1?>" />
    <input type="hidden" name="claimDiag2" id="claimDiag2" value="<?=$claimDiag2?>" />
    <input type="hidden" name="claimClass" id="claimClass" value="<?=$claimClass?>" />
    <input type="hidden" name="providerName" id="providerName" value="<?=$providerName?>" />
    </form>
    <script language="Javascript">
    <!--
    	function select_claim(claimnumber, suffix, claimdiag1, claimdiag2, claimclass, providername)
    {
    	getSection('claimNumber').value = claimnumber;
    	getSection('suffix').value = suffix;
    	getSection('claimDiag1').value = claimdiag1;
    	getSection('claimDiag2').value = claimdiag2;
    	getSection('claimClass').value = claimclass;
    	getSection('providerName').value = providername;
    }
    function getclaim(formName)
    {
    	if(getSection('claimClass').value == 'M')
    		document.forms[formName].action = 'claimDetailMed.php';
    	else if(getSection('claimClass').value == 'D')
    		document.forms[formName].action = 'claimDetailDen.php';
    	else if(getSection('claimClass').value == 'V')
    		document.forms[formName].action = 'claimDetailVis.php';
    	else(getSection('claimClass').value == 'P')
    		document.forms[formName].action = 'claimDetailPha.php';
                 else
    		document.forms[formName].action = 'home.php';
    	document.forms[formName].submit();	
    	//return true;
    	}
    }
    
    //-->
    </script>


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
  •