The Counter wont go up. As you see should the Counter go up if the PHP Code receive the A Value.
html:
<body>
<div>
<div>
<h1>Login</h1>
<form method="post" action="quiz.php">
<input name="A" value="A" type="radio">
<label for="A">A</label>
<input name="A" value="B" type="radio">
<label for="A">B</label>
<input name="A" value="C" type="radio">
<label for="A">C</label>
<button id="submitbutton"
type="submit">Antworten</button>
</form>
</div>
</div>
</body>
<?php
if(!empty($_POST)) {
if(!empty($_POST['A'])) {
$counter = $counter + 1;
}
}
echo $counter;
?>
Sometimes i get HTTP ERROR 412
Related
i made a simple calculator.But the problem is not radio buttons are not working properly to select the operation to be performed my code for input is as follow:
<form action="output.php" method="post" class="form">
<label for="">Value 1</label>
<input type="text" name="value1" placeholder="Enter 1st value">
<br>
<label for="">Value 2</label>
<input type="text" name="value2" placeholder="Enter 2nd value">
<h1>Select Operator </h1>
<input type="radio" name="addition" value="add">+
<br>
<input type="radio" name="subtraction" value="sub">-
<br>
<input type="radio" name="multiplication" value="mul">*
<br>
<input type="radio" name="division" value="div">/
<br>
<input type="submit" class="btn btn-success" value="Show Result">
</form>
and output php code is as follow:
<?php
$value_1=$_POST['value1'];
$value_2=$_POST['value2'];
if(isset($_POST['submit'])) {
if($_POST['operation'] == add) {
echo "$value_1 + $value_2";
}else if($_POST['operation'] == sub){
echo "$value_1 - $value_2";
}else if($_POST['operation'] == mul){
echo "$value_1 * $value_2";
}else if($_POST['operation'] == div){
echo "$value_1 / $value_2";
}
}
?>
The radio buttons need to share the same name, only the value should change.
label {
display: block;
}
<label><input type="radio" name="operation" value="add">+</label>
<label><input type="radio" name="operation" value="sub">-</label>
<label><input type="radio" name="operation" value="mul">*</label>
<label><input type="radio" name="operation" value="div">/</label>
The submit button has no name - so it will not appear in the POST array and the logic will never be processed. Rather than test for the presence ( or not ) of a button in the POST array you really should test for the actual form elements that you will use in your calculations.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>simple calculator</title>
</head>
<body>
<form method="post" class="form">
<label>Value 1 <input type="text" name="value1" placeholder="Enter 1st value"></label>
<label>Value 2<input type="text" name="value2" placeholder="Enter 2nd value"></label>
<h1>Select Operator </h1>
<input type="radio" name="operator" value="add">+
<br>
<input type="radio" name="operator" value="sub">-
<br>
<input type="radio" name="operator" value="mul">*
<br>
<input type="radio" name="operator" value="div">/
<br>
<input type="submit" class="btn btn-success" value="Show Result">
</form>
<?php
/*
Ensure that ALL post fields are set
*/
if( isset(
$_POST['value1'],
$_POST['value2'],
$_POST['operator']
)) {
/*
Assign POST values as variables.
*/
$value_1=(int)$_POST['value1'];
$value_2=(int)$_POST['value2'];
$operator=$_POST['operator'];
/*
Perform the relevant calculation
and assign the correct symbol for display
*/
switch( $operator ){
case 'add':$symbol='+';$res=$value_1 + $value_2;break;
case 'sub':$symbol='-';$res=$value_1 - $value_2;break;
case 'mul':$symbol='*';$res=$value_1 * $value_2;break;
case 'div':$symbol='/';$res=$value_1 / $value_2;break;
}
/*
print the sum and result
*/
printf(
'%d %s %s=%d',
$value_1,
$symbol,
$value_2,
$res
);
}
?>
</body>
</html>
I have a code. Somehow, it only able to pick the last hidden input name field instead of the other one. I also tried use if and else but nothing is displayed. Please advise.
Without the if else cases scenario:
HTML:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price" value="7.50">
<label for="ldktech_product_good">Good</label>
NOTE: Please include the charger with your iPad trade-in, or a replacement fee will be deducted from the offer
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP:
$condition = $_POST["conditition"];
$price = $_POST["price"];
echo $price;
echo "<br>";
echo $condition;
With the if else scenarios:
HTML Code:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP code:
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];}
echo $price;
echo "<br>";
echo $condition;
Nothing work. Please advised
works fine for me. don't know if your form method is post or not or your action url is set to your script url or if your script is on the same page make sure you place it accordingly.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>
</head>
<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" method="post">
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label> <br /><br />
<button id="mysubmit" type="submit">Submit</button><br /><br />
</form>
</div>
<?php
if(isset($_POST["conditition"])){
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];
}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];
}
echo $price;
echo "<br>";
echo $condition;
}
?>
</body>
</html>
I believe this is what you're trying to do?
EDIT:
<form method="post">
<div class="tab-label">
<input type="radio" name="condition1" value="good">
<input type="hidden" name="price1" value="7.50">
<label for="ldktech_product_good">Good</label>
</div>
<div class="tab-label">
<input type="radio" name="condition2" value="flawless">
<input type="hidden" name="price2" value="10">
<label for="ldktech_product_flawless">Flawless</label>
</div>
<input type="submit" name="submit" value="Get records">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['condition1']) && $_POST['condition1'] == "good") {
echo "You have selected :".$_POST['price1']; // Displaying Selected Value
} else if (isset($_POST['condition2']) && $_POST['condition2'] == "flawless") {
echo "You have selected :".$_POST['price2']; // Displaying Selected Value
}
}
?>
I am working on a php situation that i have. What it should do is like. each product has a unique id value. if the product id is posted throught submit button, A count action should be activited to count the number of time that product id is posted, so increment.
I hope i exposed the situaation clearly. This is the way i thought doing it, but can't get it work:
<?php
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do;
if ($do > 1)
{
$i= $do+1;
echo $i;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/orange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="2" />
<input type="hidden" id="price" name="price" value="25" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="3" />
<input type="hidden" id="prrice" name="price" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
If I assume the value in the hidden input #id is the times users vote the product:
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do++;
}
If I assume it incorrectly I don't understand what you want to do, sincerly.
I am a beginner in PHP. So I am working on a small task in which I want to make a form in which there are MCQS and there is a countdown timer above it of 10 minutes. When the count down timer is up the form should expire and should show the result automatically.
I have made the form as coded below but I don't know what to do so that I can add timer in this code so that the form expires and shows the result of the right selected answers.
<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<div id="page-wrap">
<h1>Final Quiz for Lip building</h1>
<form action="quiz.php" method="post" id="quiz">
<ol>
<li>
<h3>CSS Stands for...</h3>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A) Computer Styled Sections </label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B">B) Cascading Style Sheets</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C">C) Crazy Solid Shapes</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D">D) None of the above</label>
</div>
</li>
<li>
<h3>Internet Explorer 6 was released in...</h3>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-A" value="A" />
<label for="question-2-answers-A">A) 2001</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-B" value="B" />
<label for="question-2-answers-B">B) 1998</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-C" value="C" />
<label for="question-2-answers-C">C) 2006</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-D" value="D" />
<label for="question-2-answers-D">D) 2003</label>
</div>
</li>
<li>
<h3>SEO Stand for...</h3>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
<label for="question-3-answers-A">A) Secret Enterprise Organizations</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
<label for="question-3-answers-B">B) Special Endowment Opportunity</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
<label for="question-3-answers-C">C) Search Engine Optimization</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-D" value="D" />
<label for="question-3-answers-D">D) Seals End Olives</label>
</div>
</li>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
</div>
</body>
</html>
You need JavaScript for such behavior, let's say you use the following JS:
window.setTimeout(function() {
document.forms['form_name'].submit();
}, 2000);
form_name should be the name of your form. i.e
<form name = "form_name" ... >
</form>
This will delay the post in milliseconds (2 seconds) and then it should take you to another page where you can show the correct answers.
Fiddle
This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>