Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form variable without Submit

    Is it possible to get a variable through GET or POST without pressing a Submit button? Maybe without having a FORM tag?
    I have this textfield
    Code:
    <input name="date_a" type="text" id="date_a" />
    and then the following links into a table taken from a db query
    Code:
    <a href="show_data.php?st=link_1">link_1</a><br />
    <a href="show_data.php?st=link_2">link_2</a><br />
    <a href="show_data.php?st=link_3">link_3</a><br />
    Along with ?link_x I must have the value of date_a and maybe more of them from inside the textfield in show_data.php to make the calculations. Is that possible?

    Thank you.
    Last edited by nst; 02-26-2007 at 09:06 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you have an <input> tag then you must have a <form> tag. (For valid HTML anyway)

    If you don't mind using Javascript then you can have the link call a JS function which then submits the form.
    PHP Code:
    <a href="#" onclick="submitForm();">

    <
    script type="text/javascript">

    function 
    submitForm() {
        
    document.myForm.submit();


  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Of course since you are not linking that link to anything an <a> tag is not the right one to use. You could use a <span> tag instead and then you won't have any problems with the link reloading the current page.

    <span onclick=="submitForm();">Submit</span>

    Then you just need to style the span so people know to click on it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mmm.., what about the value of the link?
    As I wrote above
    Code:
    <a href="show_data.php?st=link_1">link_1</a><br />
    They don't seem to work together.
    Here is the whole of it

    Code:
    <body>
    <form  name="linked_form" action="show_data.php" method="get"id="show_data">
    
    <input name="date_a" type="text" id="date_a" value="fff" />
    <P>
    <a href="show_data.php?st=link_1" onclick="submitForm();">link_1</a><br />
    <a href="show_data.php?st=link_2" onclick="submitForm();">link_2</a><br />
    <a href="show_data.php?st=link_3" onclick="submitForm();">link_3</a><br />
    
    <script type="text/javascript">
    function submitForm() {
        document.linked_form.submit();
    }  
    </script>
    </form>
    </body>

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That's a good idea, to use <span> rather than <a>.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I assume you only care about the ?st=link_1 part of the url, since the value of your form's action handles the navigation to show_data.php.

    You can use a hidden input field named "st" and then set its value before you submit:

    PHP Code:
    <body>
    <
    form  name="linked_form" action="show_data.php" method="get" id="show_data">

    <
    input name="date_a" type="text" id="date_a" value="fff" />
    <
    input type="hidden" name="st">
    <
    P>
    <
    span onclick="submitForm('link_1');">link_1</span><br />
    <
    span onclick="submitForm('link_2');">link_2</span><br />
    <
    span onclick="submitForm('link_3');">link_3</span><br />

    <
    script type="text/javascript">
    function 
    submitForm(stVal) {
        
    document.linked_form.st.value stVal;
        
    document.linked_form.submit();
    }  
    </script>
    </form>
    </body> 
    Last edited by Fumigator; 02-26-2007 at 09:33 PM.

  • #7
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works. Great help, thanks to both of you!
    I will now read more about span.


  •  

    Posting Permissions

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