How to supress the warning message in the php - php

I am pretty new to php world. I wrote the following:
<html>
<head>
<title>It joins simple1 and prac1 program together</title>
</head>
<body>
<?php
if($_POST['user'])
{
print "hello,";
print $_POST['user'];
}
else{
print <<<_HTML_
<form method="post" action="$_server[PHP_SELF]">
Your name:<input type="text" name="user">
</br>
<input type="submit" value="hello">
</form>
_HTML_;
}
?>
</body>
</html> ---- line 23
Getting Error message:
Parse error: syntax error, unexpected $end in C:\wamp\www\php_practice\simple2.php on line 23
I have removed all html tags and just kept php tags it worked:
<?php
// Print a greeting if the form was submitted
if ($_POST['user']) {
print "Hello, ";
// Print what was submitted in the form parameter called 'user'
print $_POST['user'];
print "!";
} else {
// Otherwise, print the form
print <<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Name: <input type="text" name="user">
<br/>
<input type="submit" value="Say Hello">
</form>
_HTML_;
}
?>
Output : Giving proper output but with an warning
Notice: Undefined index: user in C:\wamp\www\php_practice\test.php on line 3
Why it is not working with the previous case? What is going wrong?
How to remove or silent the warning message in the second code. It looks bad in the browser.

The cause of your parse error:
The closing of a HEREDOC statement must occur at the beginning of a line with no whitespace before or after. You have your _HTML indented to the same level as the rest of your code, but it must occur at the very first character position of the line.
_HTML_;
// Should be
_HTML_;
The cause of your undefined index warning:
To test if $_POST['user'] is set, use isset(). That will take care of your undefined index error.
if(isset($_POST['user']))
Update: The cause of the undefined variable _server notice:
Inside a HEREDOC or double quoted string, you will need to wrap complex variables (arrays, objects) in {}. Also, place quotes around PHP_SELF.
<form method="post" action="{$_SERVER['PHP_SELF']}">

You can suppress errors in PHP with an # before the function name (won't work in this case) or by setting error_reporting to a diffrent value (http://www.php.net/manual/en/function.error-reporting.php).
but you should fix the source of the warning instead of suppressing it. In this case there are whitespaces before your HTML;

Related

PHP code is not being interpreted

I am writing a code that contains HTML as well as PHP commands. When I try to run the code on my browser, I get this message :
Parse error: syntax error, unexpected $end in C:\WAMP\www\urr00001\SummerProject\projectquestionnaire.php on line 21.
I don't understand why line 21 is wrong. I was under the assumption that the "echo <<<_END ... _END" construct must be used whenever multiline HTML must be output. But the browser does not like this. My code is shown below. How can I correct this situation, please?
<?php //questionnaire.php
if (isset($_POST['name'])) $name = $_POST['name'];
else $name = "(Not entered)";
echo <<<_END
<html>
<head>
<title>Questionnaire</title>
</head>
<body>
Your name is: $name<br>
<form method="post" action="questionnaire.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
It's confirmed, you do have spaces before _END; (remove them)
Use this: (exactly as posted)
<?php //questionnaire.php
if (isset($_POST['name'])) $name = $_POST['name'];
else $name = "(Not entered)";
echo <<<_END
<html>
<head>
<title>Questionnaire</title>
</head>
<body>
Your name is: $name<br>
<form method="post" action="questionnaire.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
Consult heredoc syntax
Warning
It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.
If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.
Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.
Allowed: (no spaces)
</html>
_END;
?>
Not allowed: (1 or more spaces)
</html>
_END;
^--// that's a space, and you have many.
?>
This is just an opinion, but I would much rather see this in the code:
**<?php //questionnaire.php
if (isset($_POST['name'])) $name = $_POST['name'];
else $name = "(Not entered)";
?>**
<html>
<head>
<title>Questionnaire</title>
</head>
<body>
Your name is: **<?php echo $name;?>**<br>
<form method="post" action="questionnaire.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
Because it lets others know (especially those who know HTML but not PHP) exactly where PHP is used and where the file is only HTML and let's them see it at a glance. Your file would still be named .php, by the way. Of course, if your project is requiring "echo <<<_END..._END;" then you have to use that.

