0

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.

2
  • 5
    mail.php is being treated by the server as a plaintext file. Fix your config Commented Mar 24, 2012 at 11:38
  • Don't you get PHP code if you just browse to mail.php? Commented Mar 24, 2012 at 11:39

4 Answers 4

1

maybe you test it using wrong url (on the browser)..

it should start with http://localhost/

not file:///c:\wamp\bla

Sign up to request clarification or add additional context in comments.

1 Comment

This should be a comment, not an answer.
0

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

Comments

0

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?

2 Comments

Why would anyone still use php3 nowadays? (even if it's just in the extension)
It's probably just left-overs from back in the day when people named their files ".php3" for some reason. That's what comes with a Debian installation any way and it works.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.