HTML form opens PHP script instead of running it - php

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");

Related

my php file doesnt output but instead shows source file

I am trying to add a php code in my html file, but whenever i do, my file instead of outputting my expected outcome, it instead presents the source code
again, i am creating a server for my project at school and i am a total noob in programming as i learn it all by myself without formal education about it. i tried to save the file as both .html and .php, although .html presents the output, but not the desired one. now i have two files below and since i am a noob in programming, my concept was to have users input the form in the .html file and output in on an identical one but in .php
this is the first file, saved in ABM11.html
<form action="AMB11.php" method="post"><table border="0">
<tr>
<td>Name</td>
<td align="center"><input type="text" name="StudentName" size="30"></td>
</tr>
<tr>
<td>Subject</td>
<td align="center"><input type="text" name="Subject" size="30"></td>
</tr>
<tr>
<td>Final Grade</td>
<td align="center"><input type="text" name="FinalGrade" size="30"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit"></td>
</tr>
</table>
</form>
this is the second file, saved as ABM11.php
<?php
$studentname = $_POST['StudentName'];
$subject = $_POST['Subject'];
$finalgrade = $_POST['FinalGrade'];
echo $studentname '<\br>';
echo $subject '<\br>';
echo $finalgrade '<\br>';
?>
basically i just want users to answer a form then have that data be posted on the same page perpetually, not have my noob source code presented
it seems , the problem with your web server , weather you are using apache or nginx . its not running properly or its not configured properly on your local machine so rather then executing php code its just showing that file on the browser .
You can debug like , create one simple php file just like echo "yourname "; and put it to your www directory and try to run it form browser if the server is proper configured then it must print and if its not prited then its mistake with your local server configuration .
and also on your source code there is one mistake of echo and .
Wrong : echo $finalgrade '<\br>';
True : echo $finalgrade .'<\br>';
You must concat br with .
that is mistake 110% but as you said in your question the code is showing on the page is not only because of this , its surely configuration problem
if . is the only mistake then it wont show you the source code on the page but you will get any php error if thats the only issue .so check your config well first.
Thanks
Akshay Champavat
If you combine variables and strings in an echo, you need to add dots to connect them. So instead of this
echo $studentname '<br>';
you need to write it as
echo $studentname.'<br>';
And similar in all other lines.
(and no backslashes, by the way!)
And also ("sourcecode displayed...") make sure to open/call the php file via a server (for example XAMPP, via localhost), not simply as a file on your computer.

PHP variables not showing up after submitting form (closed)

I'm just starting to learn form and PHP. I am testing a simple HTML file from W3Schools here with the following code:
<html>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
It is supposed to pass the information to a PHP file called welcome.php, which looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?><br>
Random thing: <?php $rand = "bananas"; echo "$rand"; ?>
</body>
</html>
When I run the HTML file on Chrome, fill in the name and the email and press submit, the page looks like this:
Welcome
Your email address is:
Random thing:
While the HTML part works, the name variable, email variable and rand variable isn't printed.
[EDIT]: I solved it by transferring the files to a server and running it by going onto the actual webpage and it worked. Also Azeez Kallayi suggested running it on Xamp.
I think you are not using any server, just opening in broswer without any server. Also correct semicolon as in the above comment.
Since PHP is a server side programming language , you need a server to execute the PHP scripts.
There are many applications available that you can use as local servers and run your application. Some of them are below.
Wamp, Xamp, Lamp
Hope this will help you
Change this
echo "$rand";
To this
echo $rand;
Firstly you should get knowledge what PHP is and how you can use it.
PHP is a server-side scripting language. So if you try to run it like a html file you will not see the expected output. You need to understand what is a server side server-side scripting is.
If you have have jumped into coding is very essential you should know how you should debug to resolve your error.
One easy way is to enable error reporting.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
Hope this will give you a start .

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 related files only work when launched from dreamweaver

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 ;).

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.

Categories