Why am I getting this PHP error?

So here's my full code
<!DOCTYPE html>
<html>
<body>
<h1>Encrypt</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter word to encrypt<input type="text" name="in">
<input type="submit">
<hr>
</form>
<h1>Decrypt</h1>
<form>
Enter word to decrypt<input type="text" name="out">
<input type="submit">
<hr>
</form>
</body>
</html>
<?php
$encrypt = $_POST['in'];
?>
And here's the error I get
Notice: Undefined index: in in /Users/idrisk/Colourity/si/index.php on line 20
Line 20 is $encrypt = $_POST['in']; and I don't see what I'm doing wrong with it. Any ideas?
As a general practice for forms in php, always check if the submit button has been clicked.
First name your submit button:
<input type="submit" name="submit">
then further in your php:
if (isset($_POST['submit'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
EDIT #1: Added to that, you seem to have 2 forms and 2 submit buttons. I suggest you keep only one form, and one submit button (remove the 2nd form element and submit button).
If you really need 2 forms, name your submit buttons differently and then you can call them separately.
<input type="submit" name="submit-in">
<!-- ... -->
<input type="submit" name="submit-out">
<?php // ...
if (isset($_POST['submit-in'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
if (isset($_POST['submit-out'])) {
// do your stuff, eg...
$dencrypt = $_POST['out'];
}
EDIT #2: If you want to echo stuff posted in your form, make sure you do the form submission checking and variable setting before the form and then echo the variable after the form (or wherever you want).
you need to first check if the form has been sent, if it hasn't then $_POST['in'] does not yet exist thus throwing the error
May be nothing but you called a php script after closing the form /form, the body /body and then then the HTML /html
replace this code $encrypt = $_POST['in']; by this $encrypt = #$_POST['in'];
this is an error on client server when you upload this file on remote server you will not saw this. use # sign on the client server when you saw this error in future.

php form never shows anything whenever it's submitted

<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>

PHP $_POST error

I'm getting the following error with $_POST['str']:
Notice: Undefined index: str in C:\Program Files\EasyPHP-5.3.8.0\www\strrev.php on line 12
I spent too much time to find an answer for this but no luck!
Please take a look at the code and let me know what's wrong with it?
<html>
<head>
<title></title>
</head>
<body>
<?php
if (trim($_POST['str'])) {
$str = $_POST['str'];
$len = strlen($str);
for($i=($len-1); $i>=0;$i--) {
echo $str[$i];
}
} else {
?>
<form method="post" action="">
<input type="text" name="str" />
<input type="button" name="submit" value="Reverse" />
</form>
<?php
}
?>
</body>
</html>
It shows the text field and Reverse button beside the error.
And also when I push the button nothing will happens.
Change if (trim($_POST['str'])) to if (!empty($_POST['str']))
Your if statement is trying to trim an array index that does not exist, hence the error. You must check if your index exists first.
In case anyone is wondering - I used empty instead of isset as the OP, by using trim, implied (or at least, I inferred from this) that a properly set $_POST['str'] containing an empty string was unacceptable. This won't fix the case where $_POST['str'] contains a bunch of spaces, however.
You're not checking if a POST has actually occured:
<html>
...
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST yada yada yada) {
...
}
}
?>
<form action="" method="POST">
...
</form>
...
</html>
$str is not necessarily an array. In order to loop through it, it needs to be an array.
It's just copying that value from $_POST['str'], so when you do the post either
Make sure it's an array with
if (is_array($str))

"Undefined index" notice after submitting HTML form

So I have a sign up form, and there is some PHP at the top of the code. It gives me this error:
Notice: Undefined index: register in /home/content/04/7195304/html/index.php on line 20
This is line 20:
if ($_POST['register']) {
Here is the submit button:
<input type="submit" class="gobutton" value="Register" name="register"/>
Edit
So here's my form tags:
<form action="index.php" method="POST">
You should check it like this:
if ( isset($_POST['register']) ) {}
to avoid getting notice.
Is your form using method="GET" instead of method="POST"?
You can also add addition checks to make sure the index exists, like this:
if (isset($_POST['register'])) {
// do stuff
}
You can also debug the form submission like this:
var_dump($_POST);

Categories