Can not get .html to display in a .php file - php

I've been trying to use PHP to allow a user to put in articles onto a certain site via a form.
The site,my code and my skills as a programmer are primitive in nature.
The user enters the input here:
<form action="articlepro.php" method="post">
Title:<input type="text" name="title"><br>
Sub-Heading:<input type="text" name="subhead"><br>
Intro:<textarea name="intro" rows="10" cols="40"></textarea><br>
Heading-1:<input type="text" name="head1"><br>
Content-1:<textarea name="cont1" rows="10" cols="40"></textarea><br>
<input type="submit">
</form>
The date is processed as such by the file articepro.php:
<html>
<body>
<?php
$title=$_POST["title"] . ".php";
$createFile=fopen($title,"x+");
$content=file_get_contents("articletemplate.php");
echo $createFile;
echo file_put_contents($title,$content);
echo $createFile;
?>
</body>
</html>
The aim of the code above is to first create a variable that stores the title from the form with the .php extension.Then the code creates a file with the same name as the $title variable.$content stores the html and php code of the file "articletemplate.php" as a string and "file put contents" puts that template into the file created earlier.
It may be of of value to know that artictemplate.php contains the template I wish for all new articles to have with bits of php that put in data as such:
<p><h1><?php echo $_POST["head1"];?></h1>
<?php echo $_POST["cont1"];?>
</p>
Now the problem I face is the fact that my code both creates the required files with the required title(Permisson:644),it also contains the intended template.BUT,I am unable to display anything when I open the created files(.php's) via URL.I either see a blank page or "k????????????????????????????????????????????????????????????????????????????????????????ä???????????????ä??"
I can open them if I download the created files off the server and change the extension to .html(cant use PHP then) though but no luck with .php or .phtml.
Webserver:Apache/2.2.24 (Unix

Related

How do I get the input of a text box and write it to a .txt file with PHP?

Original:
I am trying to make the PHP get the input that was put into the text box and write it to name.txt. I am also getting an error message that says "Expected tag name. Got '?' instead." on line 6.
<html>
<body>
<p>Enter name here:</p>
<input type="text" id="name"/>
<button onclick="[activate PHP]">Enter</button>
<?php
$fp = fopen('name.txt', 'w');
fwrite($fp, '[name entered]');
fclose($fp);
?>
</body>
</html>
I am not familiar with PHP, so please explain what your fixed code does when you answer.
New:
This was made by #Vlad Gincher. The problem is, the code is not creating a .txt file, which leads me to believe that the PHP is executing as soon as the page loads. Is there a way to activate the PHP when the form is submitted?
<?php
if(isset($_POST["textareaValue"])) {
$fp = fopen('name.txt', 'w');
fwrite($fp, $_POST["textareaValue"]);
fclose($fp);
}
?>
<html>
<body>
<p>Enter name here:</p>
<form method="post">
<input type="text" id="name" name="textareaValue" />
<input type="submit" value="Enter" />
</form>
</body>
</html>
Again, I am inexperienced with PHP, it could be an error of mine.
PHP runs on the server side. After the server finishes, it returns the output to the client. In that point, you can't use PHP, but you can force the client to send another HTTP request so the PHP would be activated.
Here is a code where it runs, checks if the client sent information to the server, and if so, add the information the the file. If not, it does nothing.
I'm using HTML's form element to tell the browser to send the information back to the server using post. Then, I can get the value using PHP's $_POST, and with the name of the input that I want to get the data from (textareaValue)
<?php
if(isset($_POST["textareaValue"])) {
$fp = fopen('name.txt', 'w');
fwrite($fp, $_POST["textareaValue"]);
fclose($fp);
}
?>
<html>
<body>
<p>Enter name here:</p>
<form method="post">
<input type="text" id="name" name="textareaValue" />
<input type="submit" value="Enter" />
</form>
</body>
</html>
When the web page has been rendered (displayed to user), the PHP is finished. PHP cannot interact with a user - for that you need either (a) an HTML form that is submitted to a .php file (even the same one that contains the form), or -- and this is far more popular these days -- javascript with AJAX.
Here is another answer that discusses both:
How can I make, when clicking a button, send an email with the data included from the form?
HTML forms get "submitted" to a back-end PHP file that receives the data from the form fields. Each form field has a name= attribute on the HTML tag -- that becomes the variable name, and the contents of the HTML element becomes the variable's data.
A very similar thing happens with ajax, except that it is more flexible (the HTML form system is very rigid/static), and (most importantly) the web page need not refresh.

PHP code being shown in HTML form text fields

I have this code to display PHP value in HTML form text field:
<form method="post" action="">
<input type="text" name="firstname" maxlength="30" value="<?php echo $firstname;?>"><span class="error"> <?php echo $firstnameErr;?></span>
Problem is I see <?php echo $firstname;?> being displayed in the text area instead of the actual user-input name or a blank field.
I tried using <? php echo and <?= and neither work any different, my server supports PHP.
How do I fix this?
i believe what is happening is you're opening the file by double clicking rather than "running" the file by navigating to it in your web browser.
i tested your code and it works fine. however, if i just OPEN the file rather than navigating to it via http, then the php code displays inside the text box.
remember, php (and other server side script types like jsp) have to be run, you can't just open them, or drag them into a browser window to test them.
so if, for example, you're using xampp or apache, you would go place the file inside your htdocs folder, open your web browser, and type something like localhost/plc.php
btw, here is the code as i tested it:
<?php
$firstname = "bob";
$firstnameErr = "phil";
?>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="text" name="firstname" maxlength="30" value="<?php echo $firstname;?>"><span class="error"> <?php echo $firstnameErr;?></span></form>
</body>
</html>

Creating a webpage with data sent from a form

I am trying to build a form in HTML that, once submitted, automatically generates a new webpage using data entered into the form.I'm usually a MATLAB and python user, so I tried them first. Matlab would parse the parse the data and save it to a new .txt file, but not a .html file. Python was much the same. After searching, I came across the suggestion to use PHP to create the new page from the form data. (Someone was using php to create user webpages with the users name, email, and a picture. I tried to adapt this to suit my needs, but it is not generating the new page as I thought it would. Instead, it just displays part of the PHP code. This is the form I made:
<form action="htmlData.php" method="post">
Product Name:
<input name="Name" size="20" type="text">
<br><br>
Project Lead Name:
<input name="PLname" size="20" type="text"> <br><br>
Team-members: <br>
<textarea name="Team_members" rows=10 cols=40> </textarea> <br><br>
Product Type: <br>
<input name="Product_Type" size="20" type="text"> <br><br>
Description: <br>
<textarea name="Description" rows=10 cols=40 type="text"> </textarea>
<br>
<br> <br>
<input value="Submit" type="submit" name="formSubmit">
<input value="Reset" type="reset">
<input value="Help" type="button" onclick="window.location.href='problems.html'">
</form>
...And this is the PHP file named htmlData.php:
ob_start();
$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description'];
$html = <<<HEREDOC
Product Name: $Name <br>
Project Lead: $PLname <br>
Team Members: $Team_members <br> <br>
Product Type: $Product_type <br>
Description: $Description
HEREDOC;
file_put_contents('newPage.htm', $html);
header()redirect.header('location: newPage.html')
What do I need to change so that once a user clicks submit, a new page is generated from the data and the user is then taken to the newly created page? Is this possible with what I have, or should I looking into using a different language?
It's possible with PHP and Python (perhaps MATLAB too).
Given your PHP code, there are a few small issues. First, the HEREDOC must be ended at the beginning of the line (there can't be any whitespace before the end HEREDOC).
Second, the PHP code to redirect is invalid. Try these changes:
<?php
$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description'];
$html = <<<HEREDOC
Product Name: $Name <br>
Project Lead: $PLname <br>
Team Members: $Team_members <br> <br>
Product Type: $Product_type <br>
Description: $Description
HEREDOC;
file_put_contents('newPage.htm', $html);
header('location: newPage.html');
exit;
The next issue you may encounter is that it cannot write to newPage.htm. You may need/want to specify a full path (e.g. /home/yoursite/public_html/files/newPage.htm).
You will need to make sure that directory is writable by the web server (as often the web server runs as a different user than your files are owned by).
Hope that helps.
If it shows a php code, that means that:
1. You dont have php installed,
2. Your http server like Apache installed.
Even if the 2 above conditions were met, the code you have is not a valid PHP code, and it would not work.
You are not yet good enough with PHP, you need to go back and learn the very basics before you try make any dynamic pages.
Instead, it just displays part of the PHP code
Seems like one of the following:
Have you installed PHP on your server?
Is the server configured to pass .php pages to be processed?
It might be worthwile to take a look here as displaying PHP code on the browser means the webserver isn't dealing with it correctly.
Once you think it's installed run phpinfo() see the docs.
You are missing an 'l' in your new file declaration:
file_put_contents('newPage.htm', $html);
Also, try physically creating the newPage.html file and place it in your project directory then see if the page is displayed or not. If this is the case I would check to see if you have PHP installed and if the server is configured to handle post. Usually it is configured to handle post or get but not usually both by default.
While not exactly solving your original problem, I hope I'm not too out of bounds with this:
Your problem is most often solved by using a database in conjunction with a language like PHP. Storing this information in a database after form submit will allow newPage.htm you have to accept GET parameters and dynamically display the corresponding data from the database. Example: newPage.html?id=245 will deliver that ID to the PHP code processing that request. PHP will use that ID to query your database for the proper information and display that to the page. This way you will not have one file for each form submission and the form data can easier to maintain. Check out something like: http://www.freewebmasterhelp.com/tutorials/phpmysql for more information.
Another aspect to keep in mind is security. Your current code puts direct user input into a file on your server. This is a huge security vulnerability. It is recommended you escape the user input.
If you not need to create new page, in start of your PHP code, remove ob_start(), and add
if(isset($_REQUEST['formSubmit'])){
....
....
}
your page will be created automaticly on submit press.

Creating a Directory with MkDir then appending the Folder to a Html doc?

I have an A-Z list of users, and I'd like to be able to append the new folders created with mkdir() instead of manually adding them to the .html page. Just attached an example
Eg:
UserList.html
Example user:
<font size="4" color="yellow">Barrack Obama
Mkdir Form:
<form action="Create.php" method="post">
Full Name: <input type="text" name="Username" /><br />
<input type="submit" value="Create User Folder"/>
</form>
Create.php
<?php
mkdir($_POST["UserName"], 0777);
?>
NB: Suggest making these folders in a different directory then your root directory, maybe users/obama
You could use a function called readdir() which will read the contents of the directory ready for output.
http://au.php.net/readdir
Here is a link to some information about reading dir's in php
http://www.dreamincode.net/forums/topic/13426-list-of-folders/page_view_findpost_p_134369
have another request that returns all the folders in the user directory and put them on the page. i.e create the link html in php as well. you could use ajax if you want to do this without reloading the page.

PHP Code to Insert Text Box Text Into HTML Body

I am using the code below:
<form>
<textarea cols="50" rows="4" name="link"></textarea>
<textarea cols="50" rows="4" name="notes"></textarea>
<input type="submit" value="Submit">
</form>
It creates two text boxes and I was wondering how to get them from this into a different html file's body code? I think I would need PHP.
At the end I would just want it to display a message saying "Complete" and in a html file e.g code.html in the tags it would have the contents of the two text boxes seperated by one line.
Regards!
Sounds like you would need PHP to receive the data.
See this for a basic form that posts to a PHP page.
you most use php html like this:
echo '<form id="form1" name="form1" method="post" action="">'.
'<p>PASWORD<br>'.
'<input type="text" name="name0" id="text" /><br><br>'.
'<p>BACKLINK<br>'.
'<input type="text" name="name" id="text" />'.
'<br>;'.
'<input type="submit" name="Enter" id="Enter" value="Enter" />'.
'</form>';
and use if for run html
if ($_POST['name0']>''){
your code
}
To accept input one one page and display it on another will require PHP. The general idea:
The HTML form page:
Contains you HTML form where the user enters and submits their input. The data from the form is sent to the PHP processing script. We'll assume you use the POST method.
The PHP processing script:
Contains code to read the POST values and sanitize them. This means you want to check for HTML you don't want, script tags, etc - anything you don't want. In this case, the user will be displaying this code to themselves, so they can't really harm another user. However, it's something important to keep in mind. The rule of thumb - never trust user input - always check it and clean it. Now that the PHP script has read and cleaned the data, you want to display it.
The PHP display script:
Contains the HTML of the page and PHP code to display the values you captured in the PHP processing script.
Other notes:
The PHP processing and display scripts can live in the same file. You will have to process before you display. Alternately, you can run the processing script and then include the display script. Both will work.

Categories