Reputation: 79
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;
}
?>
Upvotes: 1
Views: 476
Reputation: 4870
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.
if(!empty($_POST['star'])){
....
}
Upvotes: 0
Reputation: 4894
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
Upvotes: 1
Reputation: 3
You have to set method and action attributes in the form tag.
<form method='post' action='page.php'>
Upvotes: -1
Reputation: 508
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>
Upvotes: 0
Reputation: 7161
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">
Upvotes: 2