Solved

Sending Input Variable in Action Tag

Posted on 2003-12-03
8
457 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now