Jump to content

Recommended Posts

Hi, I have an html page with a form that people fill out and when they press 'submit' I use a POST php method to save the information they entered onto a .csv file in the directory in my server. The php code is in a separate file that I call from the HTML page. However, I'm getting a 404 error saying that the php file which handles all that input cannot be found.

 

 

THIS IS THE Javascript ON THE HTML DOCUMENT THAT GETS ALL THE VALUES FROM THE FORM WHEN THE SUBMIT BUTTON IS PRESSED:

 


//FUNCTION: Button press
var demogCont = function(){
var timestamp = new Date().getTime();
var age = document.getElementById('age').value;
var freq = document.demographics.item1.value;
var time = document.demographics.item2.value;
var eng = document.demographics.eng.value;
var email = document.demographics.email.value;
var demogs = [[age, freq, time, eng, email]];
var JSONdemogs = JSON.stringify(demogs);
var postArray = {timestamp:timestamp, json:JSONdemogs};
if (document.demographics.email.value.length > 0){
$.post("demogstore.php", postArray,
function(){
window.open('demographics2.html?demog=true&ts='+ timestamp,'_self',false);
}
)
.error(
function(){
alert('Communication error with server.');
window.open('demographics2.html?demog=true&ts='+ timestamp,'_self',false)
}
)
}
};

 

 

THE PHP FILE LOOKS LIKE THIS

 


<?php

// decode JSON string to PHP object
$timestamp = json_decode($_POST["timestamp"]);
$decoded = json_decode($_POST["json"]);



$fp = fopen('PHP_data'.$timestamp.'_data.csv', 'a');



foreach ($decoded as $fields) {
fputcsv($fp, $fields);
}


fclose($fp);

?>

 

The file path and the file name are correct. Does anyone know what could be causing this error?

Link to comment
https://forums.phpfreaks.com/topic/274628-server-cant-find-php-file/
Share on other sites

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Well, what values do you get to $timestamp and $decoded variables when the form has been submitted?

 

You know that timestamp is a dynamic value, right?

 

Example:

 


// decode JSON string to PHP object
$timestamp = json_decode($_POST["timestamp"]);
$decoded = json_decode($_POST["json"]);


echo $timestamp.' '. $decoded;

Yes I want to create a csv file that has timestamp as its name so that each person who fills out the questionnaire has a unique ID and then append data to that file. While timestamp is a changing value, I make a variable equal to one particular value of it so it should not be changing. In fact, this way of doing things has worked for me in the past and I pass the timestamp value on with the url as a query and that has not posed any problems. All the variable values that I get from the form seem to be in order, my unique problem is that the server tells me it cannot find the php file that I wrote to handle the recording of all that data which is strange because the file exists and it sits in the same directory as all the other files and even the path to it that is displayed when the error shows is correct. My question is, what could cause the server to fail to find the file? Many thanks!

Hi @jazzman1, here is the string I get from server:

 

 

18-Feb-2013 12:04:13 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/measured/public_html/leeds/study-info/demogstore.php on line 12

 

@AyKay47 - yep, both files are in the same directory.

I thought the initial problem was that the called php file was not found?

The foreach error is being triggered because you are passing the construct an object instead of an array. By default, if json_decode() is passed an object, it returns an object unless the second "assoc" parameter is set to TRUE.

I admit that I am a bit new to this all. The error I posted previously is from the error log on my server but it's old. I set the assoc parameter to TRUE and that didn't change anything. I use firebug for troubleshooting and that is where it tells me that the server cannot find the php file which I believe to be the actual issue.

Hey, sorry for the confusion. The log error is an old error from yesterday when there wasn't a problem with the php file being found and the script was being executed but this no longer happens. So, right now there is only that one problem and that is that the server cannot find the php file. But that is one of the things that I find very confusing, since the php file was accessed fine at one point and then suddenly it stopped working and I can't figure out what could have caused it.

 

Thanks again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.