1

Truth in advertising: this is a homework assignment that I'm struggling with.

I'm running XAMPP on my desktop. I'm using an AJAX post to send form data to a PHP page by way of the jQuery validation plug-in. Here's the JS:

var $infoForm = $('#infoForm');

$infoForm.validate({
    submitHandler: function() {
        var formValues = getFormValues();
        $.post("/Week5/process.php", formValues, postResponse, "text");
    }
});

function postResponse(response) {
    $infoForm.fadeOut("slow", function() {
        console.log(response);
        $('#postResponse').empty().append('<p>' + (response.message ? response.message : 'The server is silent') + '</p>').slideDown("slow");
    });    
}

Here's process.php:

<?php 
    header("Content-type: application/json");
    $message = array('message'=>'Thank you for your submission');
    echo json_encode($message); 
?>

This is what response looks like in the console:

<br />
<b>Warning</b>:  Unknown: failed to open stream: No such file or directory     in <b>Unknown</b> on line <b>0</b><br />
<br />
<b>Fatal error</b>:  Unknown: Failed opening required 'D:/User Data/johnr/OneDrive/Education/ICT 4510/htdocs/Week5/process.php' (include_path='C:\xampp\php\PEAR') in <b>Unknown</b> on line <b>0</b><br />

I get the same messages (without the HTML tags) in my Apache error logs. I don't think I have a path error, because when I change the URL in $.post() from 'process.php' to something like 'foo.php' I get a 404 Not Found like I would expect to get. It seems like the server is finding process.php but then failing to find the response it renders. I don't think the validation plug-in is the problem, because I tried it by handling the form's submit event directly and the result didn't change:

$infoForm.submit(function(event) {
    event.preventDefault();
    var formValues = getFormValues();
    $.post("/Week5/process.php", formValues, postResponse);
});

One last datapoint: I actually get two request/response events related to process.php. The first is a POST with the right request payload and the errors as the response payload. The second is a GET with no request or response payload.

John Riehl
  • 1,270
  • 1
  • 11
  • 22
  • 1
    What is localhost path of html page and where is process.php within that project? And are you using any php framework? – charlietfl Apr 29 '18 at 20:04
  • you can try writing full path to your file, maybe you missed folder. – BARNI Apr 29 '18 at 20:04
  • 2
    To inspect whats going on we weed to ignore js for a bit and directly query that `process.php` page. Once you managed to query it and it's response is correct we will think to js. So i definitely have to ask you to try to call `process.php` from your browser. Your URL would be something like http://127.0.0.1/Week5/process.php – Giacomo Penuti Apr 29 '18 at 20:06
  • @charlietfl The HTML and PHP are both at D:/User Data/johnr/OneDrive/Education/ICT 4510/htdocs/Week5/. The website root (DocumentRoot in httpd.conf) is D:/User Data/johnr/OneDrive/Education/ICT 4510/htdocs/ – John Riehl Apr 29 '18 at 20:12
  • @BARNI I had tried that as well, with no difference in result – John Riehl Apr 29 '18 at 20:12
  • 1
    Best suggestion is make a very simple php page with a simple text echo output and try opening in browser address bar and see if that works. Will at least help sort out if it's path problem in browser or back end. If it works compare final path to path used in browser dev tools network for the ajax – charlietfl Apr 29 '18 at 20:15
  • @GiacomoPenuti I hadn't thought to do that...thanks. Unfortunately, browsing directly to the PHP page gives the same Warning and Fatal Error as the AJAX post. I went to another PHP page that was working in the past, and it's now not working (giving the same error). It looks like I have a XAMPP problem, not an AJAX/PHP problem. Researching that now. – John Riehl Apr 29 '18 at 20:17
  • Happy to help. Regarding the error i think it may have something to do with permissions (seems like php can't read that resource). Are you sure you set xamp php properly to work in that path? How about if that is a external disk and the previous time when you config xamp had another drive letter? – Giacomo Penuti Apr 29 '18 at 20:27
  • Confirmed this is a XAMPP config issue. I had changed `DocumentRoot`, but for some reason PHP won't work in the new directory. I tried [this](https://stackoverflow.com/questions/9556647/changing-documentroot-for-apache-on-windows-isnt-working) without effect. I have things working now by simply changing `DocumentRoot` back to the installed version.hanks for the pointers. – John Riehl Apr 29 '18 at 21:18
  • You're wellcome :) – Giacomo Penuti Apr 30 '18 at 12:43
  • Question maybe for meta, but I'll ask it here. The problem turned out to be nothing at all like the question. It seems like this question should either be deleted or closed in some way. When I click close I get a list of reasons that don't really apply. Clicking delete seems like a nuclear option...will the commenters lose the rep they earned from upvotes on their comments? What's the right way to close this question out? – John Riehl May 01 '18 at 15:32

0 Answers0