PHP related files only work when launched from dreamweaver - php

I am starting to learn php, I made a form that uses a php file to write the information into an xml file. The form is in an html file, and the php action in a seperate file. I am using EAsyphp 12.1
Whenever I use the upload form publishing it through dreamweaver everything goes fine.
But when I open it directly into the broswer it shows me part of the php code.
This is the code for html
<form enctype="multipart/form-data" action="chng.php" method="POST">
<div>Name <input type='text' name="tf" /> </div>
<div>image <input name="uploadedfile1" type="file" /> </div><br />
<input type="submit" />
</form>
The php is
$target_path4 = "../img/";
$target_path4 = $target_path4 . basename( $_FILES['uploadedfile4']['name']);
$name4="../img/linkimagen.jpg";
$t = $_POST['tf'];
if(move_uploaded_file($_FILES['uploadedfile4']['tmp_name'], $name4)) {
$xml = simplexml_load_file('info.xml');
$xml->names[0]=$t;
file_put_contents('info.xml', $xml->asXML());
$xml->images[0]=$shortname;
file_put_contents('info.xml', $xml->asXML());
}
?>
The xml information is then picked up by another page using Jquery ajax.
Everything works fine if I launch the form file using dreamweaver as I said before. Also, sometimes the page that displays the information from the xml will display it if opened directly from the browser, other times it won't, however it will always display if opened from dreamweaver. I've found information for php not working in dreamweaver but not the opposite, as is my case. Is there anything I have to do so it works always? Thanks for any info!

Well if you upload it to your server - there is a php parser which parses your file
if you open it local it won't get parsed so it's just another text document.
Of course you could install something like xampp, to have a local webserver but if you are just starting out - it's a bit much I guess :)
-- Edit --
An addition: Please watch the behaviour of your browser. If you have already a webserver installed on your computer it has an adress which usually is something like h ttp://localhost/ or h ttp://127.0.0.1 - if your file doesn't get parsed it's probably because it opens directly like C:\programm files\dreamweaver\whatever\index.php and not
h ttp://localhost/index.php
hope this helps you ;).

Related

PHP File not printing HTML form content

I am taking an HTML5 Tutorial on YouTube. Part of the course discussing CSS3 and PHP.
In the PHP section, the instructor provides the following code to print the form input to the screen:
<?php
$usersName = $_GET['name'];
$usersPassword = $_GET['password'];
echo 'Hello ' . $usersName . '<br>';
echo 'That is a great password ' . $usersPassword . '<br>';
?>
The HTML it refers to is:
<form method="GET" action="phpscript.php">
Your Name: <input type="type" name="name" size="50" maxlength="50"><br>
Your Password: <input type="password" name="password"><br>
Your history:<br>
<textarea name="history" rows="20" cols="50">Just random words</textarea>
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Reset">
</form>
The PHP variables do not print to the page. This is the output:
echo 'That is a great password ' . $usersPassword . '<br>';
I double checked what the instructor had in the video, and it is exactly the same. Then, I opened the Console in Firefox and I saw an error on the PHP page that said that the doctype and character encoding was not declared. In the instructions, the instructor has a php file with only php code in it. So, I got rid of those two errors by using my html file as a template and adding the php code between my <main></main> tags, but got the same output. So, I added <script type="text/php"></script> around the php code, then nothing printed.
Please help me so I can continue moving forward in this tutorial. The instructor was using a Mac and Chrome and running the files locally. I am running everything locally in Windows 7 and Firefox. I tested things out using IE and Chrome but got the same results.
Thank you!!
Sounds to me like either you do not have php running anywhere (Since it is server side you need a server to run it and I recommend XAMPP for this use) or your file has the ending .html but you would need a .php to run php code. Just putting it in between the <main> tag won't work.
You want to output the script without running it?
Try <xmp></xmp>
For example, if you want to print a link like this, it will look like this:
Google
Google
but with the <xmp> tag, it looks like this:
Google
For just startup you don't need a fully fledged web server , you can use PHP's inbuilt server simply go in php's installation directory save your php then bring a command prompt on this directory and run
php -S localhost:80
Now access file using http://localhost/file.php
You need to keep the shell open as long as you are using php

PHP contact form returning the PHP code when I press the submit button

