I need to take a user-entered value from a form-field and append it as a URL parameter when the user hits {ENTER} or clicks the submit button. I'm new to DreamWeaver CS5, and I have a project that I'm getting pressure to complete.
I can think of three four ways to do it, and only one is two are a ColdFusion solution.
If you use method="get" in the FORM tag, all of the form fields and their values will be appended to the action URL in typical key=value pairs.
The second involves using JavaScript to change the value of the action attribute of the FORM tag in response to an onclick event (or perhaps onsubmit).
A ColdFusion method would to use CFLOCATION to redirect to the desired location. In other words, you want to end up on page2.html, so have the form action be page1.cfm and have in that CF template <cflocation url="page2.html?urlvar=#form.formvar#">.
Updated (courtesy of Ben Doom): On the form's action page, you could use the StructAppend() function to add the URL scope to the FORM scope.
Can you expand on your question a bit? Maybe we can get you a more complete answer.
Like #Al Everett said,
Only need to change method = "get" instead of "post" in form attribute.
Your simple HTML code should look like:
<html>
<head></head>
<body>
<cfif structKeyExists(form,"submit")> <!--- To ckeck if form is submiited --->
<cfoutput>Value = #url.mytextbox#</cfoutput>
</cfif>
<form name="myform" method="get" action=""> <!--- action is blank to submit on same page --->
<input type="text" name="mytextbox" />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Related
I have a django template which has multiple <a> tags.
<a class="label label-success" href="get_status/?token={{book.token}}">Update</a>
On click of it, a method from views is called where I can access the token from the url as
tkn = request.GET.get('token')
But now I want not to send the token in the url.
I searched for this and get to know about forms but I did not clearly understand them. Can anyone please help here.
For future ref: I created a form and added a hidden input field in it.
on click of submit button it will send the token value.
<form action="get_Status/" method="post">
{% csrf_token %}
{{ form }}
<input type="hidden" name="book_token" value="{{book.token}}">
<input type="submit" class="submit_btn btn label-success" value="Update" />
</form>
Ans in the views.py
book_token=request.POST.get("book_token"," ")
You can use the basic HTML form concept here.
Please check the link:
How to submit a form with JavaScript by clicking a link?
Use javascript/Jquery to submit the form.
Insert the token value in a hidden field and use form to submit it to views.
Then in the views,you can get the value as :request.POST['token']
I have an url like /documents/search?q=searchterm. On that page, I have an dropdown box where another searchterm can be added. That box has an onchange event and reloads the page:
<form><select name="w" class="form-control" onchange="this.form.submit();">
When the onchange event fires, the page is reloaded and the w parameter is added to the url, but the q parameter is lost. How can I tell my form, that on reload all the existing parameters are kept?
You need to include any other parameters in the form as hidden inputs:
<form>
<select name="w" class="form-control" onchange="this.form.submit();">
<input type="hidden" name="q" value="{{ search_term }}">
</form>
You need to make sure that search_term (obtained from the GET parameters) is in the context being passed to the template.
If possible you should consider using Django's Forms API to manage the form data.
How can i create a html form that action attribute have 2 destination.I want when user click on submit bottom , check if user entered wrong data the page goes to another pages with window.location and if user insert the correct input goes to main page with the same instruction.
First of all, what do you mean by correct input?
Main form data validation occurs in server side, not client side. you'd better use client side just for simple verification, like for typos.
There is no need for 2 destination pages (as you call it so).
You may use the standard action attribute which is the page on the server to which you are sending your form data.
there, You have the option to decide which condition needs what action and send the data (and then the user) to the desired page / action.
Sample code for the form
<form id='myform' action='action.php' method='POST' target='formresponse'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' maxlength="50" /><br/>
<label for='email' >Email Address*:</label><br/>
<input type='text' name='email' id='email' maxlength="50" /><br/>
<input type='button' name='Submit' value='Submit' />
</form>
<iframe name='formresponse' width='300' height='200'></frame>
Multiple action
function SubmitForm()
{
document.forms['contactus'].action='action1.php';
document.forms['contactus'].target='frame_result1';
document.forms['contactus'].submit();
document.forms['contactus'].action='action2.php';
document.forms['contactus'].target='frame_result2';
document.forms['contactus'].submit();
return true;
}
In django, I'm having problems passing the number that was inputed into a form to an URL:
I try like this:
templates->index.html
<form action="/mysite/{{ number }}/details.html" method="post">
<p><label for="number">Give me a number:</label>
<input type="text" name="number" id="number" /></p> <input type="submit" value="Submit" />
but the result is /mysite//details.html, instead of /mysite/123/details.html (when the user inputs 123 in the requested form)
Can you please give me an advise?
Thanks,
JJ
You shouldn't be configuring your action url in this manner. And the user POST-ed number 123 is passed in to your view function as request.POST['number'] without you specifying it in the action url.
Your action url can simply be {% url 'send_details' %} corresponding to a url definition that is
url(r'^mysite/details/$',
'send_details',
name='send_details'),
And your send_details view function will receive request.POST['number'] when the user submits it.
There's no necessity to have the number in your template as a context variable.
I'm using Django and I am having trouble passing variable information back to the server without modifying the url. Here's what I have which works the way I want except that I want the action to be /foo:
<form method="post" action="/foo/{{ variable }}">
<input type="submit" value="bar"/>
</form>
When I do this I can easily get the variable when I parse the url. Instead, I want to add the variable to the POST QueryDict. Is this possible?
Yes. just add additional input fields:
<form method="post" action="/foo/">
<input type="hidden" value="{{ variable }}" name="name_of_var">
<input type="submit" value="bar"/>
</form>
Then you can access the variable in the view by:
request.POST['name_of_var']
or if you are not sure if the variable exists. This will not raise exception is does not exist.
request.POST.get('name_of_var')
Just make an input inside your form:
<input type="hidden" value="{{ variable }}" name="foo" />
Then on a view:
foo = request.POST.get('foo', None)
Also, I recomend you to use url tag for action.
Simply add a hidden input element with the name and value you need like so:
<input type="hidden" name="varname" value="myvalue" />
Can you not make the variable an input field in the form? You can make it hidden to avoid overriding it. This will post the data in the submit and achieve what you want.
Isn't this what the Django sessions framework is for? You can pass data back and forth without trying to pass it through a url.
The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies.
https://docs.djangoproject.com/en/1.5/topics/http/sessions/