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');
Related
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.
I am creating a meeting room booking application using php. In that, when a person book a room there have an option to book tea or snacks and specify the number of items they need. For that, I use checkboxes to select items and input type number to specify the number of items.i.e, if tea selected you have to specify the no. of tea in the input field. My problem is that I can't store or display the checkbox value and associated number together.my code is
<form class="form-horizontal" method="post" id="bookroom" action="bookedreview.php">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[]">
</div>
<input type="submit" name="submit" value="value">
</form>
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
//$food=implode(',',$_POST['food']);
foreach($foodnum as $index =>$value){
$fud=$value;
$num=$foodnum['index'];
}
}
but when I display the variable I couldn't get the result. Can anyone help me how could store the checked items and the associated quantity together.
You can get with passing food name in textbox name array
<form class="form-horizontal" method="post" id="bookroom" action="">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[tea]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[coffee]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[snacks]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[water]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[nuts]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[breakfast]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[lunch]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[dinner]">
</div>
<input type="submit" name="submit" value="value">
</form>
<?php
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
echo "<table border='1' style='width:100%'><tr><th>Food Name</th><th>Count</th></tr>";
// output data of each row
foreach($food as $foo)
{
echo "<tr><td>".$foo."</td><td>".$foodnum[$foo]."</td></tr>";
}
echo "</table>";
}
?>
For Insert
foreach($food as $foo)
{
$fieldVal1=$foo;
$fieldVal1=$foodnum[$foo];
$query ="INSERT INTO foodcounts( foodsname, cnt) VALUES ('".$fieldVal1."','".$fieldVal2."' )";
mysqli_query($conn, $query);
}
I've been asking a lot of questions as of late related to creating RESTful services with PHP. My question is this:
Can all services (GET, POST, PUT, DELETE) be done from a single web form using radio buttons?
This is what I am picturing in my head:
<form action="MyService.php" method="GET">
<label for="username">Username</label>
<input type="text" name="username" id="username" /><br />
<label for="password">Password</label>
<input type="password" name="password" id="password" /><br />
<label for="id">Task ID</label>
<input type="text" name="id" id="id"/><br />
<label for="desc">Task Description</label>
<input type="text" name="desc" id="desc"/><br />
<label>
<input type="radio" name="service" value="getRadio" checked/> GET
</label>
<label>
<input type="radio" name="service" value="postRadio" /> POST<br />
</label>
<input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
<input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
<input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
<input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
<input type="submit" name="submit" value="ACTION"/>
</form>
It's incomplete so far, but what I'm thinking of trying to do is have radio button selections for each service, then outline my php file like this:
$request = $_SERVER["REQUEST_METHOD"]
switch($request) {
case 'GET':
// logic for GET based on radio button selected
break;
case 'POST':
// logic for POST based on radio button selected
break;
// and then PUT and DELETE
}
Is this doable? If so, am I on the right track, or do I need to make changes?
if you want to use the forms, and providing it does work as seems by this spec, then you can use some javascript to change the method of your form onsubmit.
<form action="MyService.php" method="GET" onsubmit='this.method = this.service.value'>
<label for="username">Username</label>
<input type="text" name="username" id="username" /><br />
<label for="password">Password</label>
<input type="password" name="password" id="password" /><br />
<label for="id">Task ID</label>
<input type="text" name="id" id="id"/><br />
<label for="desc">Task Description</label>
<input type="text" name="desc" id="desc"/><br />
<label>
<input type="radio" name="service" value="GET" checked/> GET
</label>
<label>
<input type="radio" name="service" value="POST" /> POST<br />
</label>
<label>
<input type="radio" name="service" value="PUT" /> PUT
</label>
<label>
<input type="radio" name="service" value="DELETE" /> DELETE<br />
</label>
<input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
<input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
<input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
<input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
<input type="submit" name="submit" value="ACTION"/>
</form>
First my code:
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post">
<P>
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</P>
</FORM>
';
?>
So if i run that on my xampp-Server, it shows a "Hello" and the Form in a new line.
What must I do that all this is written in one line?
Thanks
You need to remove the <p> element and display the form inline.
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post" style="display:inline">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
Remove the paragraph break '<P>'
You should really use a heredoc for this type of HTML output in PHP. Technically, the <p> tag should be around the Hello, not the form. You're looking for something like:
<?php
echo <<<EOT
Hello
<FORM ACTION="uebung3.php" METHOD="post">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
EOT;
?>
Even by removing <p> tags, your hello will still appear on a seperate line. This is because it is outside your <form> tag.
Put it inside <form> like this, while removing the <p> tags:
<?php
echo '<FORM ACTION="uebung3.php" METHOD="post">Hello
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
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.