Html/php form not out-putting - php

hi i'm trying to implement a small feature including a price checker (just testing for now!) but when i hit submit nothing is ecohed back to me! here is my code:
<!doctype html>
<head>
<meta charset="UTF-8">
<title>ValleyRangesPriceCheck</title>
<link REL='stylesheet' TYPE='text/css' HREF='Style.css'>
</head>
<body>
<form>
<input type="date" name="check-in">
<input type="date" name="check-out">
<input type="number" name="num-of-guests" min="1" max="10" placeholder="Guests">
<select name="operator">
<option>Candelight</option>
<option>Misty Woods</option>
<option>Beach Mont</option>
</select>
<button type="submit" name="submit" value="submit">submit</button>
</form>
<p> The Answer Is:</p>
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['check-in'];
$result2 = $_GET['check-out'];
$operator = $_GET['operator'];
switch ($operator){
case "Candelight":
echo $result1 + $result2;
break;
}
}
?>
</body>
</html>
Thanks for the help!

The form method and action attributes are not defined. You should define where the user data should be posted.
Try this code,
<!doctype html>
<head>
<meta charset="UTF-8">
<title>ValleyRangesPriceCheck</title>
<link REL='stylesheet' TYPE='text/css' HREF='Style.css'>
</head>
<body>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="date" name="check-in">
<input type="date" name="check-out">
<input type="number" name="num-of-guests" min="1" max="10" placeholder="Guests">
<select name="operator">
<option>Candelight</option>
<option>Misty Woods</option>
<option>Beach Mont</option>
</select>
<button type="submit" name="submit" value="submit">submit</button>
</form>
<p> The Answer Is:</p>
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['check-in'];
$result2 = $_GET['check-out'];
$operator = $_GET['operator'];
switch ($operator){
case "Candelight":
echo $result1 + $result2;
break;
}
}
?>
</body>
</html>

Related

MadLibs overwrite inputfields

