<?php
if ($_POST['discount'] == 'text1')
echo "Discount Applied!";
else
header('Location:fifapack.html');
?>
I want to be able to add more words after text 1 and not just one valid word.
<div id="dicount">
<form action=discounts.php method=post>
<center>Discount Code:<input type=text name=discount>
<input type=submit value=Apply>
</center>
</div>
This is my html.
You can send several discounts this way:
<div id="dicount">
<form action=discounts.php method=post>
<center>Discount Code:<input type="text" name="discount[]">
<center>Discount Code:<input type="text" name="discount[]">
<center>Discount Code:<input type="text" name="discount[]">
<center>Discount Code:<input type="text" name="discount[]">
<input type=submit value=Apply>
</center>
</div>
And in your php:
$b = filter_input(INPUT_POST, 'discount', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY);
if($b) {
// Do whatever you like with the array of discounts
}
To determine if text1 is in your POST, you can simply use:
if (strpos($_POST['discount'], 'text1')) {
...
}
If I understand you correctly you want to validate $_POST['discount'] against more
words?
<?php
if (($_POST['discount'] == 'text1') or ($_POST['discount'] == 'whatever')) {
echo "Discount Applied!";
}
else {
header('Location:fifapack.html');
}
?>
Related
I write a conversion from celsius to fahrenheit and vice versa. The problem is that in the form fields now random values are displayed to me. How to convert this code so that after entering the value, the converted value in the second field appears in the first field?
Is it possible to use only php at all?
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}
if(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
I want the value entered in the first field to be shown in the second transformed.
You can do all php if you post back to itself. If the page with the input is calc.php
Add an else to set the value to empty string.
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
} else {
$cel = '';
}
if(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
} else {
$fah = '';
}
Try something like this
$cel="";
$fah="";
if(isset($_POST['fah']) && !empty($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}
if(isset($_POST['cel']) && !empty($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
You can try this example:
$celValue = $_POST['cel'];
$fahValue = $_POST['fah'];
if( ! empty($fahValue)){
$celValue = fahrenheitToCelcius($_POST['fah']);
}
if( ! empty($celValue)){
$fahValue = celciusToFahrenheit($_POST['cel']);
}
function celciusToFahrenheit($cel) {
return ($cel - 32) * 1.8;
}
function fahrenheitToCelcius($fah) {
return ($fah + 32) / 1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $celValue; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $fahValue; ?>" name="cel">
<input type="submit" value="Calc">
</form>
Since both the variables give isset() true so we can have something like this
if(isset($_POST['fah']) && isset($_POST['cel'])) {
//if cel is not empty
if(!empty($_POST['cel'])) {
$cel = $_POST['cel'];
$fah=($cel-32)*1.8;
} else if(!empty($_POST['fah']){
$fah = $_POST['fah'];
$cel = ($fah+32)/1.8;
}
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
You just missing set default values that will overwrite if other post value exist, and else if to load only one parameter so other will be empty
<?php
$cel = "";
$fah = "";
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}elseif(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
Hi I have a question about generating rows and columns. would like to ask about how can I make it in one page only. This is what I have tried.
HTML:
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="get_table/execute_table.php" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
PHP:
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Yes, it is fully running but I want it in 1 page only. Thanks.
Normally, if you want it on the same page, you just omit the action="" with its value.
Then of course, put the php process in the same page as the form:
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<!-- ^^ no more value, well you could just put the same filename -->
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
<?php
$out = ''; // initialize a string holder and when the submission is done, concatenate all the strings
if(isset($_POST['submit'])) { // catch submission button
$row = $_POST['title1'];
$column = $_POST['title2'];
$out .= "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
$out .= "<tr>";
for($td=1;$td<=$column;$td++){
$out .= "<td>row: ".$tr." column: ".$td."</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
}
echo $out; // finally echo it
to achieve this by using only one page just leave the action attribute empty. and add your php block on top.and make sure to save it as .php and place your php code block on top of all html
so in the end it will look like this
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
You need to post to the same page, and check if the PhP Post array has data yet.
Replace the form action with:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
Note: although most (all?) browsers support self-posts with a blank action, this is not technically W3C compliant.
Then when the page is submitted it will reload the same page and populate the POST array. Somewhere on your page add a condition similar to:
if($_POST['title']){
//do whatever get_table/execute_table.php did
}else{
//echo the form here or, if you're allowed, use an include()
}
More info on self-posting:
How do I make a PHP form that submits to self?
I have 3 forms with 3 radios each but executed one at a time because of random generator. If I click a radio, it will go to next page and will be checked by the if else statement. Now my problem is, when it gets answered and it's correct, it will output yes. However, my other if else statement of my other radio executes. Since it's not answered, it assumed that it was wrong, so it outputted no.
here's my code:
<!doctype html>
<html>
<head>
<title>Question and Answer</title>
</head>
<body>
<?php
//Creating random numbers
$rid = rand(1,3);
?>
<?php
if ($rid == 1){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does HTML Stands For ?</h3>
<div>
<input type='radio' name='answerOne' id='answerOne' value='A' />
<label for='answerOneA'>A) Hyper text markup language</label>
</div>
<div>
<input type='radio' name='answerOne' id='answerOne' value='B' />
<label for='answerOneB'>B) Hyper turn mark lingo</label>
</div>
<div>
<input type='radio' name='answerOne' id='answerOne' value='C' />
<label for='answerOneC'>C) Happy tissue mahatma life</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose..'>
</form>";
}
if ($rid == 2){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does CSS Stands For ?</h3>
<div>
<input type='radio' name='answer2' id='answer2' value='A' />
<label for='answer2A'>A) College Computer Studies</label>
</div>
<div>
<input type='radio' name='answer2' id='answer2' value='B' />
<label for='answer2B'>B) Cascading Style Sheet</label>
</div>
<div>
<input type='radio' name='answer2' id='answer2' value='C' />
<label for='answer2C'>C) Cascaded Style Sheet</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose..'>
</form>";
}
if ($rid == 3){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does PHP Stands For ?</h3>
<div>
<input type='radio' name='answer3' id='answer3' value='A' />
<label for='answerOneA'>A) Hyper text markup language</label>
</div>
<div>
<input type='radio' name='answer3' id='answer3' value='B' />
<label for='answerOneB'>B) Hyper turn mark lingo</label>
</div>
<div>
<input type='radio' name='answer3' id='answer3' value='C' />
<label for='answerOneC'>C) Happy tissue mahatma life</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose'>
</form>";
}
?>
</body>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!doctype html>
<html>
<head>
<title>Question and Answer</title>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
$answer = array('A','B','C');
$answer1= $_POST['answerOne'];
$answer2= $_POST['answer2'];
if($answer1 == $answer[0]){
echo 'yes';
}
else if ($answer1 != $answer[0]){
echo 'no';
}
else{
}
if($answer2 == $answer[1]){
echo 'yes';
}
else if ($answer2 != $answer[1]){
echo 'no';
}
?>
</body>
</html>
Here is a cleaner way to do it. Inside is_correct function, it check if particular answer exist in $_POST, if so it returns if the answer is correct:
function is_correct()
{
$answer = array(
'answerOne' => 'A',
'answer2' => 'B',
'answer3' => 'C',
);
foreach ($answer as $k => $v) {
if (array_key_exists($k, $_POST)) {
return $v == $_POST[$k];
}
}
return false;
}
if (is_correct()) {
echo "yes";
} else {
echo "no";
}
i wrote a piece of code to do multiple row deletions based on ticked check boxes.
Now i need to mordify it to do multiple row updates.
i have been failing to access the textbox values for each row.
here's my code, in general.
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<input type="checkbox" <?php echo 'name="SaveEachComment['.$row["comment"].']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment" size="55" value="<?php echo $comment;?>" />
<input type="submit" name="SaveComments" value="submit" />
</form>
I just added a for loop to print multiple form fields. Obviously your iteration would be as many as number of rows, so you can change that part of the code. But try this:
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
echo $_POST['rowcomment'][$key] . "<br />\n";
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php for ($i=0; $i<11; $i++) { ?>
<input type="checkbox" <?php echo 'name="SaveEachComment['.$i.']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment[<? echo $i?>]" size="55" value="<?php echo $comment;?>" />
<br />
<?php } ?>
<input type="submit" name="SaveComments" value="submit" />
</form>
Below is the source code of a program. Can anyone help me to figure out the working of a program.
<?php
session_start();
?>
<?php
$aCaptcha = array (
array(),
array('crocodile'),
array('panda', 'panda bear', 'giant panda'),
array('pig'),
array('tiger'),
array('zebra'),
array('cow'),
array('elephant')
);
if (isset($_POST['register'])) {
$error = array();
if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
$error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
}
if (count($error) == 0) {
echo "<span style='color:red'>Thank you for completing the form.
We shall contact you soon.</span>";
die();
}
}
?>
<form action="index.php" method="post">
<?php
$_SESSION['captcha'] = rand(1, 7);
?>
<td colspan="3"><strong>Contact Form</strong></td>
<p>Full Name : <input type="text" name="Nmaes" value='' />
<p>Mobile No. : <input type="text" name="Nmaes" value='' />
<p>Email id : <input type="text" name="Nmaes" value='' />
<p>Subject : <input type="text" name="Nmaes" value='' />
<p>Message : <input type="text" name="Nmaes" value='' />
<p><img src="<?php echo $path;?>captcha/<?php echo $_SESSION['captcha'];?>.jpg" /></p>
<p>Type the name of the animal you see in the picture above. <input type="text" name="captcha" value='' />
<?php echo(isset($error['captcha']))?$error['captcha']:"";?></p>
<p><label> </label><input type='submit' name='register' value='register' /></p>
</form>
On the first page
random number between 1 and 7 is generated and stored in session
form is displayed
picture in the captcha directory is displayed based on the random number
On the second page
array with acceptable answers is generated - the keys are numbers 1 and 7 and the values are arrays of acceptable answers
the below code checks that the answer given by the user $_POST['captcha'] is one of the acceptable answers $aCaptcha[$_SESSION['captcha']]
if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
$error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
if acceptable then a message is printed out and PHP stops executing
echo "<span style='color:red'>Thank you for completing the form. We shall contact you soon.</span>";
die();