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>
Related
I have a Likert Survey generator where a dynamic number of questions can be entered - then a user goes through and answers each question with 4 radio buttons (Strongly Disagree, Disagree, Agree, Strongly Agree).
I can output them easily enough - but can they be model bound on the post back or do I have to traverse the posted form elements (ie not use model binding)?
I've Googled a lot and can find no solution - is it posible?
Thanks.
If you want to bind a list in view,here is a demo about binding a list with input names:
Model:
public class Qusetion
{
public string Content { get; set; }
public string Answer { get; set; }
}
View:
<form method="post">
<div>
<label>question1</label>
<input name="list[0].Content" value="question1" hidden/>
<div>
<input type="radio" name="list[0].Answer" value="Strongly Disagree" />Strongly Disagree
<input type="radio" name="list[0].Answer" value="Disagree" />Disagree
<input type="radio" name="list[0].Answer" value="Agree" />Agree
<input type="radio" name="list[0].Answer" value="Strongly Agree" />Strongly Agree
</div>
</div>
<div>
<label>question2</label>
<input name="list[1].Content" value="question2" hidden/>
<div>
<input type="radio" name="list[1].Answer" value="Strongly Disagree" />Strongly Disagree
<input type="radio" name="list[1].Answer" value="Disagree" />Disagree
<input type="radio" name="list[1].Answer" value="Agree" />Agree
<input type="radio" name="list[1].Answer" value="Strongly Agree" />Strongly Agree
</div>
</div>
<div>
<label>question3</label>
<input name="list[2].Content" value="question3" hidden/>
<div>
<input type="radio" name="list[2].Answer" value="Strongly Disagree" />Strongly Disagree
<input type="radio" name="list[2].Answer" value="Disagree" />Disagree
<input type="radio" name="list[2].Answer" value="Agree" />Agree
<input type="radio" name="list[2].Answer" value="Strongly Agree" />Strongly Agree
</div>
</div>
<input type="submit" value="submit" />
</form>
Controller:
public IActionResult BindList(List<Qusetion> list)
{
return View();
}
result:
Update:
If you want to bind with loop.you can pass list to view.And the code will work is because .net core bind model with name attribute.And because you want to bind list,so the name will be like list[index].xxx.
Here is a demo with loop:
View:
#model IEnumerable<Qusetion>
<form method="post">
#{ var i = 0;}
#foreach (var question in Model)
{
<div>
<label>#question.Content</label>
<input name="list[#i].Content" value="#question.Content" hidden />
<div>
<input type="radio" name="list[#i].Answer" value="Strongly Disagree" />Strongly Disagree
<input type="radio" name="list[#i].Answer" value="Disagree" />Disagree
<input type="radio" name="list[#i].Answer" value="Agree" />Agree
<input type="radio" name="list[#i].Answer" value="Strongly Agree" />Strongly Agree
</div>
</div>
i++;
}
<input type="submit" value="submit" />
</form>
Controller:
public IActionResult BindList(List<Qusetion> list)
{
List<Qusetion> list1 = new List<Qusetion> { new Qusetion { Content = "question1" }, new Qusetion { Content = "question2" }, new Qusetion { Content = "question3" } };
return View(list1);
}
result:
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.
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');
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) { ... }