500 Error When Attempting to Echo Form data in PHP - 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>

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

Html/php form not out-putting

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>

Form always submits as GET even with POST method

I am having a problem with my form submission. The code is below. When I submit, it is submitting as GET and not as POST, even though my method is post. I have tried method="POST" and method="post". What am I missing here?
<!doctype html>
<html>
<head>
<title>Test</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<?php
echo "POST: ";
print_r($_POST);
echo " GET :";
print_r($_GET);
?>
<form role="form" name='test' method=“post” action="test2.php">
<label for="name">Name</label>
<input name="name" type="text" />
<input type="submit" name="submit" />
</form>
</div>
</body>

Form not using the file in action attribute

I have a very simple HTML form that is supposed to send information to the file written in action attribute via GET but somehow it's transfering the information back to index.php:
index.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Forms Sandbox</h1>
<form acton="process_form.php" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
</body>
</html>
process_form.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Response Sandbox</h1>
<?php
$username = $_GET["username"];
$email = $_GET["email"];
echo $username . " : " . $email . "<br />";
?>
</body>
</html>
The bizarre aspect is that when I submit the form, the URL shows that it is not even using process_form.php:
http://127.0.0.1/Learning/?username=test&email=x%40test.com&submit_btn=Submit
If I manually change the URL to include process_form.php it seems to be working fine and I get the results I am looking for:
http://127.0.0.1/Learning/process_form.php?username=test&email=x%40test.com&submit_btn=Submit
On my development computer, I'm running EasyPHP 14.1 local WAMP server and thought it might be the root of the problem so I uploaded the files to my website that is running newest PHP on Apache, but the problem still exists there.
What am I doing wrong?
you have a typo error in action; you have given acton. Should be like this:
<form action="process_form.php" method="get">
First thing - you have a typo:
<form action="process_form.php" method="get">
^
The second thing - in my opinion the best method of handling forms is using POST method, not GET, so I would change it to:
<form action="process_form.php" method="post">
and in process_form.php I would use $_POST instead of $_GET
After digging around your question,
Index.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Forms Sandbox</h1>
<form action="process_form.php" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
</body>
</html>
process_form.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Response Sandbox</h1>
<?php
$username = $_GET["username"];
$email = $_GET["email"];
echo $username . " : " . $email . "<br />";
?>
</body>
</html>
Note: If you will not specify form method, By default it will take GET method. So please make sure action should be perfect.
Above code just copy and paste, it should work perfect.
Ask me for further clarification.
Thanks,
Gauttam

Categories