jQuery $.post() returns PHP code - php

I've got this jQuery function:
function phpmail(){
$.post('mail.php',{name:$("#name").val()},
function(out){
alert(out);
});
}
and this PHP script in mail.php:
<?php
$name=$_POST['name'];
echo $name;
?>
However, each time when I click a button, I get PHP code and not the variable, so can someone explain me what I'm doing wrong?
I'm testing this on my local computer using WAMP.

maybe you test it using wrong url (on the browser)..
it should start with http://localhost/
not file:///c:\wamp\bla

This is a serious issue. You should make sure that php is correctly installed on your instance, otherwise anyone could see your code. Also make sure that your web server is not treating your cgi files as plain-text/html

This has nothing to do with either your PHP or JavaScript code. The problem is that your web server is misconfigured. It isn't recognizing ".php" as PHP code and therefore not parsing it.
You need to tell your web server that files with a ".php" ending are to be handled by PHP. You don't mention which web server you're working with so it's difficult to give you precise instructions for that but for what it's worth, here's the relevant portion of my Apache configuration.
<IfModule mod_php5.c>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
# ...bunch of irrelevant stuff here
</IfModule>
Of course, in order for PHP to work at all, the module must be loaded like so:
LoadModule php5_module /usr/lib/apache2/modules/libphp5.so
However, if you're running Linux, it is strange if you have to configure this yourself. But then again I don't know what you're running.
Web servers differ quite a bit from each other, so it's difficult to give you good instruction without more information on your runtime environment. Are you running Apache or IIS or Lighttp etc.? - Are you running on Windows or Linux or Mac? If Linux, which distribution, Debian or Fedora Core or Ubuntu?

Do you config you Web Server(Such as Apache or nginx) correctly? Do you handle the php file as the plain text??

Related

LAMP server php files show the code instead of running it

I had a lamp server installed and the server stopped working. I have fixed that problem and the server works now, however when running a php file it just displays the code instead of runnning it. I think this problem may have to do with the fact that i have reinstalled apache2 to try to get my server to work. Im guessing it had messed up some the configuration between apache and php however i have no idea how to fix it.
I also have mysql installed which i cannot test on the server until php is working again.
you probably have not configured apache to use php, this is from my conf file
#PHP Include
Include conf/extra/php5_module.conf
and
LoadModule php5_module modules/libphp5.so
apache service would require a restart after any edits
Content-Type might be set to text/plain. Change it to text/html instead.

Why my php code doesnt work?

php is not working in my computer. I have installed Apache server and save the file with .php extension in htdocs but still its not working.
I have written these code:
<?php
echo "Hello World!";
?>
it shows nothing. I have opened .htaccess file and got,
# This folder does not require access over HTTP
# (the following directive denies access by default)
Order allow,deny
what can I do now.please help me.
Open the Apache server log and take a good look at it. That's what it's there for. It logs everything. Is in /logs in your Apache programs directory.
You either don't have php installed into Apache correctly (likely), don't have Apache set up correctly, or don't have PHP set up correctly.
You'll want to look at conf/httpd.conf for Apache configuration, and php.ini for php configuration.
Sort out Apache first. Try to serve a simple .htm or .html page. i.e. put a simple text file with .htm file extension, e.g. myfile.htm, in your DocumentRoot (see conf/httpd.conf) and see if you can browse to it.
You might want to set 'ServerName localhost', and then browse to localhost/myfile.htm
From the Apache log you will also be able to tell if php is loading correctly. If not comment out the list of extensions, e.g. extension=php_gd2.dll, and try it again. I've had it not load because some of the libraries were not present. If you get it to work with all commented out, then one way or another figure out which ones are causing the trouble and leave them out.
Important: You must shutdown Apache and restart it after any configuration changes. (Not re-boot, but rather the Apache server monitor widget.)
Also did you install 32 bit versions of both Apache and php? If you try to mix x64 with x32 it won't work. And as I recall there are specific recommendations about which versions to use with which at the php download site. Pay attention to the details there.
I recomend you to add the php module to apache
a.Running PHP 5 as an Apache Module
To configure Apache to load PHP as a module to parse your PHP scripts, use an ASCII text editor to open the Apache configuration file, "httpd.conf". If you use Apache 1.x, the file is found in "c:\Program Files\Apache Group\Apache\conf\". Apache 2.0.x users can find it in "C:\Program Files\Apache Group\Apache2\conf\" while Apache 2.2.x users can find it in "C:\Program Files\Apache Software Foundation\Apache2.2\conf\". Basically, it's in the "conf" folder of wherever you installed Apache.
Search for the section of the file that has a series of "LoadModule" statements. Statements prefixed by the hash "#" sign are regarded as having been commented out.
If you are using Apache 1.x, add the following line after all the LoadModule statements:
LoadModule php5_module "c:/php/php5apache.dll"
If you are using Apache 2.0.x, add the following line after all the LoadModule statements:
LoadModule php5_module "c:/php/php5apache2.dll"
If you are using Apache 2.2.x, add the following line instead:
LoadModule php5_module "c:/php/php5apache2_2.dll"
This steps can be found on this link
Thank you everybody who tried to give my answer and helped me. It was a surprising experience for me that how quick you answered my question!! It was my ignorance to follow your suggestions. I've solved the problem by installing xampp. Now php is running fine in my computer.I am new in PHP programming and I will ask your help in future also. So be helpful to me always. I will be grateful to you. Thanks again.
Kallol Das,
Bangladesh.

