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
}
}
?>
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>
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
I am trying to insert an array of dynamically generated input field into my db.
The problem is that the insertion gets messed up during insertion. Some records get inserted into the wrong Rows when Some field in the form are left empty.
I red somewhere that i have to index my arrays properly. i don't know how to do this. Is there a better way of doing this? or is there a way of fixing my code.
I have searched everywhere for help and i haven't found any. I would really appreciate it if i could get some help from the experts here.
Here is the form
<?php
while($row2=$coursefetch1->fetch()){
$iid = $row2['id'];
if($row2['type'] == 0){
echo '
<div class="tab-pane" id="'.$row2['id'].'">
<div class="row" align="center">
<b> '.$row2['question'].' </b><br>
<input type="hidden" name="question[]" value="'.$row2['question'].'">
<input type="hidden" name="test_id[]" value="'.$row2['test_id'].'">
<input type="checkbox" name="yanswer1[]" value="a"><span> '.$row2['a'].' </span><br>
<input type="checkbox" name="yanswer2[]" value="b"><span> '.$row2['b'].' </span><br>
<input type="checkbox" name="yanswer3[]" value="c"><span> '.$row2['c'].' </span><br>
<input type="checkbox" name="yanswer4[]" value="d"><span> '.$row2['d'].' </span><br>
</div>
</div>
';
}
elseif($row2['type'] == 2){
echo '
<div class="tab-pane" id="'.$row2['id'].'">
<div class="row" align="center">
<b> '.$row2['question'].' </b><br>
<input type="hidden" name="question[]" value="'.$row2['question'].'">
<input type="hidden" name="test_id[]" value="'.$row2['test_id'].'">
<input type="radio" name="yanswer1[]" value="a"><span> '.$row2['a'].' </span><br>
<input type="radio" name="yanswer2[]" value="b"><span> '.$row2['b'].' </span><br>
<input type="radio" name="yanswer3[]" value="c"><span> '.$row2['c'].' </span><br>
<input type="radio" name="yanswer4[]" value="d"><span> '.$row2['d'].' </span><br>
</div>
</div>
';
}
elseif($row2['type'] == 1){
echo '
<div class="tab-pane" id="'.$row2['id'].'">
<div class="row" align="center">
<b> '.$row2['question'].' </b><br>
<input type="hidden" name="question[]" value="'.$row2['question'].'">
<input type="hidden" name="test_id[]" value="'.$row2['test_id'].'">
<input type="text" name="yanswer1[]" value="">
</div>
</div>
';
}
}
?>
Here is the file that processes the form
<?php
require "head.php";
if (isset($_POST['submit'])) {
$question_array = $_POST['question'];
$testid_array = $_POST['test_id'];
$answer_array1 = $_POST['yanswer1'];
$answer_array2 = $_POST['yanswer2'];
$answer_array3 = $_POST['yanswer3'];
$answer_array4 = $_POST['yanswer4'];
$i = 0;
for($i = 0; $i < count($question_array); $i++) {
$question = $question_array[$i];
$testid = $testid_array[$i];
$answer = $answer_array1[$i]."".$answer_array2[$i]."".$answer_array3[$i]."".$answer_array4[$i];
$enterscore = $achilles->prepare("INSERT INTO `user_answer` (test_id,question,your_answer)
VALUES (:t,:q,:a)");
$enterscore->bindparam(':t',$testid);
$enterscore->bindparam(':q',$question);
$enterscore->bindparam(':a',$answer);
$enterscore->execute();
}
}
?>
...
<input type="hidden" name="test_id" value="'.$row2['test_id'].'">
<input type="radio" name="yanswer1" value="a"><span> '.$row2['a'].' </span><br>
<input type="radio" name="yanswer2" value="b"><span> '.$row2['b'].' </span><br>
<input type="radio" name="yanswer3" value="c"><span> '.$row2['c'].' </span><br>
...
in other places by analogy
edit:
...
if (!empty($_POST['submit'])) {
...
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 using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />