SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member jayg5000's Avatar
    Join Date
    Dec 2006
    Location
    USA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2 Forms and 1 Checkbox

    I have done some research on changing things with innerHTML but haven't quite grasped the concept. I have an html form that has a check box at the end. What i need it to do is change the "action" in the form tag to go one place if checked, and another place if not checked. I know how to do this in PHP but it would be nicer in javascript. Any Ideas?

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Don't change innerHTML, just the action:
    Code JavaScript:
    document.form1.action="page.php";

    BTW, it is not 'nicer' in JS -- what if it's turned off?
    Saul

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    <script type="text/javascript">
    function send(){
    var f = document.getElementById('myform');
    var el = document.getElementById('c1');
    if(el.checked) { f.action ="a.php"; }
    if(!el.checked) { f.action ="b.php"; }
    }
    </script>
    <form id="myform" method="post" onsubmit="return send()">
    <input type="checkbox" id="c1">
    <input type="submit" value="submit form">
    </form>

  4. #4
    SitePoint Member jayg5000's Avatar
    Join Date
    Dec 2006
    Location
    USA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code! That works perfectly. That is a good point about having JavaScript turned off. Do you consider that a big enough factor to not use something like this but to resort to php?

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jayg5000 View Post
    Thanks for the code! That works perfectly. That is a good point about having JavaScript turned off. Do you consider that a big enough factor to not use something like this but to resort to php?
    Definitely yes. It affects the site's functionality -- that should never be the case.
    Saul


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
  •