I have a simple HTML input element:
<input type="text" class="form-control" name="value">
This field could have comma-separated values e.g. ABC, DEF, GHI. The field value when submitted must be exactly the same as when entered. However, when I am printing the field value to the console, I am getting
ABC%2C+DEF%2C+GHI.
I want ABC, DEF, GHI
I tried things like decodeURIComponent and accept-charset="ISO-8859-1" for the form, but they don't work. How can I prevent the encoding of the commas and spaces? Thanks in advance!
Before submiting, encode the value and it should work, according to my test
<form id="myForm" action="form.php" method="GET">
<input id="encodeMe" name="string" value="this will be encoded correctly" />
<input type="submit" value="OK" />
</form>
$('#myForm').submit(function() {
var enc = escape($("#encodeMe").val());
$("#encodeMe").val(enc);
});
Ok, I got it. In JavaScript, the input field has to be handled thus:
decodeURIComponent(str.replace(/\+/g,' '))
where str = ABC%2C+DEF%2C+GHI. Only decodeURIComponent is not enough. Hope it helps!
Related
I have a big form with lots fields, now I want to use JavaScript to simplify the fields, that means, pre process and delete some of the fields with JavaScript then return a new processed field.
For example, I have fields in the form like plot_country, plot_state, plot_city, what I want to do is to precalculate the location_# from the country, state, city in JavaScript, and return the GET fields just contain location_#.
In this way the url will be simplified, and the server can directly use the location_# and it doesn't need to process the location combinations any more.
Does anyone have some ideas? Thanks!
Try something like this:
<form id="location_form">
<input type="text" id="plot_country">
<input type="text" id="plot_state">
<input type="text" id="plot_city">
<input type="hidden" id="location">
<input type="submit">
</form>
<script type="text/javascript">
$( "#location_form" ).submit(function( event ) {
$('#location').value($(this).serialize());
$('input[type=text]').remove();
});
</script>
Say I have
<input type="hidden" name="Content" id="Content_m">
Can I send a specific text to the server ? If so, is it done by adding a value field to form field?
Yes, you can:
<input type="hidden" name="Content" id="Content_m" value="your_text">
yes. to send info use value field like so:
<input type="hidden" value="iwanttosendthis" name="Content" id="Content_m">
Hidden inputs are submitted with the form as normal inputs.
You can retrieve whatever value the hidden input contained from (for example) $_POST in PHP:
$myVar = $_POST["Content"]
In my HTML code I have the following input field, which is part of a form:
<input type="text" name="group[{$sConfigurator.groupID}]" value="{$optionTopOptionID}" id="{$formSelectID}" onChange="this.form.submit();"/>
The value is changed by a javascript function. The problem is, that the submission of the form isn't triggered. How can I do this?
it seems everything is right. try it with a submit input type and lower case of onchange:
<form action="example.php">
<input type="text" name="group[{$sConfigurator.groupID}]" value="{$optionTopOptionID}" id="{$formSelectID}" onchange="this.form.submit();"/>
<input type="submit" name="blabla" value="push!" />
</form>
is it works?
how to get the button value from jsp to servlet
in jsp:
<input type=button name=bt value=gi onclick="document.frm.submit();"></input>
and in servlet like that:
String gi =request.getParameter("bt");
System.out.print("button value" +gi);
result=null
thanks
Rather use <input type="submit">.
<input type="submit" name="bt" value="gi">
Its name/value pair will be sent to the server side as well:
String bt = request.getParameter("bt"); // gi
No need for JavaScript hacks/workarounds here. It would also break your application in case that the client has JavaScript disabled.
Take a hidden variable inside form and use it like this.
<form name="frm" method="post" action="">
<input type="hidden" name="hdnbt" />
<input type="button" name="bt" value="gi" onclick="{document.frm.hdnbt.value=this.value;document.frm.submit();}" />
</form>
Now, in the servlet,
String gi =request.getParameter("hdnbt");
System.out.print("button value" +gi);
You need to convert the button parameter to String using .toString(). There is nothing wrong with your code.
If I have three text input and I want to combine the values in these three text input into one POST name, how can I do that?
UPDATE:
a good example would be if I have a phone number field, and I have three fields for the phone number... I wanted this to be posted as one so back in the server side I can just access it as $POST['phone']
Would be nice if something like jQuery can help me out here.
Have them as an array:
<input type="text" name="inputs[]" />
<input type="text" name="inputs[]" />
<input type="text" name="inputs[]" />
Then you can access them in the POST array.
You have not indicated the programming language, but in PHP it would be, $_POST['inputs'][0], $_POST['inputs'][1], $_POST['inputs'][2]...
Since you want to have only one phone input which contains the full phone number appear on server side, not parts of it, and you are using jQuery in your project, this will make things easy for you:
1. Sample Markup
<form id="my_form" method="post">
<input type="text" name="phones[]" />
<input type="text" name="phones[]" />
<input type="text" name="phones[]" />
<input type="hidden" name="phone" />
<input type="submit" name="send" value="Send It" />
</form>
2. jQuery
$(document).ready(function(){
var $phones = $('#my_form input[name="phones[]"]'),
$phone = $('#my_form input[name="phone"]');
$('#my_form').submit(function(){
// join all the phone parts together
var phone_number = '';
$phones.each(function(){
phone_number += this.value;
});
// change the hidden input element's value
$phone.val(phone_number);
// remove the phone parts input elements
$phones.remove();
});
});
I wonder why you would need to do that.
In php, you can do as Shef said.
In simple single word cases, you can concatenate them with some separator (e.g. # or $) and process the same on the server side.