Html:
<input type="checkbox" value="1" name="my_checkbox[]">
<input type="checkbox" value="2" name="my_checkbox[]">
<input type="checkbox" value="3" name="my_checkbox[]">
In action:
$arr= $request->getParameter('my_checkbox[]');
This does not work.
Any solution?
How about this:
$arr = $request->getParameter('my_checkbox');
Related
If I click save I want to redirect to userslist view and when I click save and new I want to redirect again to add users view and script "save successfully" but it is too hard to solve. Does anybody have an idea to fix this issue?
This is my view in add form.
<form method="post" action="<?php echo site_url('users/add')?>" >
<label>FirstName</label>
<input type="text" name="first_name" placeholder="Firstname"required>
<label>LastName</label>
<input type="text" name="last_name" placeholder="Lastname"required>
<label>Email</label>
<input type="email" name="email" placeholder="Email"required>
<label>Username</label>
<input type="Username" name="username" placeholder="Username"required>
<label>Password</label>
<input type="password" name="password" id="password" placeholder="Password"required>
<label>Confirm Password</label>
<input type="password" placeholder="Confirm Password" id="confirm_password" required>
<label>Permissions</label>
<input type="text" name="permissions" placeholder="Permissions"required>
<label>Role ID</label>
<input type="text"required name="role_id" placeholder="Role ID"required>
<label>Created By</label>
<input type="text"required name="created_by" placeholder="Created By"required>
<button type="submit" value="formSave">SAVE</button>
<button type="submit" value="formSaveNew">SAVE AND ADD NEW</button>
This is my controller to add
public function add()
{
$this->data['page_title'] = "Add User";
$input_data = $this->input->post();
if(!empty($input_data)){
$this->User_model->insert($input_data);
redirect('/users');
} else {
$this->load->view('templates/master', $this->data);
}
}
Let's say when you submit a form it sends a list of ids.
<form action="/process">
<input type="hidden" name="ids" value="4, 6, 10, 14, 20, 56" >
<input type="submit" value="Submit">
</form>
At the controller side
def process(EmailCommand cmd){
//now iterating over registrations after data binding
cmd.ids.each {
}
}
//Command Object
class EmailCommand {
List<Registration> ids
}
I want to bind all the ids passed to controller to the ids list in EmailCommand command object. How can i achieve it? I appreciate any help! Thanks!
It would be something like
<form action="/process">
<input type="hidden" name="ids[0].id" value="4" >
<input type="hidden" name="ids[1].id" value="6" >
<input type="hidden" name="ids[2].id" value="10" >
<input type="hidden" name="ids[3].id" value="14" >
<input type="hidden" name="ids[4].id" value="20" >
<input type="hidden" name="ids[5].id" value="56" >
<input type="submit" value="Submit">
</form>
Or if you want something more dynamic :
<form action="/process">
<g:each in="[4, 6, 10, 14, 20, 56]" var="id" status="i">
<input type="hidden" name="ids[${i}]" value="${id}" >
</g:each>
<input type="submit" value="Submit">
</form>
I could only get it to work after changing the command object to
class EmailCommand{
List<Registration> ids= ListUtils.lazyList([], { new Registration() } as Factory )
}
and view to the following as bassmartin suggested.
<g:hiddenField name="ids[0].id" value="1"></g:hiddenField>
<g:hiddenField name="ids[1].id" value="2"></g:hiddenField>
<g:hiddenField name="ids[2].id" value="3"></g:hiddenField>
<g:hiddenField name="ids[3].id" value="4"></g:hiddenField>
<g:hiddenField name="ids[4].id" value="5"></g:hiddenField>
<g:submitButton name="submit" value="submit"></g:submitButton>
I am wondering why empty list in command object doesn't work. Is this limitation of grails version 2.2?
You have 2 options here:
Straight forward -> trick with the split comma-separated String in the "setter":
class EmailCommand {
List<Registration> ids
void setIds( String val ){ ids = Registration.getAll( val.split( ', ' ) ) }
}
Correct -> use form params for that:
<form action="/process">
<g:each in="[4, 6, 10, 14, 20, 56]" var="id">
<input type="hidden" name="ids" value="${id}" >
</g:each>
<input type="submit" value="Submit">
</form>
and let grails do the binding.
I have multiple check box with the same input name like this
<input type="checkbox" name="pop[]" value="pop1">pop1<br>
<input type="checkbox" name="pop[]" value="pop2">pop2<br>
<input type="checkbox" name="pop[]" value="pop3">pop3<br>
<input type="checkbox" name="pop[]" value="pop4">pop4 <br>
<input type="checkbox" name="pop[]" value="pop5">pop5<br>
<input type="checkbox" name="pop[]" value="pop6">pop6 <br>
<input type="checkbox" name="pop[]" value="pop7">pop7<br>
<input type="checkbox" name="pop[]" value="pop8">pop8 <br>
The database table contains two columns (ID and POPNAME).
I need every pop checked to inserted into database in separate row but with the same ID so this is my PHP:
$pop = implode(',', $_POST['pop']);
mysql_query("INSERT INTO pops (id, popname) VALUES (LAST_INSERT_ID(), '$pop')");
But this not working. I tried to stick to this answer make explode then use for each but also not working. This is the foreach code:
$pop = implode(',', $_POST['pop']);
$pops = explode(',', $pop);
foreach ($pops as $pop )
{
mysql_query("INSERT INTO pops (id, popname) VALUES (LAST_INSERT_ID(), '$pop')");
}
Am I missing something!
It's solved.
Problem is $pop defined twice one as a var another one in foreach
So after rename the first var every thing ok
$popimp = implode(',', $_POST['pop']);
$pops = explode(',', $pop);
foreach ($pops as $pop )
{
mysql_query("INSERT INTO pops (id, popname) VALUES (LAST_INSERT_ID(), '$pop')");
}
PDO php:
Database connection code:
<?php
$dbhost = 'localhost';
$dbname = 'yourDB';
$dbuser = 'root';
$dbpass = '';
try {
$db = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection error: ".$e->getMessage();
}
?>
and php code:
$pop= implode(', ', $_POST['pop']);
$statement = $db->prepare("INSERT INTO popTable (pop) VALUES (?)");
$statement->execute(array($pop));
$success_message = "Order's has been inserted successfully.";
html:
<form action="pop_form">
<input type="checkbox" name="pop[]" value="pop1">pop1<br>
<input type="checkbox" name="pop[]" value="pop2">pop2<br>
<input type="checkbox" name="pop[]" value="pop3">pop3<br>
<input type="checkbox" name="pop[]" value="pop4">pop4 <br>
<input type="checkbox" name="pop[]" value="pop5">pop5<br>
<input type="checkbox" name="pop[]" value="pop6">pop6 <br>
<input type="checkbox" name="pop[]" value="pop7">pop7<br>
<input type="checkbox" name="pop[]" value="pop8">pop8 <br>
<input type="submit" value="Submit"/>
</form>
my form looks like this :
<form class="form-validate" id="booking-form" name="form-booking" method="post" action="/component/booking/?view=booknig">
<div class="width-60 fltlft">
<fieldset class="form-booking">
<legend></legend>
<ul>
<li><input type="text" readonly="readonly" class="readonly" value="0" id="jform_id" name="jform[id]"></li>
<li><input type="hidden" value="2013-06-17 10:15" name="jform[date_from]"></li>
<li><input type="hidden" value="1" name="jform[service_id]"></li>
<li><label class="" for="jform_phone" id="jform_phone-lbl">Telefon</label><input type="text" class="inputbox" value="" id="jform_phone" name="jform[phone]"></li>
<li><label class="" for="jform_email" id="jform_email-lbl">Email</label><input type="text" class="inputbox" value="" id="jform_email" name="jform[email]"></li>
<li><input type="hidden" value="" id="jform_checked_out" name="jform[checked_out]"></li>
<li><input type="hidden" value="" id="jform_checked_out_time" name="jform[checked_out_time]"></li>
</ul>
</fieldset>
</div>
<input type="hidden" value="booking.save" name="task">
<input type="submit" class="button-submit" value="Send" name="Send">
<input type="hidden" value="1" name="e391ee49d5291fa9d566236f5c0a435e"> <div class="clr"></div>
</form>
and to retrieve POST data i'm using :
$jinput = JFactory::getApplication()->input;
$form_values = $jinput->post->get('jform');
but when i'm tring to print_r $form_values i get only Array
where i'm making mistake
I need to get in an action method the values from the selected checkboxes on a form
how would you do that ? (or probably get them all and see who was selected)
public ActionResult (int[] ids)
...
<input type="checkbox" value = "1" />
<input type="checkbox" value = "2" />
<input type="checkbox" value = "3" />
<input type="checkbox" value = "4" />
<%=Html.Submit(); %>
You could try giving the checkboxes a name (ids):
<input type="checkbox" name="ids" value="1" />
<input type="checkbox" name="ids" value="2" />
<input type="checkbox" name="ids" value="3" />
<input type="checkbox" name="ids" value="4" />
Should work with:
public ActionResult Index(int[] ids) { ... }