PHP Won't Echo out anything with $_POST - php

I have 2 files, index.html, and welcome.php.
index.html:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
And here is the PHP:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
Echo seems to work whenever i'm not using $_POST, with just regular text or variables. Is there something I'm doing wrong? I am also writing this code on repl.it, could that have something to do with it? Major newbie here.

Related

Input from form to url?

So the goal is to take the input from a form append it to the end of the URL and then return the HTML from that page.
I am not entirely sure how to take the forms value (in this case $2) and attach it to the URL.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$2")?>
<?php echo $_POST ["$html"];?>
</body>
</html>
On submit any input from a form with method="POST" will be stored in the $_POST global. You can access it from there and append it to your URL string.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="iacoCode" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST["iacoCode"])) {
$html = file_get_contents("http://www.metar.mysite.net/metar?id=" . $_POST["iacoCode"]);
echo $html;
}
?>
</body>
</html>
Using the IF statement to check if it is set will prevent it from loading the URL with no variable.
The way we extract data via php from a form is like below
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$_POST['$2']")?>
<?php echo $_POST ["$html"];?>

404 error, on mamp server, file not found

I get the following 404 error when trying to access a PHP file on MAMP
"The requested URL /‘welcome.php’ was not found on this server."
Both the html file and the php file run fine via localhost just not from the file action.
They are in the same location and the action looks like action='welcome.php'
<html><head charset=“utf-8”>></head><form action=‘welcome.php’ enctype="text/plain" method=“post”>
First name: <input type="text" name="firstname"><br> Last name: <input type="text"
name="lastname"> <button action=“submit”>Submit </button> </form> </html>
welcome.php looks like this
<html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo
$_POST["email"]; ?> </body> </html>
You have a few curly quotes in your code “ ” and ‘ ’ which will explain the "file not found" error for your form's action action=‘welcome.php’. You should also remove enctype="text/plain" - consult this answer on Stack for more information about it.
You are also using the wrong names name="firstname" and $_POST["name"].
Also name="lastname" and $_POST["email"], those must match.
Your Html file should now read as:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
and welcome.php file:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
The requested URL /‘welcome.php’ was not found on this server.
This is because you use wrong quotes (quotation marks) in your HTML. In markup you should use nothing else then " or '.
So your code should looks like:
<html>
<head charset="utf-8"></head>
<body>
<form action="welcome.php" enctype="text/plain" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<button action="submit">Submit</button>
</form>
</body>
</html>

PHP - form action calling itself, how to display everything in 1 page?

I have two .php files as such:
test1.php
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
test2.php
<html>
<head>
<title>Sample Display Page</title>
</head>
<body>
<?php
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
?>
</body>
</html>
I want to know how can I get it all to display on a single page, i.e. on the test1.php, without having to have two files.
Check if $_POST["userNumber"] isset, and echo the form if it's not.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST["userNumber"]){
echo "You have chosen ".$_POST["userNumber"];
}else{
echo '<form action="test1.php" method="post">';
echo 'Please enter a number <input type="number" name="userNumber"><br>';
echo '<input type="submit">';
echo '</form>';
}
?>
</body>
</html>
Your test1.php will need to look like this
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test1.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
</body>
</html>
You simply need to remove the action="test2.php" section and combine both pages into one just like the following:
<html>
<head>
<title>Sample</title>
</head>
<body>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST['userNumber'])){
$user_number = $_POST['userNumber'];
echo "You have chosen $user_number";
}
?>
</body>
</html>
Pretty simple actually:
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST) && isset($_POST['userNumber'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if (isset($_POST['submitted'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
else {
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submitted">
</form>
<?php
}
?>
</body>
</html>
Just use isset() for submit button, note that i added name="submit" to your button since it was missing
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST['submit'])
{
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
} else {
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<?php } ?>
</body>
</html>
This one is fully tested and allows you to revise the value after submission. You still have to worry about cross-site scripting attacks, etc... but that is outside of the scope of the question.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
$user_number = '';
if (isset($_POST['userNumber'])) {
$user_number=$_POST['userNumber'];
?>
<p>You have chosen <?php echo $user_number ?></p>
<?php
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber" value="<?php echo $user_number ?>"><br>
<input type="submit">
</form>
</body>
</html>
set the variables up before the form as placeholders and check for isset
<?php
$user_number = '';
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
}
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<p>You have chosen: <?php echo $user_number ?></p>
Sample
<form action="<?php $_PHP_SELF ?>" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body> </html>

Basic Form using HTML and Php

I am trying to create just a basic form using Php and HTML. When I type into the fields and click submit nothing appears on the page except for "Hi,"
This is what I have for the HTML document
<!DOCTYPE html>
<html>
<head>
<title>Php Form</title>
</head>
<body>
<form method="post" action="PHPD1.php">
<label> First Name:<input name="FName" type="text"/></label>
<br>
<label> Last Name:<input name="LName" type="text"/></label>
<br>
<label>Message: <textarea name="message">
</textarea>
</label>
<br>
<input name="submit" type="submit" value="Submit"/>
</form>
</body>
</html>`
and for the php document I have
<html>
<head>
<title>Deliverable 1</title>
</head>
<?php
// Capture the values posted to this php program from the text fields
$FName = $_POST['FName'] ;
$LName = $_POST['LName'] ;
$Message = $_POST['Message'] ;
?>
<body>
Hi, <?php print $FName; ?>
<br>
<?php print $Message; ?>
</body>
</html>
<textarea name="message">
</textarea>
the name given for textarea is "message". so the same name should be used in the php page also to access the value.
instead of
$Message = $_POST['Message'] ;
use
$Message = $_POST['message'] ;
you have done a small mistake.
just check now.

Printing text box value on page in PHP

I am new to PHP (in-fact I am learning it). I am trying to get the value of textbox defined in HTML and print it on HTML page through PHP code. I am able to send the value of textbox to another page using form post and getting it using $_POST['name'] on other page.
I am not able to print the value of textbox on same page.
Here is my code:
<html>
<head>
<?php
function myfunction()
{
printf($_POST['fname']);
}
?>
</head>
<body>
Name: <input type="text" name="fname" />
<input type="button" onClick="myfunction()" value="Click" />
</body>
</html>
First of all it's not required to write php function within <head> tags. Second,
you can't POST data without <form> tags. try this
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
$name=$_POST['fname'];
echo $name;
}
else {
?>
<html>
<head>
</head>
<body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
Name: <input type="text" name="fname" />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php } ?>
you can not call PHP function by button's click event...
if you want to print on same page then you can do by below code..
<html>
<?php
if(count($_POST)>0){
echo $_POST['fname'];
}
?>
<body>
<form method='post'>
Name: <input type="text" name="fname" />
<input type="submit" value="Click" />
</form>
</body>
</html>
hope you will get what you want by my answer..

Categories