I am using this package to get rates from users, which works for products/posts completely fine for me but i have an issue when i don't have exist record in my database!
What I mean?
Normally when you want to rate you will rate to exist record in database such as exist post or exist product and you express yourself about that record by giving start/rates. now i want to use this system for something that doesn't exist such as testimonial means: people write their testimonial and give star then save it, here i get error of
constraint violation: 1048 Column 'rateable_id' cannot be null
and that's simply because this package tries to get id of testimonial while it doesn't exist, so i need a solution to save both together.
my form
<form class="poststars" action="{{route('testimonialsStar')}}" id="addStar" method="POST">
{{ csrf_field() }}
{{Form::label('comment', 'Comment', array('hidden'))}}
{{Form::textarea('comment', null, array('class' => 'form-control'))}}
<span style="padding-top: 20px !important;vertical-align: -moz-middle-with-baseline;padding-left: 4%;color: #fff;">give rate</span>
<input class="star star-5" value="5" id="star-5" type="radio" name="star"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" value="4" id="star-4" type="radio" name="star"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" value="3" id="star-3" type="radio" name="star"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" value="2" id="star-2" type="radio" name="star"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" value="1" id="star-1" type="radio" name="star"/>
<label class="star star-1" for="star-1"></label>
<button type="submit" class="btn btn-success" name="submit">Send</button>
</form>
My routes
//Testimonial Rating System
Route::post('/ratingtes', 'IndexController#testimonialsStar')->name('testimonialsStar');
Route::get('/writetestimonials', 'IndexController#testimonialsds')->name('testimonialsindex');
My Methods
public function testimonialsStar (Request $request, Testimonial $testimonial) {
$rating = new Rating;
$rating->user_id = \Auth::id();
$rating->rating = $request->input('star');
$rating->comment = $request->input('comment');
$rating->rateable_id = str_random(40);
$testimonial->ratings()->save($rating);
return redirect()->back();
}
public function testimonialsds () {
return view('frontend.testimonials');
}
Thanks.
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.
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');
This is a 5-star rating form and I am trying to make it work.
<div class="stars">
<form>
<input class="star star-5" id="star-5" type="radio" name="star" value="5"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="star" value="4"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="star" value="3"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="star" value="2"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="star" value="1"/>
<label class="star star-1" for="star-1"></label>
<button class="button" type="submit" name="submit" value="submit">Submit</button>
</form>
</div>
When I press the submit button nothing happens. The code does not move into the if statement, though it should since submit is not longer null.
<?php
if(isset($_POST['submit'])){
$rating = $_POST['star'];
echo "hello";
echo $rating;
}
?>
set form method=post
if html and php code in same page use
set form action="" otherwise use
set form action=".php file"
for ex:<form method="post" action=" or .php file">
You just missed to put form method and action in form.
Use this
<form method="post" action="" >
and on php code use
if(isset($_POST['star']))
it will work
To use super global variable $_POST, u need to specify method attribute to the form tag with submit button.
Change your form tag and your submit button tag to :
<div class="stars">
<form method="POST" name="starRatingForm" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input class="star star-5" id="star-5" type="radio" name="star" value="5"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="star" value="4"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="star" value="3"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="star" value="2"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="star" value="1"/>
<label class="star star-1" for="star-1"></label>
<input type="submit" name="submit" value="submit"/>
</form>
</div>
isset() checks if a variable has a value including ( False , 0 , or empty string) , but not NULL. Returns TRUE if variable exists otherwise returns FALSE.
On the other hand the empty() function checks if the variable has an empty value, empty string , 0, NULL ,or False. Returns FALSE if variable has a non-empty and non-zero value.
Example
if(!empty($_POST['star'])){
....
}
You have to set method and action attributes in the form tag.
<form method='post' action='page.php'>
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.
I creating form element by this code:
$input_new = $this->createElement('radio', 'Stars', array(
'label' => 'Stars',
'Options' => array('class'=>'star {split:2}'),
'multiOptions'=>array(
'1'=> '',
'2'=> '',
'3'=> '',
'4'=> '',
'5'=> '',
'6'=> ''
);
Here what i got:
<label for="Stars" class="optional">Stars</label>
<label for="Stars-1"><input type="radio" name="Stars" id="Stars-1" value="1" class="star {split:2} in_line"></label><br />
<label for="Stars-2"><input type="radio" name="Stars" id="Stars-2" value="2" class="star {split:2} in_line"></label><br />
<label for="Stars-3"><input type="radio" name="Stars" id="Stars-3" value="3" class="star {split:2} in_line"></label><br />
<label for="Stars-4"><input type="radio" name="Stars" id="Stars-4" value="4" class="star {split:2} in_line"></label><br />
<label for="Stars-5"><input type="radio" name="Stars" id="Stars-5" value="5" class="star {split:2} in_line"></label><br />
<label for="Stars-6"><input type="radio" name="Stars" id="Stars-6" value="6" class="star {split:2} in_line"></label>
I want to remove at each input and tag too, but main label(Stars) still stay. How i can do this?
Well, your radio buttons could go to a display group (a fieldset), so you could as well remove all labels for the buttons and just use the group label. If not, you'll probably need to write an external decorator view. I can't think of a cleaner way to get rid of the labels.
[EDIT]
Here's an example:
In the form, set the decorators like this:
$input_new->setDecorators(array(array('ViewScript', array('viewScript' => 'starsview.phtml'))));
Then create a starsview.phtml file. These links should prove useful in order to figure out how to create the view script's content:
Edit individual radio buttons in zend form view script
http://framework.zend.com/manual/en/zend.form.standardDecorators.html (scroll down to the section titled "Zend_Form_Decorator_ViewScript").
Hope this helps.