Simple Javascript error - Form submit doesn't append querystring - html

I want to submit a form to a URL appending few values as the querystring.
I am using this to call;
<form id="form1" method="GET" action="http://abc.appspot.com/_ah/xmpp/message/chat/">
<input type="text" id="data" />
<input type="submit" value="Submit" />
</form>
However, when I click on submit button, the URL that is fired is "http://abc.appspot.com/_ah/xmpp/message/chat/?"
The value of the "data" is not appended. Am I doing something silly here?
Thanks

Only fields which have name attribute are submited. This makes sense because how would you access those values without some key in POST of GET. So change your code to
<input type="text" name="data" id="data" />

Add name to input text and check
<input type="text" id="data" name="data" />

Give any form elements that are to be submitted a name attribute or they'll be ignored. So:
<input type="text" id="data" name="data" />
(You don't need the id attribute unless you reference it from somewhere else, but it won't hurt.)
Also it probably makes more sense to have method="POST" on your form.

Related

Redirect to selected site with variable which i got

<form action="/subsite/" method="GET">
<input type="text" name="" placeholder="Your Nick">
<input class="button" type="submit" />
</form>
I want to redirect I mean it should looks like
www/subsite/text
What should I use ? POST ?
The method="get" means that the parameters (and values) will be sent in the query string (the stuff after the question mark in the URL). eg.
/subsite?input_field_name=input_field_value
In your case, the input field doesn't have a name, which will cause problems. You probably want something like this:
<input type="text" name="nickname" placeholder="Your Nick">
So if you submit this form:
<form action="/subsite" method="get">
<input type="text" name="nickname" placeholder="Your Nick">
</form>
Then after submitting, the browser will go to:
/subsite?nickname=value_of_nickname_variable
If you use a method="post", then the form data (variables) will be sent along in the request body, not the query string. There are other differences between get/post, but that's one of them :)
If you just want to do a simple redirect when clicking the button, you could use javascript instead, eg.: window.location.href='/my_url_path_here'

How can I make an html button that passes a parameter?

I want a html button with parameter functionality.
new/?sorting=desc is the url that it should link to. But when I try, the parameter is not passed. How should it be done? I tried both the methods below but none worked.
<FORM METHOD="GET" ACTION="./?sorting=desc">
<INPUT TYPE="submit" VALUE="Äldst först">
</FORM>
I want buttons that behave like these links:
Äldst först
Nyast först
If you want something to act as a link, then you should use a link.
That said:
When you submit a GET form, the query string is removed from the action and a new one is generated from the data in the form.
You need to store the data in hidden inputs.
<form action="/social/tracking/new/">
<input type="hidden"
name="sorting"
value="desc">
<input type="submit"
value="Nyast först">
</form>
<form action="/social/tracking/new/">
<input type="hidden"
name="sorting"
value="asc">
<input type="submit"
value="Sort the other way">
</form>
If you are using jQuery, you can use the code below.
This will fill a hidden input with the correct value when you click on one of the submit buttons.
<form method="get" action="./">
<input type="hidden" name="sorting" id="sorting" value="" />
<input type="submit" value="Äldst först" id="sort_desc" />
<input type="submit" value="Nyast först" id="sort_asc" />
</form>
<script>
$('#sort_desc').click(function(){
$('#sorting').val('desc');
});
$('#sort_asc').click(function(){
$('#sorting').val('asc');
});
</script>
I think its not possible. A form is used to send data (mostly) via POST or GET. Your goal is to open a specific URL. I would create a standard and would style it like a button. Whats the reason you want to use a button?

Why does this URL go to the wrong place when I submit the form?

<html>
<head><title></title></head>
<body>
<form action="https://oauth.vk.com/authorize?client_id=3343995&scope=photos,wall,offline&redirect_uri=http://example.com/vkontakte/auth&response_type=code" method="get">
<input type="submit" value="Auth">
</form>
</body>
</html>
Hello. I've got a problem with this HTML. When i'm clicking the submit button, i expect go to the https://oauth.vk.com/authorize?client_id=3343995&scope=photos,wall,offline&redirect_uri=http://example.com/vkontakte/auth&response_type=code link, but instead i go to https://oauth.vk.com/authorize There is no redirects.
When your form uses method="get", the query parameters will be erased from your action attribute and form elements will be URL encoded in the URL's query string.
If you can use a POST, you can just change your form to use method="POST" and it will work. You should still correctly encode your URL names and values http://jsfiddle.net/9FmtW/3/
If you must use a GET, you have to include your query parameters as hidden fields in your form http://jsfiddle.net/9FmtW/ This has the benefit of correctly URL encoding your query string parameters
<form action="https://oauth.vk.com/authorize" method="get">
<input type="hidden" name="client_id" value="3343995" />
<input type="hidden" name="scope" value="photos,wall,offline" />
<input type="hidden" name="redirect_uri" value="http://example.com/vkontakte/auth" />
<input type="hidden" name="response_type" value="code" />
<input type="submit" value="Auth" />
</form>
Your URL contains an URL in it, you need to escape special URL specific characters like & and =

HTML Form POST re-writing action attribute

I have an HTML form that I'm trying to get to post with part of a query string already inplace, but it keeps re-writing the URL.
<form id="mls_form" action="/index.php?option=com_mls&view=mls" method="get">
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>
Output is:
http://www.mysite.com/index.php?mlsnum=value
It seems really simple, but I don't know why it's re-writing the action attribute.
Use the POST method rather than the GET method. The URL parameters will be sent as specified in the action attribute, and the form inputs will be sent in the post data. Your server script can then read them each using whatever API is appropriate (in PHP, $_GET versus $_POST, or find them all in $_REQUEST).
If you must use GET you can give the additional parameters as hidden input fields.
<form id="mls_form" action="/index.php" method="GET">
<input type="hidden" name="option" value="com_mls" />
<input type="hidden" name="view" value="mls" />
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>

POST extra values in an HTML <form>

I have a simple form which passes the value of an <input /> element:
<form action="updaterow.php" method="POST">
<input type="text" name="price" />
</form>
How can I post extra values along with the <input /> value? For example, an arbitrary string, or a variable inside the current PHP script.
I know this is possible with GET:
<form action="updaterow.php?foo=bar" method="GET">
<input type="text" name="price" />
</form>
or:
<form action="updaterow.php?foo=<?=htmlspecialchars($bar)?>" method="GET">
<input type="text" name="price" />
</form>
but I need POST.
You can include a hidden form element.
<input type="hidden" name="foo" value="bar" />
You can simply use a hidden field. Like so:
<input type="hidden" name="your-field-name" value="your-field-value" />
This field will then be available in your PHP script as $_POST['your-field-name']
As mentioned already, using hidden input fields is an option, but you can also use a session. That way you don´t have to post anything, the variables remain on the server and are not exposed in the html.