isset is not working on submit - php

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'>

Related

How to select another one of radio button?

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.

Multiple radio button rating in the same form

I tried to search yet i unable to find out what i am looking for.Please help me.I am working on a rating form which using radio button star for rating,I am using while loop of mysqli_fetch_assoc to pull the date from database.As shown below
<?php
$sql = "SELECT * FROM orderdetail,orderuser,food WHERE orderuser.CustomerID=orderdetail.CustomerID AND orderuser.OrderID=orderdetail.OrderID AND orderdetail.FoodID=food.FoodID AND orderuser.confirmstatus=1";
$result = $conn->query($sql) or die($conn->error);
while($colum = mysqli_fetch_assoc($result))
{
?>
<img src="../php/<?php echo $colum['Foodphoto'];?>" style="height:150px;width:150px">
<br>
<?php echo $colum['FoodName'];?>
<br>
<div class="stars">
<form action="">
<input class="star star-5" id="rate-5[]" type="radio" name="rate[]" />
<label class="star star-5" for="rate-5[]"></label>
<input class="star star-4" id="rate-4[]" type="radio" name="rate[]" />
<label class="star star-4" for="rate-4[]"></label>
<input class="star star-3" id="rate-3[]" type="radio" name="rate[]" />
<label class="star star-3" for="rate-3[]"></label>
<input class="star star-2" id="rate-2[]" type="radio" name="rate[]" />
<label class="star star-2" for="rate-2[]"></label>
<input class="star star-1" id="rate-1[]" type="radio" name="rate[]" />
<label class="star star-1" for="rate-1[]"></label>
</form>
</div>
<hr>
<?php
}
?>
but the rating for the second result is not working,i find out that is due to the problem of name but i tried to edit it by adding the array still not working. And how could i store them into mysql using php?those tutorial i found only using ajax or jquery i need the ratings submit once the user submitted the form.
The problem I think is that you cannot relate the form being submitted with the record so any update will not work as expected. One of the simplest ways to accomplis this would be a hidden field with the ID of the record - the ID can then be used in the update statement - ie: ( pseudo sql update )
update orderdetail set rating=$rate where ID=$id
So, add a hidden field per form like this perhaps
while($colum = mysqli_fetch_assoc($result)){
?>
<img src='../php/<?php echo $colum['Foodphoto'];?>' style='height:150px;width:150px'>
<br>
<?php echo $colum['FoodName'];?>
<br>
<div class='stars'>
<form action=''>
<input class='star star-5' type='radio' id='rate-5' name='rate' />
<label class='star star-5' for='rate-5'></label>
<input class='star star-4' type='radio' id='rate-4' name='rate' />
<label class='star star-4' for='rate-4'></label>
<input class='star star-3' type='radio' id='rate-3' name='rate' />
<label class='star star-3' for='rate-3'></label>
<input class='star star-2' type='radio' id='rate-2' name='rate' />
<label class='star star-2' for='rate-2'></label>
<input class='star star-1' type='radio' id='rate-1' name='rate' />
<label class='star star-1' for='rate-1'></label>
<!--
HIDDEN FIELD WITH ID
-->
<input type='hidden' name='id' value='<?php echo $colum['id'];?>' />
</form>
</div>
<hr>

How to show a radio button one by one on php or html

I'm new to php and html and I was wondering, how having a code like below:
<!DOCTYPE html>
<html>
<body>
<form>
<p>Question Number 1</p>
<input type="radio" name="question1" value="A">A<br>
<input type="radio" name="question1" value="B">B<br>
<input type="radio" name="question1" value="C">C<br>
<input type="radio" name="question1" value="D">D
<p>Question Number 2</p>
<input type="radio" name="question2" value="A">A<br>
<input type="radio" name="question2" value="B">B<br>
<input type="radio" name="question2" value="C">C<br>
<input type="radio" name="question2" value="D">D
<p>Question Number 3</p>
<input type="radio" name="question3" value="A">A<br>
<input type="radio" name="question3" value="B">B<br>
<input type="radio" name="question3" value="C">C<br>
<input type="radio" name="question3" value="D">D
<p>Question Number 4</p>
<input type="radio" name="question4" value="A">A<br>
<input type="radio" name="question4" value="B">B<br>
<input type="radio" name="question4" value="C">C<br>
<input type="radio" name="question4" value="D">D
</form>
</body>
</html>
I want to show a question one bye one with button next and with check if
a value is null. What should I use? PHP or Javascript.
$('input[type="radio"]').on('change', function(e) {
$('#'+e.target.name+'').hide();
var nextRadioGroupName = "question" + (parseInt(e.target.name.replace('question', '')) + 1);
$('#'+nextRadioGroupName+'').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<form>
<div id = "question1">
<p>Question Number 1</p>
<input type="radio" name="question1" value="A">A<br>
<input type="radio" name="question1" value="B">B<br>
<input type="radio" name="question1" value="C">C<br>
<input type="radio" name="question1" value="D">D
</div>
<div id = "question2" style="display:none">
<p>Question Number 2</p>
<input type="radio" name="question2" value="A">A<br>
<input type="radio" name="question2" value="B">B<br>
<input type="radio" name="question2" value="C">C<br>
<input type="radio" name="question2" value="D">D
</div>
<div id = "question3" style="display:none">
<p>Question Number 3</p>
<input type="radio" name="question3" value="A">A<br>
<input type="radio" name="question3" value="B">B<br>
<input type="radio" name="question3" value="C">C<br>
<input type="radio" name="question3" value="D">D
</div>
<div id = "question4" style="display:none">
<p>Question Number 4</p>
<input type="radio" name="question4" value="A">A<br>
<input type="radio" name="question4" value="B">B<br>
<input type="radio" name="question4" value="C">C<br>
<input type="radio" name="question4" value="D">D
</div>
</form>
Firstly add a div element for per question group to easily show/hide them
Define change event with Jquery and find the next question group as iteration like "question1", "question2" etc.
Then show the next question group

Laravel rating while creating testimonial

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.

radio button value cant send via mail

First radio button only taken
<div class="field_radio" name="preference" >
<input class="radio1" type="radio" name="preference" id="preference"
value="team" onclick="ShowHideDiv()" /><label for="radio1">
<span>Team</span></label>
<input class="radio2" type="radio" name="preference" id="preference"
value="individual" onclick="ShowHideDiv()" /> <label for="radio2">
<span>Individual</span></label>
</div>
Php section
if (empty($_POST["preference"])) {
$errorMSG .= "preference is required ";
} else {
$preference = $_POST["preference"];
}
you took same id on both input field
<div class="field_radio" name="preference">
<input class="radio1" type="radio" name="preference" id="preference_one" value="team" onclick="ShowHideDiv()" />
<input class="radio2" type="radio" name="preference" id="preference_two"
value="individual" onclick="ShowHideDiv()" />
</div>
<div class="field_radio">
<input type="radio" class="radio1" name="preference" id="preference1" value="team" onclick="ShowHideDiv()" />
<label for="radio1"><span>Team</span></label>
<input type="radio" class="radio2" name="preference" id="preference2" value="individual" onclick="ShowHideDiv()" />
<label for="radio2"><span>Individual</span></label>
</div>
Note: Ids of the radio buttons are same make it different will solve your problem!

Categories