SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Links submitting a form and passing a variable

    I have a form and want to be able to submit it using 1 of 2 different javascript links. Now these links work using the onclick handler:

    Code:
    onclick="this.form.submit();">
    However, i want the script that deals with the form submission to be able to know which link has submitted the form. So like if you used an <input="submit"> type button you would get a value, can this be done for an onclick link?

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    <script type="text/javascript">
      function 
    callmyfunc(formname)
      {
        
    alert(formname);
      }
    </script>

    <form name="a" onsubmit="callmyfunc(this.form.name)">
    </form>
    <form name="b" onsubmit="callmyfunc(this.form.name)">
    </form> 
    something like that perhaps?

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think so. What I am trying to replicate is this:

    <form name="something">
    <input type="text" name="name">
    <input type="submit" name="do" value="button1">
    <input type="submit" name="do" value="button2">
    </form>

    now, the user can either click on the 1st or 2nd button. Either way, th script will get the value for the text field and also which button is pressed.

    I simply want to replace the form buttons with textlinks using the onclick() function.

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <form action="#" mathod="get" name="something">
    <
    input type="text" name="name">
    <
    input type="submit" name="do" value="button1">
    <
    input type="submit" name="do" value="button2">
    <
    a href="#" onclick="document.forms[0].action='index.php?target=' + this.innerHTML;alert(document.forms[0].action);">button3</a>
    </
    form
    that'll do it

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Arr, thanks i see how that would work. This is hard to explain but I can't unfortunatly use that method because I can't tell before hand what form elements there are going to be; and there will be a lot so hardcoding into a GET is not really going to work.

    Thinking about this I came up with this sort of idea:

    Code:
    <javascript>
    function submitit1() {
     var str = "<input type='hidden' name='do' value='' size='button1'>"
      frm.innerHTML += str
    this.form.submit();
    }
    function submitit2() {
     var str = "<input type='hidden' name='do' value='' size='button2'>"
      frm.innerHTML += str
    this.form.submit();
    }
    </javascript>
    
    <form action="#" method="get" name="something">
    <input type="text" name="name">
    <a href="#" onclick="submitit1()">button1</a>
    <a href="#" onclick="submitit2()">button2</a>
    </form>
    I imagine there are lots of errors there, going to try and test something like it. I can see already it could be made better using one function and an attribute which is the value of the hidden variable... etc.

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you using a server side language to make this, if so can you explain a lil more and i will be able to give you a better solution.

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I am. The bit making the form is rather complicated and I don't want to get into building a GET link to do it; there could be 50+ form elements. Not sure what other info I can give you that would help?

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it to work, basically before submitting the link generates a hidden form element which can be then used to determine which link was clicked. All the data is sent. If anyone has any improvements please do post them.

    Code:
    <script>
    function submitit(dowhat) {
    
    	var str = "<input type='hidden' name='do' value='" + dowhat + "'>"
    	document.getElementById("hiddenbit").innerHTML = str;
    	frm.submit();
    
    }
    
    </SCRIPT>
    
    <form action="test.php" method="post" name="frm">
    <input type="text" name="name">
    <a href="#" onclick="submitit('button1')">button1</a>
    <a href="#" onclick="submitit('button2')">button2</a>
    <span id="hiddenbit"></span>
    </form>


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
  •