Form isn't shown - php

I have a file test.php with the following code:
<html>
<head>
<title>Listing 10.2 </title>
</head>
<body>
<div>
<form method="post" action="test.php" >
<p> <input type="text" name="guess"/> </p>
</form>
<?php
if(!empty($_POST['guess'] )) {
print "Last guess $_POST['guess']";
}
?>
</div>
</body>
</html>
I have a problem with the form not showing up. However, if I remove the PHP portion of the code, it is visible. What is my problem?

You can't embed the $_POST variable into the string like that, trying changing the php section to:
<?php
if(!empty($_POST['guess'] )) {
print "Last guess {$_POST['guess']}";
}
?>

Use . for combine values and strings
if(!empty($_POST['guess'] ))
{
echo "Last guess".$_POST['guess'];
}

Related

Im trying to use the $_GET method to obtain whatever the user has typed in a form and then using it in an if statement. It has come up with an error

this is the code for my html page in the site:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<html>
<body>
<form action="Untitled-4.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
</body>
</html>
and this is my code for the "Untitled-4.php" file:
<html>
<body>
<?php
if ($_GET["name"] == "tom"){
echo "hello you are special";
}
?>
</body>
</html>
this is the error i am getting:
"undefined index on line 7 of the php file"
I am trying to use the GET method to take whatever the user types in the "name" box to be used in the if statement that echoes "you are special" if the user types in "tom". can anyone tell me what the problem here is.
(the question stack overflow is saying is a possible duplicate is a completely different question)
Check if the variable is set first:
<html>
<body>
<?php
if (isset($_GET["name"])) {
if ($_GET["name"] == "tom"){
echo "hello you are special";}
} else {
echo "you are not special";
}
?>
</body>
</html>
for some reason i just tried it again with no changes to my code and it appears to be working i did not actually have to do anything.

How come nothing is appearing when I add if statements to my PHP code?

I am trying to make a form that echos out whatever the user inputs into the 'name' field, but, I cannot figure out why when I add an if statement, nothing in the code shows up.
Here is my index.php code:
<?
if (empty($_POST['name'])) {
echo "Hello world!";
} else {
echo "Hello $_POST['name']";
}
?>
<html>
<head>
<?php include 'header.php'; ?>
</head>
<body>
<form action="index.php" method="post">
<p>Name: <input type="text" name="name"></p>
<input type="submit" name="submit" value="Go">
</form>
</body>
<footer>
<?php include 'footer.php'; ?>
</footer>
</html>
The valid syntax for adding an array into echo is:
echo "Hello {$_POST['name']}";
Or concatenate it using:
echo "Hello " . $_POST['name'];
Reference: echo
Make sure you enable errors while debugging:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Problem, seems to be with php short tag <? which might not enabled, try using <?php or enable short tag in php.ini
The page is reloading index.php with your form. change <form action="index.php" method="post"> to <form action="" method="post">
You can't echo array inside "" quotes.
Try this (var in {}):
echo "Hello {$_POST['name']}";
Or this (with string operator):
echo "Hello " . $_POST['name'];

Php file not displaying data collected from html file

I'm trying to link a simple html file with a php file.The data from the html file is properly getting transferred to php file , but while displaying that data through php file nothing is gets displayed on the browser.
html:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">Calculator</font></b></i></header>
<form action="result.php" method="POST" >
<br>
<br>
Number 1: <input type="text" name="number1">
<br><br>
Number 2: <input type="text" name="number2">
<br>
<br>
<input type="submit">
</form>
</body>
</html>
php:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">RESULT</font></b></i></header>
<?
$var1= $_POST['number1'];
$var2= $_POST['number2'];
echo $var1;
echo $var2;
?>
</body>
</html>
<form action="result.php" method="GET" >
you are using GET and receiving as POST
$var1= $_POST['number1'];
$var2= $_POST['number2'];
you change either one of them, but my advice would be to change your form action to:
<form action="result.php" method="POST" >
You're are using method as GET in your source file but getting as $_POST in the destination file.
You should use method="POST" from your source file.
So, It should have something like
<form action="result.php" method="POST" >
Learn more about Sending data from Form Here
How do you know that the data is beign transfered? Try this as it seems that you are using GET instead of post, then use the proper one:
<?
echo 'POST: ';
var_dump($_POST);
echo 'GET: ';
var_dump($_GET);
?>
As of this:
<form action="result.php" method="GET" >
you should either do:
$var1= $_GET['number1'];
$var2= $_GET['number2'];
or change
<form action="result.php" method="POST" >
(which is anyway recommended ...)
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">RESULT</font></b></i></header>
<?php
$var1= $_GET['number1'];
$var2= $_GET['number2'];
echo $var1;
echo $var2;
?>
</body>
</html>

PHP - Can't submit form to handler

Here is my Code :
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" action="../../../wamp/www/abc.php" method="get">
<input type="text" name="text1" id="username" />
<input type="submit" />
</form>
</body>
</html>
The abc.php file is located in C:/wamp/www/abc.php
<html>
<body>
Hello World
<?php
echo "Hello";
echo $_GET["username"];
?>
</body>
</html>
In the form, when I press the Submit button, only "Hello World" is displayed.
The value entered in the textbox is not displayed at all. Even the "Hello" message printed inside the php code is not displayed.
How can i display the Value ?
text1 is the name of your input, so the correct PHP code would be.
<?php
echo "Hello ";
echo $_GET['text1'];
?>
Try the below, get array is created using name attr not id one.
echo $_GET["text1"];
There is two solution for you should use :
First Way :Change your php code:
<?php
echo "hello";
echo $_GET["text1"];
?>
Second Way: Chnage your HTML
<input type="text" name="username" id="username"/>
If second "Hello" from echo is not displayed, there is a problem with your PHP installation.

PHP if-statement using $_POST variable doesn't seem to work. Why?

On one PHP server I have two files. One file (the name is "first.php") contains this code:
<html>
<head>
<title>First Page</title>
</head>
<body>
Please enter your password and age:
<form action="pass.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
The other file ("pass.php") contains this code:
<html>
<head>
<title>Secon Page</title>
</head>
<body>
<?php
if ($fname=="Jack")
echo "You are Jack!";
else
echo "You are not Jack!";
?>
</body>
</html>
As far as I understand, if a user enters "Jack" in the first page, than the second page should be displayed with "You are Jack!" line, but it doesn't happen. Why is it so?
On your second page, instead of checking for $fname, check for $_POST['fname'] instead. I think that is all you are missing.
You probably don't have register_globals set. This is depreciated and will be removed in 6.x. So for good programming you should instead of $fname try $_POST['fname'] on your second page.
pass.php needs to look like this
<html>
<head>
<title>Secon Page</title>
</head>
<body>
<?php
if ($_POST['fname'] =="Jack")
echo "You are Jack!";
else
echo "You are not Jack!";
?>
</body>
</html>
It might help to set the post values as variables and work with that. Something like this:
foreach($_POST as $key => $value)
{
$$key = $value;
}
Then whatever is posted will be available rather than using $_POST['xxxxx'] in your logic.

Categories