I'm trying to learn PHP using NetBeans although I've come up against a problem with the interpreter and I can't tell how to fix it.
It's to do with the notation <<<_END. It should, from what I'm learning wrap everything into a variable until it's ended with _END
However, if I plug in the following example:
<?php
echo <<<_END
<html><head><title>PHP form upload</title></head><body><form method='post' action='upload.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload'/>
</form>
_END
if ($_FILES)
{
$name = $_FILES ['filename']['name'];
move_uploaded_file($_FILES ['filename'][tmp_none], $name);
echo "Uploaded image '$name' <br/> <img src='$name'/>";
}
echo "</body></html>";
?>
I get the following error message
Parse error: syntax error, unexpected T_SL in script.php on line 13, where line 13 is the code that says 'echo <<<_END'.
Can anyone help me, please?
There must be no space/tab/indentation before ending _END like this:
echo <<<_END
<html><head><title>PHP form upload</title></head><body><form method='post' action='upload.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload'/>
</form>
_END;
Don't forget that it is not allowed to
indent the closing tag if you do so
you will get a parsing error.
http://www.phpf1.com/tutorial/php-heredoc-syntax.html
Missing semicolon after _END
You can't have any indentation before _END
Related
I'm trying to encode a simple code but I'm getting this error:
Parse error: syntax error, unexpected '<' in C:\wamp\www\test.php(2) : eval()'d code on line 1
This is the code:
<form method="POST" action="">
Enter your command: <input type='text' name='cmd'> <input type='submit' name='execute' value='Execute'>
</form>
echo $_POST['cmd'];
This is the base64_encode with eval:
<?
eval(base64_decode('PGZvcm0gbWV0aG9kPSJQT1NUIiBhY3Rpb249IiI+CkVudGVyIHlvdXIgY29tbWFuZDogPGlu cHV0IHR5cGU9J3RleHQnIG5hbWU9J2NtZCc+IDxpbnB1dCB0eXBlPSdzdWJtaXQnIG5hbWU9J2V4ZWN1dGUnIHZhbHVl PSdFeGVjdXRlJz4KPC9mb3JtPgoKCmVjaG8gJF9QT1NUWydjbWQnXTsK'));
?>
That is because you are trying to eval non-php code (html form)
You forgot the PHP tags around your code:
<?php
// Any PHP code as needed
?>
<form method="POST" action="">
Enter your command: <input type='text' name='cmd'> <input type='submit' name='execute' value='Execute'>
</form>
<?php
echo $_POST['cmd'];
?>
This question already has answers here:
How to get the file path in html <input type="file"> in PHP?
(3 answers)
Closed 9 years ago.
i m trying to update images using GET method
user_form.php
<form action='upload.php' method='get'>
<input type='file' name='user_img' />
<input type='text' name='username' />
<input type='submit' name='update' value='update'>
</form>
upload.php
if(isset($_GET['update']))
{
echo 'username: '.$_GET['username'];
echo 'file name: '.$_FILES['user_img']['tmp_name'];
}
i m getting correct value for username, however, blank value for filename.
can anyone please let me know if we can use $_FILES variable for GET method? if yes then please point out where m i going wrong in the above sample code. thank you.
You cannot upload files using a GET HTTP request. Files are sent in the HTTP body, which requires a POST or PUT request.
You need to do:
<form action="someaction.php" method="post" enctype="multipart/form-data">
Add this to your form tag:
enctype = multipart/form-data
You should send your form by post method and specify that this form will have a file
<form action='' method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type='file' name='user_img' />
<input type='text' name='username' />
<input type='submit' name='update' value='update'>
</form>
It is also preferable for reasons of security and performance to indicate the maximum size of files to be sent.
You need to add enctype = multipart/form-data in a form if you want to upload /use files.
I want to insert some programming code along with the descriptions like a tutorial site.
This is the sample code for HTML code:
$code = "
<p>This is introduction to HTML </p>
[code]
<html>
<head>
<title>This is sample descriptions</title>
</head>
<body>
<form method='post' action='index.php'>
<input type='text' name='username' value=''>
<input type='password' name='password' value=''>
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>
[/code]
<p> This is sample for PHP </p>
[code]
<?php
echo "Hi, This is PHP";
?
[/code]
";
$code = mysql_real_escape_string($code);
mysql_query("INSERT INTO tutorials SET tutorial='$code'");
To display I am retrieving the content from a database and using htmlspecialchars like,
echo htmlspecialchars($code);
For highlighting the codes, I am using google-code-prettify, that requires that the code be in between pre tag with prettyprint class,
<pre class='prettyprint'>
echo htmlspecialchars($code);
</pre>
Where ever the tags, [code] and [/code] are replaced with <pre class='prettyprint'>"; and </pre> like,
$code = str_replace("[code]", "<pre class='prettyprint'>", $code);
$code = str_replace("[/code]", "</pre>", $code);
When I echo,
echo htmlspecialchars($code);
only plain text is displayed like:
<html> <head> <title>This is sample descriptions</title> </head> <body> <form method='post' action='index.php'> <input type='text' name='username' value=''> <input type='password' name='password' value=''> <input type='submit' name='submit' value='Submit'> </form> </body> </html> </pre> <h5> 2. This is sample code for paragraph </h5> <pre class='prettyprint'> <html> <head> <title>This is sample
You're calling htmlspecialchars after doing the replacements so the <pre> tags are escaped as well (and won't be rendered as HTML). Reversing the order should do the trick:
$code = htmlspecialchars($code);
$code = str_replace("[code]", "<pre class='prettyprint'>", $code);
$code = str_replace("[/code]", "</pre>", $code);
echo $code;
Further, have a look at highlight_string for highlighting source code.
There are <pre class='prettyprint'> and </pre> statements in your code too. This may make the code not work as expected.
You may try by changing < and > in your code to < and > respectively.
I am trying to pass a certain data which I found in my table using the search to another php page . here is my code
echo "
1<form action="adm_edit.php?product_code=$record[0]" method="POST">
2<input type=submit value=Edit>
3</form>
4<form action="adm_edit.php?product_code=$record[0]" method="POST">
5<input type=submit value=Delete>
6</form>
";
my search function is working fine and record[0] is contain desired data but I am getting this error when I run this code:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in search.php on line 1
I put numbers on lines in above code for ease of reading
So could you please help me?
Thank you
Take care when using quotes from html elements in echos, also when using variables!
When using ' instead of ", you also have to put quotes in front of the variable and that way you stop echoing a string and can start with echoing a variable. You need to concatenate the var and the string with a . !
This will work :
echo '
<form action="adm_edit.php?product_code='.$record[0].'" method="POST">
<input type=submit value=Edit>
</form>
<form action="adm_edit.php?product_code='.$record[0].'" method="POST">
<input type=submit value=Delete>
</form>
';
<form action="adm_edit.php?product_code=<?php echo $record[0]; ?>" method="POST">
<input type=submit value=Edit>
</form>
<form action="adm_edit.php?product_code=<?php echo $record[0]; ?>" method="POST">
<input type=submit value=Delete>
</form>
I have the following code sample upload3.php:
<html>
<head>
<title>PHP Form Upload</title>
</head>
<body>
<form method='post' action='upload3.php' enctype='multipart/form-data'>
Select a File:
<input type='file' name='filename' size='10' />
<input type='submit' value='Upload' />
</form>
<?php
if (isset($_POST['submit']))
{
echo "isset submit";
}
else
{
echo "NOT isset submit";
}
?>
</body>
</html>
The code always returns "NOT isset submit".
Why does this happen? Because the same script upload3.php calls itself?
You do not have your submit button named:
Change
<input type='submit' value='Upload' />
To:
<input type='submit' value='Upload' name="submit"/>
Two things:
You'll want to try array_key_exists instead of isset when using arrays. PHP can have some hinky behavior when using isset on an array element.
http://www.php.net/manual/en/function.array-key-exists.php
if (array_key_exists('submit', $_POST)) { }
Second, you need a name attribute on your button ( "name='submit'" )
Because you don't have any form element whose name property is submit.
Try to use var_dump($_POST) to see the keys that are defined.
Notice that files are an exception; they're not included in $_POST; they're stored in the filesystem and they're metadata (location, name, etc) is in the $_FILES superglobal.
Try looking at the REQUEST_METHOD and see if it's POST. It's a little bit nicer.
<input type='submit' value='Upload' />
should be
<input type='submit' value='Upload' name='subname'/>
and that subname should be in $_POST[' ']
it will look like
if (isset($_POST['subname']))
{
echo "isset submit";
}
else
{
echo "NOT isset submit";
}