Multiple checkboxes in mvc 3 - asp.net-mvc-3

I have multiple checkboxes with the same name. Like this:
<input name="zones" value="#zoneItem.Id" type="checkbox" /><label>#zoneItem.Name</label></span>
<input name="zones" value="#zoneItem.Id" type="checkbox" /><label>#zoneItem.Name</label></span>
<input name="zones" value="#zoneItem.Id" type="checkbox" /><label>#zoneItem.Name</label></span>
but in get or post i can't get which ones are checked. How can i do it?

They should have different values. Right now you gave them all the same value. So once you give them different values:
<input name="zones" value="1" type="checkbox" />
<input name="zones" value="2" type="checkbox" />
<input name="zones" value="3" type="checkbox" />
in your controller action you will get the list of values of those that were checked:
public ActionResult Foo(string[] zones)
{
...
}

They can be retrieved by adding a parameter to your action method.
public ActionResult GetData(Guid[] zones)
{
}
Make sure the array type matches your Id type.

Related

Submit checkbox values to Controller

I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">

Alter multiple columns in the Database based on selected values- Hibernate

I am displaying the details of a particular table in my JSP page using a for-each loop in the following manner:
<c:forEach items="${List2}" var="alist">
<tr>
<td>
<input type="checkbox" name="check" value="${alist.check}">
</td>
<td>${alist.two}</td>
<td>${alist.three}</td>
<td>${alist.four}</td>
</tr>
</c:forEach>
<input type="radio" id="agree" value="Agree"/>Agree
<input type="radio" id="disagree" value="Disagree"/> Disagree
<input type="submit" value="Submit"/>
Now, there is no column named "check" in my DB. It is a transient field in my POJO. I do have a Agree/Disagree column in my DB. I want to check particular rows, click "Agree" or "Disagree" and on clicking Submit, the Agree/Disagree column for those records should be updated respectively. I'm using Spring MVC with Hibernate. Please tell me if any more info is required from my side.
You can do this with simple jQuery, Set class(say .chkbox here) to all checkboxes. Whenever any change event happened say,
if checked, the value of checkbox added to JS array.
If unchecked, the value will be removed from JS array.
$(document).ready(function() {
var selectedArray = [];
$('.chkbox').change(function() {
if($(this).is(":checked")) {
selectedArray.push($(this).val());
} else {
selectedArray.pop($(this).val());
}
//alert(selectedArray);
$("#textbox1").val(selectedArray);
});
});
Finally I used textbox to hold all the selected checkboxes value. You can make it into a hidden give a name attribute.
<input type="checkbox" class="chkbox" value="101"> This is 101<br />
<input type="checkbox" class="chkbox" value="102"> This is 102<br />
<input type="checkbox" class="chkbox" value="103"> This is 103<br />
<input type="checkbox" class="chkbox" value="110"> This is 110<br />
<input type="checkbox" class="chkbox" value="111"> This is 111<br />
<input type="checkbox" class="chkbox" value="112"> This is 112
<br />
<input type="hidden" id="textbox1" name="selectedArray" />
Sent it to a controller and split the values by comma (,).
Update :
In your code, you can update like this.
<c:forEach items="${List2}" var="alist">
<tr>
<td>
<input type="checkbox" class="chkbox" name="check" value="${alist.check}">
</td>
<td>${alist.two}</td>
<td>${alist.three}</td>
<td>${alist.four}</td>
</tr>
<input type="hidden" id="textbox1" name="selectedArray" />
</c:forEach>
That's all about required.
See the JS Demo how it works. Hope this helps.

passing array to view codeigniter

I have the problem of doing the validation of fields myself. Now there are 5-6 fields in the form. So I am checking each one in my controller, and if there is wrong i wish to load the view again and pass the error array to it.
I achieved the above functionality with this:
<html>
<head>
<title>My Form</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<?php
echo $fullname;
?>
<?
echo form_open('/membership/register');
?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<h5>Mobile</h5>
<input type="text" name="mobile" value="" size="15" />
<h5>Home</h5>
<input type="text" name="home" value="" size="15" />
<h5>Full Name</h5>
<input type="text" name="fullname" value="" size="100" />
<br><br>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
and in controller the code is:
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
$this->load->view('register', $errors);
}
Now the real problem I have is if the many fields are wrong. I want to have $errors array passed to view and accessed there for all the values it contains. so I don't have to specify $fullname or $mobile to get the value. How can this be done? as to show the user everything missing in one go
First of all I advise using codeigniter's built in form validation class
Here is how I usually process my validation in the controller:
if ($this->input->post())
{
// process the POST data and store accordingly
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean');
// the rest of your form fields validation can be set here
if ($this->form_validation->run() == FALSE)
{
// validation hasnt run or has errors, here just call your view
$this->load->view('same_view_file_with_the_form', $data);
}
else
{
// process the POST data upon correct validation
}
}
In my view file I call each error like this:
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<span class="error-red"><?php echo form_error("username"); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<span class="error-red"><?php echo form_error("password"); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<span class="error-red"><?php echo form_error("cpassword"); ?></span>
Do all your checks in the controller prior to binding errors into the view.
e.g.
$errors = array();
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
}
if ( do_something_to_validate(mobile) )
{
$errors['mobile'] = 'invalid mobile';
}
// after checking everything do this
$this->load->view('register', $errors);

