php form never shows anything whenever it's submitted - php

<html>
<body>
hi!
<?php
Hi!
if(htmlspecialchars($_POST["name"] == "hey"))
{
Hi!
}
?>
</body>
</html>
It's probably something small, but I can't figure out why my message never shows when I try running it. I tried echo, print, and just typing the text out on screen, and I can never get the php form to run, it's always blank. Perms are set to 644. The form submitting the block of code's below...
<html>
<body>
<form action="action.php" method="post">
<input type="text" name="name"><br>
<input type="submit">
</form>
</body>
</html>

In addition to the comments and answers from other users regarding your code missing an echo or print() on "Hi", your brackets are mixed up:
if(htmlspecialchars($_POST["name"] == "hey")) should be :
if (htmlspecialchars($_POST["name"]) == "hey")

You have a syntax error, to print data within php you need to use echo:
<?php
echo "Hi!"; // <--- here
if(htmlspecialchars($_POST["name"]) == "hey") // <---- here you was a syntax error too
{
echo "Hi!"; // <--- here
}
?>
Or other related functions like: print, print_r, var_dump

you couldn't just write html inside php without echoing it..
<html>
<body>
hi!
<?php
if(htmlspecialchars($_POST["name"]) == "hey")
{
echo "Hi!";
}
?>
</body>
</html>

Related

How to keep value after post request?

I want to learn how to keep value after a post request.
In my code, I have these variables:
myvar
myans
result
myvar is a random val between 0-5. I will get myans from my input if myans == myvar, then result will be true (else it will be false).
I see myvar before I submit my ans just for trying, but although I see the var when I send it, what I see it is sometimes false and sometimes true. What am I missing?
example.php:
<?php
session_start();
if (!isset($_COOKIE['result'])) {
setcookie("result","EMPTY");
}
setcookie("myvar",rand(0,5));
if (!empty($_POST)) {
if ($_POST['myans'] == $_COOKIE['myvar']) {
setcookie("result","TRUE");
}
else {
setcookie("result","FALSE");
}
}
?>
<html>
<head>
<title>Example</title>
</head>
<body>
<form method="post" action="">
<?php echo $_COOKIE['myvar'] ?>
<input type="text" name="myans">
<input type="submit" value="Send">
<?php echo $_COOKIE['result'] ?>
</form>
</body>
</html>
The problem you were having was caused by your random number generator making creating a new number before comparing to the original. I made some changes to only set "myvar" if it's empty. this will only set it once, but at least you can see your code working as intended. I recommend you plan out what exactly what functionality you want before adding to it.
I also switched you out from the "$_COOKIE" var to "$_SESSION" vars. They are hidden from the client, and by php default last about 24 minutes if not refreshed. I don't know what you plan on using this for, but using cookies allows the end user to manipulate that info. If this is not a concern for you, by all means just uncomment the "setcookie()" lines.
<?php
session_start();
if (!isset($_SESSION['result'])) {
//setcookie("result","EMPTY");
$_SESSION["result"] = "EMPTY";
}
//setcookie("myvar",rand(0,5));
if(empty($_SESSION["myvar"])){
$_SESSION["myvar"] = rand(0,5);
}
//
if (!empty($_POST)) {
if ($_POST['myans'] == $_SESSION['myvar']) {
//setcookie("result","TRUE");
$_SESSION["result"] = "TRUE";
} else {
//setcookie("result","FALSE");
$_SESSION["result"] = "FALSE";
}
}
?>
<html>
<head>
<title>Example</title>
</head>
<body>
<form method="post" action="">
<?php echo isset($_SESSION['myvar']) ? $_SESSION['myvar'] : ""; ?>
<input type="text" name="myans">
<input type="submit" value="Send">
<?php echo isset($_SESSION['result']) ? $_SESSION['result'] : ""; ?>
</form>
</body>
</html>

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'];

How to handel php error

I'm using two files one is of html and another is of php
In my html file I have created this
code for my html file : index.html
<html>
<body>
<form method='get' action='ans.php'
<input type="text" name="name">
<input type="button" name="submit">
</form>
</body>
</html>
When user enter the input into the input tag it will take the user to the ans.php file in which I have coded some php
My php file: ans.php
<?php
$text= $_GET['name'];
echo $text
?>
if(isset($_GET['name']))
{
echo $_GET['name'];
}
else
{
}
Use empty() to check if the variable is present and not empty or false. Add a check -
if(!empty($_GET['name'])){
$text= $_GET['name'];
echo $text;
}
If the variable may contain false then Hanky 웃 Panky's answer will work.

