Solved

Sending Input Variable in Action Tag

Posted on 2003-12-03
8
460 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python - desktop use 1 33
How would I do a website like this? 5 60
Html5 Index on a table 7 29
div to fit another div 8 23
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

831 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