Вопрос

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.

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top