On the page "index.php", I have the following html:
<form class="centerd" method="post" action="upload.php" enctype="multipart/form-data">
<textarea id="html" type="text" name="html-text"></textarea>
<input type="submit" value="upload"/>
</form>
On the page "upload.php", I have the following PHP code:
<?php
file_put_contents("/files/test.html", htmlspecialchars($_POST['html-text']));
?>
<h1>From the textbox:</h1>
<? echo htmlspecialchars($_POST['html-text']); ?>
I want the text from the textbox to show up after the <h1> tag, and I want /files/test.html to be created (/files/ already exists), and the text from the textbox to be put into the test.html file.
What actually happens, is whatever is in the textbox shows up after the <h1> tag, but /files/test.html is never created.
It looks like "/files/test.html" is an absolute path. Have you tried "files/test.html"?
I'm sorry that I cannot comment on what Brannon said, I simply don't have reputation for it yet. But to the point:
There are two possibilities:
As Brannon said -> your path seems to be wrong.
You don't have permission to write (create) files in your directory. This can, in my experience, be a common error for Mac users who haven't provided permission in their htdocs folder.
you forgot the php!
not: <? echo ?>
but: <?php echo ?>
Related
I'm using wamp server for testing my php scripts. I just got an error on one of my recent projects so I created a basic example and found out that it isn't working. So, in my www folder, I have 2 files:
MyApp.html
MyApp.php
MyApp.html body:
<div id="tab">
<form action="MyApp.php" method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" name="submit1">
<br>
</form>
</div>
MyApp.php:
if ($_POST['submit1']) {
$test = 'ThisIsMyApp';
print($test);}
So what I want to do here is to press submit button, and it should print "ThisIsMyApp" from php file. I open .html file on my localhost, and after I click submit button, I get this error:
Not Found
The requested URL /index.php was not found on this server.
How do I fix that?
1st of all make sure both of them are in the same directory.
and do your MyApp.php has the php tags?
<?php ---your code--- ?>
I'm trying to set the value in my input text using the get method. However, when I try to render the page, it keep showing my code in the text field instead. Can someone explain what I did wrong?
<form name="quotepage" action="search.php" method="get">
<b>Stock Symbol:</b>
<input type="text" size="8" name="sb" value="<?php echo $_GET["sb"]; ?>" />
<input type="submit" value="Quote" onClick="quotepage.action='search.php';"/>
</form>
When I try to render the page, it will show my code in the value tag in my text field.
If you are seeing an error/warning/notice in your text box, try changing:
<?php echo $_GET["sb"]; ?>
into:
<?php echo isset($_GET["sb"])?$_GET["sb"]:""; ?>
to avoid showing the content if there was no content yet.
And better yet, change it to:
<?php echo isset($_GET["sb"])?htmlspecialchars($_GET["sb"]):""; ?>
to also escape nasty characters such as " that otherwise will break your HTML.
If you are actually seeing <?php echo $_GET["sb"]; ?> inside your text box, then you are having problems running PHP. Check that your script file name ends with .php and check PHP is working on your system.
Do you have a LAMP stack or similar set up?
You need Apache running with PHP installed at the least for this. Also what is this 'onClick="quotepage.action='search.php';"' attribute for?
I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document
i am new to php. i have designed a form in html and its php part in different file.now i want to connect the file with each other. i have tried using
<form action="file.php" method="post">
but connecting in this way is not secured and i cannot connect using
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
as this will work only if the php codes and html codes are written in same file. please help me.
if i use
and if someone enters this url as
http://www.variable.com/file.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E
then it will show alert box telling it is hacked.
As the Method is 'POST', it's Secure. No need to worry.
Or else..
Create a 'Session' on submiting the form. So no direct access to the php file would occur.
Thanks.
bhumin.vadalia#gmail.com
I can't understand you well, but connecting it this way is secure. If you have this form in html file:
<form action="file.php" method="post">
<input type="password" name="password">
</form>
And you have file called file.php in the same directory it is perfectly fine. In the PHP file you should have:
<?php
$password = $_POST["password"];
To get your input. Ask me something else if you don't understand it :)
As you wrote in your comments, your field shows raw text without "escaping" HTML characters.
In your file.php put or edit with:
echo htmlspecialchars($_POST['yourfield']);
Then output will not be "hacked" by JavaScript.
i am wrinting this code below in notepad and i saved in .html extent ,
i want to design a simple comment system with a database (which is notepad ) to save the comments , but when i click on the submit button it apears there is error in an specific command which is $_SERVER['PHP_SELF'] . can you help me ?
<html>
<body>
<div class ="forumform">
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
name: <input type=text name="name" id="name">
comment: <input type=text name="commentcontent" id="commentcontent">
<button type="submit" value="submit" name="submit" class="btn">submit</button>
</form>
</body>
</html>
Did you say: "i am wrinting this code below in notepad and i saved in .html extent"?
PHP commands/scripts like the <?=$_SERVER['PHP_SELF']?>, only run in files with.php ententions only!! not on HTML.
YOU should change the extension of your file, with the above script to .php
you are using the php echo shortcut make sure it is enabled in your php.ini file serach for this short_open_tag and enable it or simply use <?php echo .... ?> instead of <?= ... ?>