I have two check boxes with table names when any check box is checked i wants to save that table name in to a java string which i can use in the query to get data from that table or update that table.
I have used onClick functions and also got the check box value but not getting how to access it in the rest of the code so that i can use that value in the DB query.
I assume you have HTML code like the following:
<input type="checkbox" name="use_table1" />
<input type="checkbox" name="use_table2" />
On the server-side Java code, you can then query:
String tableName = null;
if (request.getParameter("use_table1") != null)
tableName = "tbl_1";
if (request.getParameter("use_table2") != null)
tableName = "tbl_2";
Note that the "outside" names differ from the real table names. No one out there on the web should need to know your real table names. And, most important, no one should be allowed to read an arbitrary table from your database. That's why I used that if-then-else code to select the table name.
Just give the table name as value. The browser will send only the name-value pairs of the checked ones as request parameters.
<input type="checkbox" name="tablename" value="table1">
<input type="checkbox" name="tablename" value="table2">
<input type="checkbox" name="tablename" value="table3">
<input type="checkbox" name="tablename" value="table4">
In the Servlet, you can grab them by HttpServletRequest#getParameterValues().
String[] checked = request.getParameterValues("tablename");
Simple as that :) No need for unnecessary JavaScript hacks/workarounds which ain't going to work in JS-disabled clients. This is one of the many unknown features of HTML.
Related
I have checkboxes inside a form. These checkboxes are a list of persons dynamically created since this list depends on who is logged in. Like, for a supervisor view, it will only list your subordinates.
I'm not sure if I'm missing something but the docs and other answers in SO are all saying that you have to have the id of the input in order to retrieve the form data but in my case, I used the person's ID in the list to come up with the name of the checkbox element. So my checkbox names are like input-{{person_id}}
Now in the post method, I'm not sure how to retrieve the checkboxes in the form. Tried using self.__dict__ but I can't seem to find anything that I can use
link1 - this specifies the name
link2 - this also says that the ID should be pre-determined
link3 - docs also say that if the parameter is not included, an empty list would returned
Maybe there is a workaround where I just get all the elements in the form and make them seen in tornado? Using javascript maybe?
I'm mistaken, it seems that if you use the same name for a checkbox element for all the dynamic checkboxes you create, then when you submit it to the tornado web handler, it will get the values of all those checkboxes that have that name.
the values are retrieved by using the get_arguments
Is this how you mean?
HTML
<form>
<input type="checkbox" id="box1" name="Name1" checked>
<label for="box1">Name 1</label>
<input type="checkbox" id="box2" name="Name2">
<label for="box2">Name 2</label>
</form>
jQuery
$("form input[type='checkbox']").each(function() {
console.log($(this).attr('name'))
})
Output
"Name1"
"Name2"
You can loop through the request.body_arguments attribute; it's dict which maps arguments names to lists of values (to support multiple values for individual names).
class MyHandler(tornado.web.RequestHandler):
def post(self):
for key, value in self.request.body_arguments.items():
print(f"key {key} has value: {value}")
...
There are also arguments (holds both query and body args) and query_arguments (holds only query args) attributes; you can check the docs here.
I have a "settings" page in my site and I would like the user to be able to either check or uncheck checkboxes in the settings page. The checkboxes' states should then load when the user logs in.
My idea is to assign ids to each checkbox like this:
<input type="checkbox" id="chk1" data-col='column1' value="0"/>
<input type="checkbox" id="chk2" data-col='column2' value="0"/>
and then use jQuery to set the values for each on the checked function like this on the "SAVE SETTINGS" button:
$( "input:checkbox" ).each(function() {
if $(this).prop('checked'){
$(this).prop('value','1')
}
});
and then have a column in my MySql table for each checkbox and then send the value (either 1 or 0) to my database via ajax/php.
Then when the user logs in, I can get the values (1 or 0) for each column and then based on that, I can set the checked state.
Is this a good way to go about it? I want to make sure I don't go down the wrong path if there is a simpler solution or approach.
Assume that $query_array is the container of your db query. Since you've included php in the post's tag you can insert a php tag inside input(assuming the file is a php too):
<input type="checkbox" id="chk1" data-col='column1' value="<?php echo $query[0][col_name];?>"/>
^the 0 above might be an id, and col_name is the column name which contains 0/1
When that is compiled, it should be similar to this:
<input type="checkbox" id="chk1" data-col='column1' value="0"/>
Instead of using jquery why don't you just render the checkboxes with their value already set:
<input type="checkbox" id="chk1" data-col='column1' value="<?php echo $chk1?>" <?php echo $checked?>/>
Edit
If you wanna keep it HTML only you could store the states in a JSON (where the property name is the id of the chekbox and the value it's state) and loop through it as:
$.each(jsonChkbxValues, function(index, val) {
var elem = $('#' + index);
elem.prop('value', val);
if (val) {
elem.attr('checked', 'checked');
}
});
I'm using the onsubmit event on a form to verify it before it is sent. I'm having issues getting the value of checkboxes that allows multiple choice.
Html:
<input type="checkbox" name="question5[]" value="1" />
<input type="checkbox" name="question5[]" value="2" />
Javascript:
var form = document.forms['questionnaire'];
var q5 = form.elements["question5"].value;
When I try to get the value of this question I'm not able to retrieve it the same way I did for the other fields. I'm wondering what is the correct way to get the value of those checkboxes since I can't retrieve it like a radio or text input.
The name of the field is question5[] not question5 and since you have multiple of them, you will get a NodeList (which is like an Array) back, not a single Element.
I've got an assignment to pass data between 2 .htm pages, in a manner which the source gets copied to the destination.
sourcePage.htm contains a form. (it contains more controls this is just a sample)
<form id="myform" action="destPage.htm" method="get" >
<input type="text" name="user" />
<input type="submit" name="submit" value="Submit" />
</form>
and destPage.htm is blank.
Using JavaScript I am required to parse the data from the url, that part isn't the problem
, the problem is that I am also required that destPage would be an exact duplicate of sourcePage.
My question is, if there's a way to pass the form as an object or some way to pass the control types and their properties along side the data.
You specified in the answer of ek_ny, that you want to dynamically build the form, based on it's input.
You can do this, in fact, with the JavaScript DOM:
var i = document.createElement('input');
i.setAttribute('type', "text");
i.setAttribute('name', "user");
var f = document.createElement('form');
f.setAttribute('action', "destpage.html");
// etc.
f.appendChild(i);
document.getElementById('container').appendChild(f);
The form will be added as a child in the <div id="container"> container.
Now you can use hidden input elements, which give, for instance, the specifics of the form:
<form>
<input type="hidden" name="x_type" value="input-text" />
<input name="x" type="text" />
<input type="hidden" name="y_type" value="select:[...]" />
<select name="y">
...
</select>
</form>
As far as I know, you won't be able to do a post between two pages. At least when I've attempted that you get an error-- it really doesn't make sense to have a post from one static page to the other (right?). What you can do is serialize the data you want to pass, put it on the url string to the next page and then deserialize that data and populate the controls on the destination page. If the html between the two pages is identical, then it should be pretty straightforward, if not it will be a little tricker. If you used jQuery it would be pretty easy, because you could serialize an entire form. If you need to come up with a generic solution (and you should, because it will help you learn) that's one thing, if you need to just get it working for this assignment and there are only a couple of form fields, you'll just need to encode the values you want to pass and pass them on a URL string with a get request.
I need to write a java script. This is supposed to validate if the checkbox is selected in the page or not. The problem here is that the check box is inside a grid and is generated dynamically. The reason being the number of check box that need to be rendered is not know at design time. So the id is know only at the server side.
Here is a thought:
As indicated by Anonymous you can generate javascript, if you are in ASP.NET you have some help with the RegisterClientScriptBlock() method. MSDN on Injecting Client Side Script
Also you could write, or generate, a javascript function that takes in a checkbox as a parameter and add an onClick attribute to your checkbox definition that calls your function and passes itself as the parameter
function TrackMyCheckbox(ck)
{
//keep track of state
}
<input type="checkbox" onClick="TrackMyCheckbox(this);".... />
You have to generate your javascript too, or at least a javascript data structure (array) wich must contain the checkboxes you should control.
Alternatively you can create a containing element, and cycle with js on every child input element of type checkbox.
If it's your only checkbox you can do a getElementsByTagName() call to get all inputs and then iterate through the returned array looking for the appropriate type value (i.e. checkbox).
There is not much detail in the question. But assuming the the HTML grid is generated on the server side (not in javascript).
Then add classes to the checkboxes you want to ensure are checked. And loop through the DOM looking for all checkboxes with that class. In jQuery:
HTML:
<html>
...
<div id="grid">
<input type="checkbox" id="checkbox1" class="must-be-checked" />
<input type="checkbox" id="checkbox2" class="not-validated" />
<input type="checkbox" id="checkbox3" class="must-be-checked" />
...
<input type="checkbox" id="checkboxN" class="must-be-checked" />
</div>
...
</html>
Javascript:
<script type="text/javascript">
// This will show an alert if any checkboxes with the class 'must-be-checked'
// are not checked.
// Checkboxes with any other class (or no class) are ignored
if ($('#grid .must-be-checked:not(:checked)').length > 0) {
alert('some checkboxes not checked!');
}
</script>