i want insert this form value to datanase :
<input type="checkbox" name="brand1" id="brand1" value="1"> <label for="brand1">Brand 1</label>
<input type="checkbox" name="brand2" id="brand2" value="1"> <label for="brand2">Brand 2</label>
<input type="checkbox" name="brand3" id="brand3" value="1"> <label for="brand3">Brand 3</label>
<input type="checkbox" name="brand4" id="brand4" value="1"> <label for="brand4">Brand 4</label>
<input type="checkbox" name="brand5" id="brand5" value="1"> <label for="brand5">Brand 5</label>
these text box are get by php from a table in database and may be Variable
i want insert to database by this format
if brand 1 are checked $brand="1,";
and Finally like this :
insert($name,$brands); and $brands = "1,2,3,4,5,";
if write this by if and while but it doesn't work because if insert run in while {} Five times insert Done and if insert run out of while {} , $brand = "5,"
thanks for your help or idea for this problem
it's mean :
<form method="post" action="#">
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
?>
<input type="checkbox" name="brand<?php print"$brand_id"; ?>" value="1" style="cursor:pointer;"><label for="brand<?php print"$brand_id"; ?>" style="cursor:pointer;"> <?php print"$brand_name"; ?></label>
<?php }} ?>
Source Output:
<input type="checkbox" name="brand1" value="1"> <label for="brand1">Brand Name 1</label>
<input type="checkbox" name="brand2" value="1"> <label for="brand2">Brand Name 2</label>
<input type="checkbox" name="brand3" value="1"> <label for="brand3">Brand Name 3</label>
<input type="checkbox" name="brand4" value="1"> <label for="brand4">Brand Name 4</label>
<input type="checkbox" name="brand5" value="1"> <label for="brand5">Brand Name 5</label>
<input type="submit" value="Submit" />
</form>
when submit form , insert source is :
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = brand.stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
$brand_ids = "brand.$brand_id";
if($$brand_ids==1) {$brands="$brandid,"}
}} ?>
$db->add_submenu("$brands");
You should change the name of your checkboxes to brand[]. It will give you an array once submitted at $_POST['brand']
Ex.
<input type="checkbox" name="brand[]" value="1" ... />
<input type="checkbox" name="brand[]" value="2" ... />
<input type="checkbox" name="brand[]" value="3" ... />
<input type="checkbox" name="brand[]" value="4" ... />
<input type="checkbox" name="brand[]" value="5" ... />
on the other side you can either do something like the following:
// this will return '1, 2, 3, 4, 5' when all are selected.
$index = implode(", ", $_POST['brand']);
and at that point you will have the brands in comma delimited form.
Related
I created a page for multiple rows submit data to mysql with php!
But, I need filter check the checkbox[] has been checked for submit current row data
In my demo,
If I checked the row2 and row3, I expected I will get id=2 & id=3
finally I get the id=1 & id=2
In the same situation, if I checked row3 only, I will get the id=1
I probably understand the principle, but I really can’t find a solution
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
if (#$_POST['checked'][$key] == "on") {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
}
print_r($row);
?>
<form action="" method="POST">
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">submit</button>
</form>
I try #CBroe
if checked row3, I still get a
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
$row[$key]['checkbox'] = $_POST['checkbox'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="checked[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">Submit</button>
</form>
#CBroe Thanks for your
<?php
$row = "";
if ($_POST) {
foreach ($_POST["id"] as $key => $v) {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="id[1]" value="1">
<input type="text" name="other_value[1]" value="a">
</p>
<p>
<input type="checkbox" name="id[2]" value="2">
<input type="text" name="other_value[2]" value="b">
</p>
<p>
<input type="checkbox" name="id[3]" value="3">
<input type="text" name="other_value[3]" value="c">
</p>
<button type="submit">submit</button>
</form>
I made radio buttons and all radio buttons are selected, where I try my code.
How to choose just one radio button in sintaks laravel?
This is My View Page :
<div class="form-group">
<b>Paket</b>
<br/>
<fieldset>
<input type="checkbox" name="delux" id="delux" value="d"> <label for="">Paket Delux </label>
<input type="checkbox" name="paket1" id="p1" value="p1"> <label for="">Paket 1</label>
<input type="checkbox" name="paket2" id="p2" value="p2"> <label for="">Paket 2</label>
</fieldset>
</div>
<div class="form-group">
<b>Jenis Pembayaran</b>
<br/>
<fieldset>
<form id="form_radio" name="form_radio">
<input type="radio" value="tunai" name="tunai" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="nontunai" id="rd2"> <label for="">non tunai</label>
</fieldset>
</div>
<input type="submit" value="Upload" class="btn btn-primary">
</form>
And this is My controller :
public function input()
{
$jenis = JenisMkn::select('id_jenis','jenis_makanan')->get();
return view('upload_gambar',['jenis'=>$jenis]);
}
public function proses(Request $request)
{
$cek = Gambar::get('checkbox');
echo $cek;
$radio = Gambar::get('radio');
echo $radio;
What the fault in my code?
Any help? Thank you.
Simply give them the same name, cek this code:
<input type="radio" value="tunai" name="transaksi" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="transaksi" id="rd2"> <label for="">non tunai</label>
You can rename the name input with your name.
If you want only one radio button to get selected then you must specify same value for name property. for example:
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
Hope it helped.
Help please understand how to save data of this kind:
there is a site with a quiz, and the possibility of creating an unlimited number of questions
part of view create.blade.php:
<div class="form-group {{ $errors->has('question_title') ? 'has-error' : ''}}">
<label for="question_title1" class="col-md-4 control-label">{{ 'question №1' }}</label>
<div class="col-md-4">
<!--
<input type="hidden" name="question_number[]" value="1">
-->
<input class="form-control" name="question_title1" type="text" id="question_title"
value="" required>
{!! $errors->first('question_title1', '<p class="help-block">:message</p>') !!}
</div> <br> <br>
A. <input class="" name="question_description1[]" type="text" id="question_description1" value="a1">
<input type="checkbox" name="question_answer1[]" value="1" checked> <br>
B. <input class="" name="question_description1[]" type="text" id="question_description1" value="a2">
<input type="checkbox" name="question_answer1[]" value="2"> <br>
C. <input class="" name="question_description1[]" type="text" id="question_description1" value="a3">
<input type="checkbox" name="question_answer1[]" value="3"> <br>
D. <input class="" name="question_description1[]" type="text" id="question_description1" value="a4">
<input type="checkbox" name="question_answer1[]" value="4"> <br>
</div>
part of controller QuizController.php:
public function store(StoreQuizRequest $request)
{
$data_quiz = $request->only(
'title', 'description', 'category', 'published', 'access', 'start_date', 'end_date',
'duration','show_answers');
$data_quiz['user_id'] = Auth::user()->id;
$quiz = Quiz::create($data_quiz);
$question_1['question_title']=$request->question_title1;
$question_1['question_description']=implode('|&-&|',$request->question_description1);
$question_1['question_answer']=implode('|',$request->question_answer1);
$question_1['quiz_id'] = $quiz->id;
$question_2['question_title']=$request->question_title2;
$question_2['question_description']=implode('|&-&|',$request->question_description2);
$question_2['question_answer']=implode('|',$request->question_answer2);
$question_2['quiz_id'] = $quiz->id;
$question_save1 = Question::create($question_1);
$question_save2 = Question::create($question_2);
return redirect('quiz')->with('flash_message', 'Quiz created!');
}
How to create a loop in controller to save all questions with answers to DB? Thanks for answers!
Let check it out: How do I create arrays in a HTML ?
You could create a form like this, and duplicate a form group if more questions are added:
<div class="form-group">
....
<input name="question_title[]" />
A. <input name="question_descriptionA[]" type="text" id="question_descriptionA" value="a1" />
<input type="checkbox" name="question_answerA[]" value="1" checked /> <br />
B. <input name="question_descriptionB[]" type="text" id="question_descriptionB" value="a2" />
<input type="checkbox" name="question_answerB[]" value="2" /> <br />
C. <input name="question_descriptionC[]" type="text" id="question_descriptionC" value="a3" />
<input type="checkbox" name="question_answerC[]" value="3" /> <br />
D. <input name="question_descriptionD[]" type="text" id="question_descriptionD" value="a4" />
<input type="checkbox" name="question_answerD[]" value="4" /> <br />
....
</div>
And you can access the request data as array, as follows:
$data = $request->only('question_title',
'question_descriptionA', 'question_answerA',
'question_descriptionB', 'question_answerB',
'question_descriptionC', 'question_answerC',
'question_descriptionD', 'question_answerD');
I would like to create a html quiz with a dynamic number of questions and each question has a dynamic number of answers. My goal is to submit all of the questions when the quiz is completed. What changes to my HMTL do I need to make and what php do I use to get the array?
HTML
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions[]" type="hidden" value="1">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions[]" type="hidden" value="2">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
PHP
$questionID = $_POST['questions'];
$answers = $_POST['answers'];
foreach( $questionID as $i => $qid ) {
echo "The question id is ".$qid." and selected answer(s) is ".$answers[$i]. "<br>";
}
I believe one of my html problems has to do with the answers are named the same for both questions. Another php problem is which answer(s) were selected. I do not want to use AJAX as that causes more load on the server and I use JS for a quiz timer. Thanks for your help.
I think this will do what you want:
$questionAnswers = array('question1'=>array('answer1','answer2','answer3','answer4'));
$questionText = array('question1'=>'Question 1');
Displaying the form:
<form>
<?php foreach($questionAnswers as $q => $answers)
{
echo '<div class="questions"><strong class="white">'.$questionText[$q].'</strong>'
for($i = 0; $i < count($answers); $i++)
{
echo '<input name="'.$q.'[]" type="checkbox" value="'.$i.'"> <label>'.$answers[$i].'</label><br>';
}
echo '</div>';
}
?>
</form>
Processing submission:
$foreach($questionAnswers as $q => $answers)
{
$userAnswers = isset($_REQUEST[$q]) ? $_REQUEST[$q] : array();
...
}
You should specify the number of the question in the input name attribute, e.g.:
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions" type="hidden" value="1">
<input name="answers[1]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[1]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[1]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[1]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions" type="hidden" value="2">
<input name="answers[2]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[2]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[2]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[2]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[2]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[2]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
Then when you get a $_POST it will be like:
answers[1] = 'a',
answers[2] = 'b' ....
I am writing a php site that has a form with a series of check boxes. I will be loading an array from a file that I would like to go through and check some of the boxes by default when the form is loaded.
Here is an example:
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" />
</form>
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
Is this possible? What would be the best way to do it.
You should fill your array before the HTML part. And then:
<input type="checkbox" name="option1" value="option1" <?php if (in_array("option1", $array)) { echo 'checked="checked"'; } />
Try this :
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" <?php if(in_array("option1",$array)){?> checked="checked"<?php}?> />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" <?php if(in_array("option2",$array)){?> checked="checked"<?php}?> />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" <?php if(in_array("option3",$array)){?> checked="checked"<?php}?> />
</form>