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.
Related
I would like to replace \n with a new line.
This is the result from database:
<!DOCTYPE html>\n<html>\n<body>\n<div>\n<label class=\"s20 c03\">Label</label>\n</div>\n</body>\n</html>
I want the string to be aesthetically pleasing, for example:
<!DOCTYPE html>
<html>
<body>
<div>
<label class="s20 c03">Label</label>
</div>
</body>
</html>
It sounds like the \n are burnt into the string. To replace them with actual newline characters, use this:
$replaced = preg_replace('/\\\\n/', '\n', $yourstring);
You should definitely find the source of the double escaping first.
To undo your very exact data issue, there is stripcslashes() however.
Replaces a literal \n with a newline.
And the \" into a plain double quote.
It also undoes \r and octal \000 or hex \x00 escapes, or (it's most likely that your database also contains these) the double backslash \\ back into a singular one.
#ahmadalbayati is on the right track I think.
Pretty Markup
If you just want the database-returned string to look pretty in the returned page's markup (i.e. via View Page Source in a browser), simply echo it where you want it in the markup...
<?php
$dbResponse = "<!DOCTYPE html>\n<html>\n<body>\n<div>\n<label class=\"s20 c03\">Label</label>\n</div>\n</body>\n</html>";
echo $dbResponse;
?>
...which yields the following markup:
<!DOCTYPE html>
<html>
<body>
<div>
<label class="s20 c03">Label</label>
</div>
</body>
</html>
Pretty Rendered Page (and Markup)
If you want to show the database-returned string when the returned page is rendered, you can use the str_replace and htmlspecialchars functions in conjunction as follows...
<?php
$dbResponse = "<!DOCTYPE html>\n<html>\n<body>\n<div>\n<label class=\"s20 c03\">Label</label>\n</div>\n</body>\n</html>";
echo str_replace("\n", "<br />\n", htmlspecialchars($dbResponse));
?>
..., which yields the following displayed page...
<!DOCTYPE html>
<html>
<body>
<div>
<label class="s20 c03">Label</label>
</div>
</body>
</html>
...and the following markup behind it:
<!DOCTYPE html><br />
<html><br />
<body><br />
<div><br />
<label class="s20 c03">Label</label><br />
</div><br />
</body><br />
</html>
I am writing a PHP code in Adobe Dreamweaver. My code is as shown below. I am expecting my code to output two boxes, into which I write something. And when I click on the submit button, I expect to see the two separate things that I entered into the box to be concatenated. But I'm not getting a concatenation. In fact, NOTHING happens when I click on submit. I am confused. Is this code not something I should be using Dreamweaver for? I am relatively new to PHP and I do not understand all the bits. My suspicion is that Dreamweaver does not recognize "twoFieldForm.php". Can someone please tell me how I can overcome this issue?
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET[fieldOne];
$second = $_GET[fieldTwo];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\";
?>
<form action = "twoFieldForm.php" method = "get">
Field 1 : <input type = "text" name = "fieldOne"/>
Field 2 : <input type = "text" name = "fieldTwo"/>
<input type = "submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\"";
?>
<form action="" method="GET">
Field 1 : <input type="text" name="fieldOne"/>
Field 2 : <input type="text" name="fieldTwo"/>
<input type="submit">
</form>
</body>
</html>
Explanation: You have to add quotes in $_GET['']. Remember indention please, always when I see people using adobe dreamweaver, their code is horrible... If you are refering to the same file, you don't have to use an action="somewhat.php". Just leave it empty. Aswell you have missed a " after your echo statement.
This will work now. Please start using a good IDE then you won't have those basic mistakes because the IDE will show you your mistakes already...
Try add quotes:
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer=$_GET['fieldOne'].$_GET['fieldTwo'];
echo "concatenated string is:".$answer;
try this...
I have a simple form and I'm trying to pass the form variable to php and output the value. I have tried to solve this myself with the almighty google but wasn't successful. The html form code is as follows
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post" action="test1.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
Then the php to handle the form is:
<html>
<head>
<title>Form</title>
</head>
<body>
<?php
echo "<h1>Hello " . $_POST["username"] . "</h1>";
?>
</body>
</html>
The output I'm getting no matter what I type into the html form is , Hello " . $_POST["username"] . ""; ?>
I don't know why it also outputs the ending semi colon and ending php tag also, maybe this is evidence of what's going wrong here?
PHP is
misconfigured or
not configured or
not working or
not working within HTML files.
You have to configure your webserver that it parses PHP inside HTML files, see here for example:
=> Server not parsing .html as PHP
The syntax is fine, try to write a sample code snippet like below
<?
$a = "hello word!";
echo $a;
?>
and check if php is working.
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))
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;