im trying to make a madlibs kind of game, right now i got everything working besides 1 thing. the output should overwrite the questions instead of showing above the questions. how can i manage to do this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Mad Libs</title>
<link rel="stylesheet" type="text/css" href="MadLibs.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:wght#700&display=swap" rel="stylesheet">
</head>
<body>
<form action="" method="POST">
<h1> MadLibs</h1>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Input1 = $_POST["Input1"];
$Input2 = $_POST["Input2"];
$Input3 = $_POST["Input3"];
$Input4 = $_POST["Input4"];
echo "here is a test $Input1. here is an other test $Input2 and again an other test
$Input4 test $Input3.";
}else{
<p id="Text">test question <input type="text" required name="Input1" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input2" placeholder="Enter your answer.."></p>
<p id="Text">test question<input type="text" required name="Input3" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input4" placeholder="Enter your answer.."></p>
<input id="Submit" type="submit" name="submit" value="Send">
</form>
?>
<footer>
<p> A #2021 website</p>
</footer>
</body>
</html>
The end result shows like this but the questions and the input field should be gone when the submit button is pressed

Beginner PHP $_POST issue (input type?)

I'm trying to figure out what's going wrong here. I'm trying to post a question number to the page, so as you answer questions it increments up. For some reason I can't get it to post, the only way I got it to work was when I include var_dump($q_num); after the part where I increment the $q_num variable.
I'm assuming this is something very simple but I'm not sure what it is. Any help would be appreciated, thanks!
I know I should sanitize the input, and I've tried a FILTER
<?php
include('inc/quiz.php');
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Math Quiz: Addition</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<?php
if (isset($_POST['page'])){
$q_num = (int)$_POST['page'];
} else {
$q_num = 1;
}
$p_num = 10;
?>
<?php var_dump($q_num);?>
<div class="container">
<div id="quiz-box">
<p class="breadcrumbs">Question #<?= $q_num;?> of <?= $p_num;?></p>
<p class="quiz">What is <?php
?> </p>
<form action="index.php" method="POST">
<input type="hidden" name="page" value="<?php ($q_num+1);?>" />
<input type="submit" class="btn" name="answer" value="135" />
<input type="submit" class="btn" name="answer" value="125" />
<input type="submit" class="btn" name="answer" value="115" />
</form>
</div>
</div>
</body>
</html

500 Error When Attempting to Echo Form data in PHP

I am getting an error page from the server when trying to view my test.php page. Everything on the page loads fine if I remove the php tags.
I am getting an "http error 500" with the following code:
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<?php
echo "<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value='".date()."'>
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>";
?>
</body>
</html>
Or even strip out most of the echo/print
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value="<?php print(date()); ?>">
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>
</body>
</html>
```
<?php
echo '<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value="'.date().'">
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>';
?>
```
this should work.
Use the following:-
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<?php
echo "<form>
<input type=\"hidden\" name=\"uid\" value=\"Anonymous\">
<input type=\"hidden\" name=\"date\" value='".date()."'>
<textarea name=\"Message\"></textarea><br>
<button type=\"submit\" name=\"submit\">Comment</button>
</form>";
?>
</body>
</html>

simple html dom filter form to get name and values as php array

Im new to using simple_html_dom, i have this form which i dont intend to submit but i want to get the names and values as an array in php.
$url = '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<div class="waitmsg" id="WaitMessage" style="display:none">
<WaitMessage>Transaction Processing, Please Wait...</WaitMessage>
</div>
<form id="mpiRun" action="form.php" method="post">
<input type="hidden" name="dest" value="33777" />
<input type="hidden" name="one" value="102900" />
<br>
<br>
<noscript>
<center>
<h1>Processing Transaction</h1>
<input type="submit" />
</center>
</noscript>
</form>
</body>
</html>';
I want to search through this in and return a php array e.g
Array
(
[dest] => 33777
[one] => 102900
)
this should do the trick:
<?php
include_once('simple_html_dom.php');
$url = '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<div class="waitmsg" id="WaitMessage" style="display:none">
<WaitMessage>Transaction Processing, Please Wait...</WaitMessage>
</div>
<form id="mpiRun" action="form.php" method="post">
<input type="hidden" name="dest" value="33777" />
<input type="hidden" name="one" value="102900" />
<br>
<br>
<noscript>
<center>
<h1>Processing Transaction</h1>
<input type="submit" />
</center>
</noscript>
</form>';
$html = str_get_html($url);
$arr = [];
foreach ($html->find('input') as $element) {
if ($element->type == "hidden") {
$arr[$element->name] = $element->value;
}
}

Display text after submit button is clicked. PHP

I am trying to catch some text from the textbox, to save it in a variable and then post it on my browser if I click the submit button. My code is this:
<?php
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</body>
</html>
I don't know what is wrong. The code on rextester
For starters you need to implement a form on you HTML page and set the action to a new .php page.
<form action='display.php' method='post'>
*your input fields go here*
Now create a new php page (in this case display.php)
Declare the variables as you did....
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
then you can simply echo each variable accordingly...
Final Result
HTML PAGE:
<form action='result.php' method='post'>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
result.php
<?php
$var1 = $_POST['text1']; // create variables of your form elements
$var2 = $_POST['display'];
echo '$var1 . ' ' . $display . '.';
?>
Basically the php page is saying get text1 and display from the form (names) and create variables of them...
Then echo (print) these variables on the screen. (in plain english xD)
p.s
Your rextester isn't working because you haven't specified a form.
You need to setup the FORM tags so they have the correct attributes for POST. Try:
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
</body>
</html>
You need to use a form, otherwise you aren't posting anything.
Something like:
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form method="post" action="yourphpfile.php">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="text" value="display" name="display" id="display" /><br /><br />
<input type="submit" value="submit"><br /><br />
</form>
</body>
</html>
yourphpfile.php
<?php
if(!empty($_POST['text1']) and !empty($_POST['display'])){
$var1=$_POST['text1'];
$var2=$_POST['display'];
echo "$var1 $var2";
}
?>
Check this complete example
To display text box and button on clicking submit button
<form>
<input type="submit" value="OK" name="btn_name"/><br />
<?php
if(isset($_GET['btn_name']))
{
echo "<input type=text name=txt_userinput />";
echo "<input type=submit value=Area name='btn_calculate'/>";
}
?>
</form>

Categories