1

I'm new to both PHP and developing on Mac OS X.

I have the following code on the front-end (browser on my laptop):

$('form').on('submit', function(e) {
    $.post( 'save.php', $(this).serialize(), function(response) {
        console.log( response );
    });

    e.preventDefault();
});

And this simple PHP code on the back-end (Apache server on my laptop):

<?php

$f = $_POST['content']); //content is coming from a form textarea

echo $f;

I expect whatever I submit to be echo'd on the console (by PHP). What happens instead is that I get the entire PHP code in the console. My rudimentary understanding is that the server is not executing the PHP code and is instead returning it as plain-text.

I've searched around for three hours and did the following:

  1. (Re)installed MAMP.
  2. Made sure Apache server is actually running
  3. Made sure I access the site via Apache as opposed to opening it as a file in the browser. I'm doing /localhost/~username/index.html
  4. Checked to see if httpd.conf contains this line and it does:

    LoadModule php5_module modules/libphp5.so

  5. Added this to httpd.conf and restarted Apache server:

    AddType application/x-httpd-php .php

I then started reading the PHP documentation and found this, which describes the problem I have and the solution. The issue is that, unlike with libphp5.so, I don't actually have a mod_php.so or a lib_perl.so file under the modules folder. I also searched for them on Spotlight and it didn't return anything.

So... what else do I need to do/check? Thank you in advance.

Ege Ersoz
  • 6,461
  • 8
  • 34
  • 53
  • Server configuration questions are better for serverfault.com, SO is for programming questions. – Barmar Dec 25 '13 at 23:50
  • 1. There is no closing tag `?>` in your php file 2. Did you install PHP on your server ? what happens when you create index.php in your apache root directory with a simple `echo` ? with `phpinfo()` – Nir Alfasi Dec 25 '13 at 23:53
  • PHP usually does not parse `.html` files. Change the file to `.php` and see if that works. – Sverri M. Olsen Dec 25 '13 at 23:53
  • 1
    Closing tag is irrelevant. It's not required in an all PHP file. Are you saving these files in the correct directory according to your virtual hosts configuration? – SomeShinyObject Dec 25 '13 at 23:54
  • 2
    @alfasin Closing tags are actually advised against because trailing whitespace *after* the closing tag will get through. – poke Dec 25 '13 at 23:56
  • So you are accessing the file like this: `http://localhost:8888/~username/save.php`. I think port 8888 is MAMP default. Post your exact URL – Jompper Dec 26 '13 at 00:02

1 Answers1

0

Okay, I figured it out. I believe the problem stemmed from my own lack of understanding of OS X in general and Apache/MAMP in particular (I'm new to both!). Specifically, I didn't realize that OS X runs its own Apache server, and that when you install MAMP, it essentially gives you an alternate installation (I think?). I realized this when I tried to start Apache from the terminal using 'sudo apachectl -k start' and it said process was already running. I then did a bit of Googling and understood why.

Then I implemented the solution. Previously, I had everything under User\Sites and I was getting the PHP script back in plain-text. I moved my files to Applications/MAMP/htdocs. Then I ran MAMP PRO, which showed that Apache was running on port 8888. So I navigated to localhost:8888/~username/ and submitted the form, and the PHP script was processed correctly and I got the echo back.

Someone correct me if my understanding is incorrect. But at least my little project works now. :)

Ege Ersoz
  • 6,461
  • 8
  • 34
  • 53