Having trouble getting php to work

I am just starting in web development, and I am having trouble getting php to work. I have access to a domain on my universities' server that should have php installed, although I'm not entirely sure about that.
This is the code I am trying to run.
<html>
<body>
<p>
testing to see which scripting languages work
</p>
<?php
echo "php works";
?>
<script type="text/javascript">
document.write("javascript works")
</script>
</body>
</html>
I notice you are able to 'run' HTML and javascript code by opening the text file with a browser (I use Chrome). But when I view the page source inside the browser, the php is commented out. This same code is loaded onto my website the university provides, and the same thing happens there.
I've looked around for how to solve this, I figured you need to have some sort of flag indicating the page runs php, but I can't find anything.
Here is my webspace if it helps:
Removed for privacy purposes
Thanks everyone for the responses, I was able to get it to work by changing it to a php file.
The usual flag for indicating that a file contains PHP is to give it a filename ending in .php.
Support from the server is, of course, required.
In more general terms, there are lots of different ways that this can be done, but they depend on the specific web server software that is being in use.
With Apache HTTPD, you use the directive SetHandler application/x-httpd-php inside a conditional.
For example, to take the Debian php5.conf file:
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
This causes any files with a name that matches that regular expression (i.e. end in .php, .php3 or .phtml) to be processed by PHP.
For certain, your server is running php. I tried to invent a web page name that i was certain didn't exist and got this error:
Apache/2.2.3 (Red Hat) DAV/2 mod_pubcookie/3.3.4a PHP/5.1.6 mod_python/3.2.8 Python/2.4.3 mod_ssl/2.2.3 OpenSSL/0.9.8e-fips-rhel5 mod_perl/2.0.4 Perl/v5.8.8 Server at home.engineering.iastate.edu Port 80
You're running PHP, alright. Like everyone else has mentioned, you'll have to ensure that the filename extension of that file whose code you posted above ends in .php then try it again.
You can't just open a php script in a browser, because it has to be executed by the php program first. This is done by a webserver (you can install one to test on your own machine, see, e.g., XAMPP).
If the server is configured to execute php (which it seems to), you should be able to make it work by naming your script index.php. The file extension .php tells the server to run the script first before sending anything to the browser.

Apache downloads php files instead of executing script

