Solved

Sending Input Variable in Action Tag

Posted on 2003-12-03
8
462 Views
Last Modified: 2010-04-09
How do I send the form input as a parameter in an action statement?  Below testSearchValue validates the input.  If all is well, that searchText variable needs to be sent to search.html.  Naturally below the text string "searchText" is being passed.  I need its value to be passed.  Thanks!

<form method="post" onsubmit="return testSearchValue(this)" action="search.html?page_number=1&user_input=searchText">
      <input name="searchText" type="text" size="30"/>
      <input type="submit" value="Go!"/>
      <input type="reset" value="Reset"/>
</form>
0
Comment
Question by:bparmelee
8 Comments
 
LVL 3

Expert Comment

by:mrh30
ID: 9873067
It's much more simple than you're making it!

<form method="post" action="search.html">
    <input name="searchText" type="text" size="30" />
    <input name="page_number" type="hidden" value="1" />
    <input type="submit" value="Go!" />
    <input type="reset" value="Reset" />
</form>

And believe it or not, both searchText and page_number will be submitted as CGI parameters when the form is submitted.  I don't know what language you're writing in, but in Perl you'd just be able to pull the parameters out with $q->param('searchText') and $q->param('page_number') statements.
0
 
LVL 3

Accepted Solution

by:
daveroydunn earned 500 total points
ID: 9873437
If you want the variables to appear at the end of the address you need to use the GET method.

<form method="get" onsubmit="return testSearchValue(this)" action="search.html">
     <input name="searchText" type="text" size="30"/>
     <input type="submit" value="Go!"/>
     <input type="reset" value="Reset"/>
</form>

Alternatively if you don't want the variables to appear at the end of the address you need to use the POST method.

<form method="post" onsubmit="return testSearchValue(this)" action="search.html">
     <input name="searchText" type="text" size="30"/>
     <input type="submit" value="Go!"/>
     <input type="reset" value="Reset"/>
</form>

Using the POST method means that any processing of the variables would need to be done Server Side.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 9874705
But if you really want them to be on the querystring using post, you will have to change the action of the form dynamically in the testSearchValue() function.  In that function, just before you return true, change the action like this:

 function testSearchValue(theForm)
 {
    // your validation code ...

    // ... then if everything validates:
 
    var page_number = 1;  // or however you get this info
    var user_input = "searchText";
    theForm.action = "search.html?page_number=" + page_number + "&user_input=" + searchText;
    return true;
 }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:knightEknight
ID: 9874722
OOPS ... typo in the most important line!   should be:

   theForm.action = "search.html?page_number=" + page_number + "&user_input=" +  user_input;

(use the variable name at the end)
0
 
LVL 3

Expert Comment

by:mrh30
ID: 9874742
But as Daveroy has already pointed out, you can do that with GET.  There's not a lot of point in having a convoluted way of doing things when the easy way is supplied for you!
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 9875034
true, but GET has limitations on how much data it can pass.  It is sometimes convenient to pass some data in the form and some on the querystring.  I do this all the time in ASP to keep the form data collection seperate from the page control data collection.  However, since that does not appear to be how it is being used here, I prefer your suggestion of using hidden fields.   But my suggestion above is another alternative, and one that addresses the question directly.
0
 
LVL 3

Expert Comment

by:mrh30
ID: 9875061
Yes, I agree with you.  A lot of the work we do involves websites passing quite a lot of CGI data around, so we almost always use POST and several hidden fields.
0
 

Author Comment

by:bparmelee
ID: 9879139
Thanks to all!  The GET worked.  I originally used the Javascript method above, but it seemed to not want to work with PHP.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Filktering Alphabetically 8 41
HTML5 storage and web apps 11 43
Date on a table 16 33
Check input text, Number 6 26
This article discusses four methods for overlaying images in a container on a web page
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question