I created a contact form for my website. When I click the submit button, it just shows the PHP code instead of the message it is supposed to show, like the text information the user inputs into the form.
Here is the HTML code for the form:
<html>
<link href="contact.css" rel="stylesheet" type="text/css" />
<head>
<title>Contact Us</title>
</head>
<body>
<form action="contact.php" method="post">
<div id="ContactForm">
<fieldset>
<h4>Contact Parquest:</h4>
<label class="labelone" for="name">Name:</label>
<input name="name"/>
<label for="email">Email: </label>
<input name="email"email"/>
<label for="commens">Comments: </label>
<textarea name="commens"></textarea>
</fieldset>
<fieldset>
<input class="btn" type="submit" value="Send Email"/>
<input class="btn" type="reset" value="Reset Form"/>
</fieldset>
</form>
</div>
</html>
I couldn't post the PHP code, because it just doesn't show when I try, so here's a screenshot:
There is no problem to your code.. The problem is on your environment.. I guess you are not running the html file through a server..
If the url on your browser looks like these:
file:///c:/path/to/your/file/page.html
then you are doing it wrong.. in order to run .php scripts, you need a web server like apache or nginx... the url of should be like these
http://localhost/path/to/file/page.html
then the php file should run as expected..
php files are interpreted scripting language and thus it needs an interpreter in the server in order to run.. if it is just browsed in the browser without a server, it will just output the code inside..
You need a web server to run php scripts.
Try installing wamp and then run your page under localhost.
I have executed the given code. It's working fine on my local server. Conventionally it should print the value inputs posted by form on contact.php.
But the thing I noticed in your PHP code is the variable name is $commets, instead of $comments.
I faced the same problem and when I tried starting my own server it worked.
when you are using Windows, open cmd and type in this;
php -S localhost:4040;
you will see something like this
PHP 8.1.1 Development Server (http://localhost:4040) started
meaning you have started your own server, then you can copy the link found in the parenthesis
so you copy say this "http://localhost:4040" and paste it in your browser then you can now direct it to your html document like this
http://localhost:4040/Desktop/Projects/index.html
now your form should work well with your php script when you submit
i had the same issue, even if appache & mysql were running on my computer.
in the html file, i wrote :
form action="http://localhost/file.php" method="POST"
instead of :
form action="file.php" method="POST"
It seems like connecting HTML forms to php files only works when you download wampserver and turn on all services...
Here's how it worked for me:
You can download wampserver from here: http://www.wampserver.com/en/ (follow instructions)
I saved it to the C: drive on my computer.
Next, you can create a php file and an html file both in the 'www' folder of the wamp folder in whatever drive you put it in. Copy the form code into the HTML file, and the php code into the php file.
Finally, go to 'localhost/yourhtmlname.html' and fill out the form. It should work as normal.

HTML form opens PHP script instead of running it

I am using WAMP with MySQL and PHP to run a local server. The WAMP server is online and I use a simple HTML script to run PHP file that connects to a database and inserts data in a table that I have created on WAMP server.
The problem is that instead of running (executing) my script, the browser opens it in text mode. As in I can see the script.
Now there have been similar questions on this forum and others and people have solved the problem. However what makes my problem unique is that I am able to run a test.php. I am able to display text with it as well as open info.php so PHP is running on my server, however when I use the HTML form it refuses to run and only opens the script.
I am using Chrome browser and I have also checked it in Firefox with the same result.
I have one HTML file that is a form linked to my PHP files which handles connection and insertion of values in the table on the WAMP server. The name of the database is test.
form.html
<form action= "create_product.php" method= "get">
<center>
<table>
<td><label><b><font size="5"> Name</label></td>
<td><font size="5"><input type="text" name="name" /></td>
</tr>
<tr>
<td><label><b><font size="5"> Description</label></td>
<td><font size="5"><input type="text" Description="description" /></td>
</tr>
<tr>
<center><td></td></center>
<center><td><input type="submit" value="Submit"/> <input type="reset" value="reset"/>
</tr>
</table>
</center>
insert_product.php
<?php
$con= mysql_connect("localhost", "admin","");
$db = mysql_select_db("test");
$name = $_GET["name"];
$description = $_GET["description"];
$query = "Insert into products('name','description')values('".$name."','".$description."')";
$result = mysql_query($query);
?>
I had the same problem and was surfing all known technical forums. I finally figured out what was the problem - nothing to do with PHP or Apache setup. Nothing wrong with the code (at least on mine). Here's what was NOT working:
I double-clicked to open the file from Finder (or explorer if you are a Windows user), and the URL in browser is
"/file/xxxxx/foo.html"
. This would open
/file/xxxx/bar.php"
, which doesn't work!
However, if I typed into the URL in browser
"localhost/~xxx/foo.html"
, it runs nicely when
"bar.php"
is called.
Perhaps this is what you can try.
"file:///XXXX/foo.php"
doesn't work, but
"localhost/XXX/foo.php"
does.
Make sure that you open it localhost/yourfile.php not like C:\wamp\yourfile.php
Start all services
or try xampp download
tried wamp and xampp , in my opinion xampp is better
This is edit- If u have tested that PHP is working on your local server,then the most probable cause for text display could be missing php end tag(?>)
Also please note that if u have saved your file as anything other than .php,it will not be parsed. Files containing PHP code but saved as HTML or doc file renders the php codes within it useless.
<<--EDIT ENDS-->>
u gave action= "create_product.php" in form although u specified the php file as insert_product.php
Also as a good practice,always provide database connection variable in mysql_select_db
use $db = mysql_select_db("test",$con);
instead of $db = mysql_select_db("test");

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.

Display Image to Browser Directly from PC source using PHP

How to get file source path from users PC when they submit their file on form like below.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="Submit">
</form>
Example path that I want is file:///C:/Users/hafizul/Downloads/myimage.png
So, I can display user image using the code below:
<?php
$path = 'file:///C:/Users/hafizul/Downloads/myimage.png';
header('Content-Type:image/png');
echo file_get_contents($path,FILE_USE_INCLUDE_PATH);
?>
Thanks!
Since PHP is a server-based language, you cannot pass him parameters of a Path on the client computer: the code is executed on the server without access or knowledge of the client and its hard drive.
A better option in this case is to use JavaScript or Flash on your page to display the image, the user can review it before submitting it and actually upload it on the server.
Check here for instance for the JavaScript version:
http://teck.in/javascript-to-preview-local-image.html
Note that your code might be blocked by security measures on the site since the access of a script to the local computer filesystem might be harmful.
You will not know where the file came from on their computer reliably, but you can send an uploaded file back to the browser as you seemed to be attempting:
header("Content-type: image/png");
readfile($_FILES['file']['tmp_name']);
Refer to the PHP manual chapter "Handling file uploads":
http://php.net/manual/en/features.file-upload.php

Categories