How to get value to work in php and html tag?

The first file (which is html) has two inputs and the second file (php) is the form to display the data when submitted. I want the php file to print whatever message is typed into the input and submitted.
html file:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
welcome_get.php file:
<html>
<body>
Welcome <?php echo $_GET["name"];?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
I know it is probably very simple but for some reason, whenever I type something into the input and submit, it doesn't display the values I inputted. It just displays only the message in the php file:
Welcome
Your email address is:
Use isset to check the variables exist before you use them. i.e.
<html>
<body>
<?php
if(isset($_GET['name'])){
echo 'Welcome '. $_GET["name"] .'<br>';
}
if(isset($_GET['email'])){
echo 'Your email address is: '. $_GET["email"] .'<br>';
}
?>
</body>
</html>
additionally, it's worth ensuring you have all errors displayed by putting this at the top of your file:
<?php
display_errors(E_ALL);
ini_set('display_errors', 'on');
?>
finally, check you're php install is working correctly, by creating a new file (phpinfo.php) with the following contents:
<?php
phpinfo();
?>
open this in the browser, and check you get the about php page.

Not able to redirect to next page

I am using Win XP os and XAMPP. I was using eclipse as the editor. In Eclipes I was not able to redirect next page so now I have installed Zend Development Environment.
Now also I am getting the same problem.
My Code is
HomePage.php
<html>
<body>
<form name="Form1" id="FormId" action="Welcome.php" method="post">
name : <input type="text" name="txtName">
Phone Number : <input type="text" name="txtPnum">
<input type="submit" name="SubmitIt" value="Submit It">
</form>
</body>
</html>
And Welcome.php is
<?php
ob_start();
session_start();
if(!($_SESSION['UName']))
{
$_SESSION['UName']=$_POST['txtName'];
}
if(!($_SESSION['Ph Num']))
{
$_SESSION['Ph Num']=$_POST['txtPnum'];
}
?>
<html>
<body>
Welcome <?php
if(isset($_SESSION['UName']))
{
echo $_SESSION['UName'];
}
else
{
echo "Session not set<br/>";
echo "{$_SESSION['UName']}";
echo "The session contains <br>";
print_r($_SESSION);
}
?>
</body>
</html>
Its working fine (redirecting to next page) in the Browser but its not working in the debug mode. Both in Eclipse and Zend Development Environment.
Instead of show the content of the next page, it showing the page name.(Welcome.php in my example).
Should I need to install any other extra softwares or code itself worng.... Whats the problem. Please suggest me.
Thanks in advance....!
which part is supposed to make a redirection, i don't see any header('Location: redirect.php') or something
and why do you use ob_start() here .
you didnt release the output buffer add ob_get_clean(); in the end
<?php
ob_start();
session_start();
if(!($_SESSION['UName']))
{
$_SESSION['UName']=$_POST['txtName'];
}
if(!($_SESSION['Ph Num']))
{
$_SESSION['Ph Num']=$_POST['txtPnum'];
}
ob_end_flush();
?>
<html>
<body>
Welcome <?php
if(isset($_SESSION['UName']))
{
echo $_SESSION['UName'];
}
else
{
echo "Session not set<br/>";
echo "{$_SESSION['UName']}";
echo "The session contains <br>";
print_r($_SESSION);
}
?>
</body>
</html>
try to add this at the end of your code i am pretty sure it is because you are not releasing the output buffer, although i think it should have done it automatically
echo ob_get_clean();
Update:
I am not really sure why you are using the $_SESSION variable here, but is you want to fix the problem, you can use for example $uname instead of $_SESSION['UName'];
Welcome.php
<?php // at the beginning of your file, no spaces or newline
session_start();
$uName=$_POST['txtPnum'];
$txtPnum=$_POST['txtPnum'];
$_SESSION['UName'] = $uName;
$_SESSION['PhNum'] = $uName;
?>
<html>
<body>
Welcome <?php echo $_SESSION['UName']; ?>
</body>
</html>
you get rid of the ob start since you are still debugging your code. and try one step at a time.
Wish you good look.

Categories