Editing Magento Checkout Form

On clicking "place order" in Magento, I want to capture some of its values and feed them to the following form:
<form method="post" action="https://www.jambopay.com/JPExpress.aspx" target="_blank">
<input type="hidden" name="jp_item_type" value="cart"/>
<input type="hidden" name="jp_item_name" value="test shop"/>
<input type="hidden" name="order_id" value="455879"/>
<input type="hidden" name="jp_business" value="business#yourdomain.com"/>
<input type="hidden" name="jp_amount_1" value="51"/>
<input type="hidden" name="jp_amount_2" value="0"/>
<input type="hidden" name="jp_amount_5" value="0"/>
<input type="hidden" name="jp_payee" value="email#yourcustomer.com"/>
<input type="hidden" name="jp_shipping" value="company name"/>
<input type="hidden" name="jp_rurl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=0"/>
<input type="hidden" name="jp_furl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=1"/>
<input type="hidden" name="jp_curl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=2"/>
<input type="image" src="https://www.jambopay.com/jambohelp/jambo/rsc/paymentsbyJamboPay.jpg"/>
</form>
The values include:
Order Number,
Total Amount,
Customers Email Address,
How do i do this?
You can get value of order after save order is called you can get last order id from this methods
$orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
once you have order id then load order like this
$order = Mage::getModel('sales/order')->load($orderId);
and you can get all details of order

Validating Form using codeigniter

I have the following form:
<label>One</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<label>Two</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<label>Three</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<!-- there may be more inputs like above (users can create new inputs
as many as they want) // I have a jquery function to create new rows-->
<input type="submit" value="submit">
Now My question is how to validate the form using Codeigniter when I have input names like this- name="productid[]" instead of name="productid" in my form.
Usually I use to validate my form like this way but this time it won't work for the form above.
How to validate it?
You would use the literal field names, with brackets:
$this->form_validation->set_rules('product_id[]', 'Product', 'required');
$this->form_validation->set_rules('quantity[]', 'Quantity', 'required');
This will run the validation on every field with that name. If you have to validate only a specific index, once again - use the literal field name (and specify the index in your HTML):
// <input name="product_id[3]">
$this->form_validation->set_rules('product_id[3]', 'Product', 'required');
It's all in the documentation for Codeigniter's Form Validation class:
Using Arrays as Field Names
The Form Validation class supports the use of arrays as field
names. Consider this example:
<input type="text" name="options[]" value="" size="50"
/>
If you do use an array as a field name, you must use the EXACT
array name in the Helper Functions that
require the field name, and as your Validation Rule field name.
For example, to set a rule for the above field you would use:
$this->form_validation->set_rules('options[]', 'Options',
'required');
Or, to show an error for the above field you would use:
<?php echo form_error('options[]'); ?>
Or to re-populate the field you would use:
<input type="text" name="options[]" value="<?php echo
set_value('options[]'); ?>" size="50" />
You can use multidimensional arrays as field names as well. For
example:
<input type="text" name="options[size]" value="" size="50"
/>
Or even:
<input type="text" name="sports[nba][basketball]" value=""
size="50" />
As with our first example, you must use the exact array name in the
helper functions:
<?php echo form_error('sports[nba][basketball]'); ?>
If you are using checkboxes (or other fields) that have multiple
options, don't forget to leave an empty bracket after each option, so
that all selections will be added to the POST array:
<input type="checkbox" name="options[]" value="red" />
<input type="checkbox" name="options[]" value="blue" />
<input type="checkbox" name="options[]" value="green" />
Or if you use a multidimensional array:
<input type="checkbox" name="options[color][]" value="red"
/> <input type="checkbox" name="options[color][]"
value="blue" /> <input type="checkbox"
name="options[color][]" value="green" />
When you use a helper function you'll include the bracket as
well:
<?php echo form_error('options[color][]'); ?>
Did You try this ...? A few lines below in the guide ...

Resources