I just set up a local web server on a new Mac running 10.6 (Apache2 / PHP / Mysql). Everything seems to be working fine except when I navigate to SOME (not most) pages Apache downloads php files instead of executing them.
I thought this might have something to do with the time it takes php to execute the script so I increased my "max_execution_time" but it didn't help.
I wish I could give more details but the error logs don't output anything so I really have no idea why this is happening. One thing that might be of significance is that only pages that don't query the database seem to work every time.
UPDATE:
Just discovered something strange that might give more insite into the problem. If I type the following URLs:
localhost/index.php // the home page is served up correctly
localhost/index.php?page=1 // the home page is served up correctly
localhost/index.php?page=home // ERROR: index.php is downloaded !!!!!!!!!!
localhost/index.php?page=contact // the contact page is served up correctly
localhost/index.php?page=blog // ERROR: index.php is downloaded
localhost/index.php?page=10 // the blog page is served up correctly
Uncommenting the following line in /etc/apache2/httpd.conf fixed the problem for me:
LoadModule php5_module libexec/apache2/libphp5.so
You could try adding this to your conf or htaccess:
AddType application/x-httpd-php .php
I know that this was asked a long time ago, but I just encountered this problem so I thought I'd post what the cause was for me.
Make sure that you don't have...
AddHandler application/x-httpd-php5 .php
...in BOTH your .htaccess file and your httpd.conf file. Check your .htaccess file, comment it out, restart Apache, and try again. That is most likely your problem since Apache renders php for some sites but not others. It has to be at the directory level, which would point to a .htaccess file setting.
I stumbled upon this problem after upgrading php to 5.5 and reinstalling apache.
Finally,this fixed it, in case someone else needs it.
apt-get install libapache2-mod-php5
(here's the answer)

Setting up local server with PHP

I'm trying to setup an Apache/PHP/Postgresql server locally on my machine. I'm using Windows vista business 32bit. I tried to install everything manually (one thing at a time, apache, postgresql and php (all the latest stable releases)) and after I get everything up and running.
Whenever I try to run a script on my machine, I get a "What do you want to do with the *.php file?" dialog. The dialog is the browser's open/save dialog
I'm just trying to get the output of phpinfo() to make sure everything is up and running...
I already tried to mess around a bit with the Apache conf file, but since I don't know much about what I'm doing, I reinstalled everything again and the problem is still there. I kinda get the feeling it must have something to do with the PHP thingy isn't correctly installed.
When i try to get the output of phpinfo as in:
<pre><?php
phpinfo();
?></pre>
I get the browser's "Open/Save" dialog for the *.php file.
You should have something like this in your httpd.conf file:
LoadModule php5_module "c:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "c:/php"
Make sure that's in place, and don't forget to restart apache!
In Windows, the default location for your conf file is C:\Program Files\Apache Group\Apache2\conf\httpd.conf
You can also have a look at the official page of PHP in the install section.
There is a closer link if you are on Windows.
And you can also use some precompiled installer for this like XAMMP and install Postgres after all is set up and running with the web server and php.
are you on Windows?
I use Wamp server, which is an excellent way of getting Apache, MySQL and PHP installed and configured without any hassle on Windows.
If you want to use Postgres instead, provided that you've got it installed separately it will work fine. (one great thing you can do with Wamp is add and remove PHP extensions via a GUI pretty much on-the-fly, and pgsql is one of them).
Maybe somebody can help, but you'd be much better off if you'd provide some relevant details.
What sort of system are you using? Be specific.
What do you mean by "everything up and running"?
What are you doing when you "try to run a script"?
What installation procedures did you use? (If you were following them off a script or how-to, we at least need to know where to find the script or how-to.)
We don't automatically know these things. What seems obvious to you may not be clear to us, and what seems irrelevant to you may turn out to be crucial.
In httpd.conf, make sure the PHP module is being loaded and that that line isn't commented out. (Comments in httpd.conf starts with #.)
Also what OS are you running?
I had the same problem, You need to configure apache and add the php module...
e.g I compiled the php from source as well as the apache. After doing so I then copied the libphp5.so from php/lib dir in to the apache/modules dir. Than you have to add php in the http.conf
LoadModule php5_module modules/libphp5.so
AddHandler php5-script php
you can then restart apache....it's not the most elegant of solutions but it works.

Categories