Require_Once not Linking PHP Files on Server - javascript

I have two PHP files located on my server. crpapi.php (http://www.nickrubin.byethost14.com/crpapi.php) and example.php (http://www.nickrubin.byethost14.com/example.php).
I am building an application using AJAX that posts an id value to example.php, which using crpapi.php, displays some information back to the user. As of now, I am having the AJAX post to "http://www.nickrubin.byethost14.com/example.php" - which works, and displays back some of the information I need. The problem is that the "require_once('crpapi.php')" command in the example.php file isn't working, and therefore not echoing information received from the crpapi.php file. Basically, the files aren't connecting for some reason. Maybe I have the wrong path?
Both files are located in the same directory on my server.
example.php
require_once('crpapi.php');
Thanks for the help.

If the file is on the server, it may it may be the link to the file. So, if the file is in the folder /api and you're working from the root, consider /api/crpapi.php. Try enabling error_reporting to see what the problem is.

I would recommend use set_include_path() before require_once()
set_include_path(implode(PATH_SEPARATOR, array(
realpath(__DIR__ . '/..'),
get_include_path()
)));
Sets the include_path configuration option for the duration of the script.

Related

How do I configure Nginx to allow me to call a PHP script with AJAX?

Trying to save an object to a JSON file in javascript by converting the object to JSON (via JSON.stringify), and passing the stringified JSON to a PHP script using AJAX. However, this continually throws a 405 error. I suspect this has to do with the config file of my Nginx server, or it could be that I'm not hosting the PHP properly. I've seen the phrase 'static hosting' mentioned, which is unclear to me. Currently, the PHP script exists in the same directory as my HTML and javascript (the data in the object was collected via the HTML interface). I've tried adding headers in the nginx config with no success. General examples on how to enable CORS and hosting PHP with Nginx would be helpful.
Javascript :
$.post('save.php', {data : jsonData});
Nginx config currently has no headers added as this has not been successful in the past.
Never mind. The problem was with the PHP hosting. The default config file has a commented portion that forwards PHP to a server at 127.0.0.1:9000. I call this script with '127.0.0.1:9000/myScriptName.php'.
Now if only the PHP script itself would work...

How can I make a POST request to a php file within the same angular application?

I have a strange situation where I need to send some image data to a php file from within the angular application itself... yes, the php file resides in the angular docroot.
Every time I try to upload image data (from the browser) to this file, the application doesn't seem to go through to it. Instead, I get back a response that is the entire index.html code. I understand that this is probably because I am using ui.router to handle my routing, and have it defaulting to index.html. However, I have tried to add app.use("/upload.php", express.static(path.join(__dirname, '/upload.php'))); to my node server script... in hope that the /upload.php url would be succesfully routed... however, I still only get back index.html code and it seems that the upload.php file is never reached.
Can anybody provide any hinters? Much appreciated.

PHP include Javascript-generated filename?

while(x<=num_of_cpkgs){
var cpkg_navtray = '\'navtrays/' + cpkg_array[x] + '.html\'';
<?php include ?> cpkg_navtray <?php ; ?>;
x++;
}
cpkg_array contains potentially multiple file names. I'm wondering if there's a way to include a Javascript-generated filename in a PHP include statement like this?
This doesn't work like this.
TL;DR:
You need AJAX calls for this.
Longer:
When you load a page in your Browser, the server send you the file. If the file is a php file, then it calls the php to process the file. After the file is processed the server send it to you as a static file.
With JS you can do some interaction to the website. Until now you probably got used to sending each file as a GET or POST data with a form. With JS you have to make an XMLHttpRequest to create a dynamic request and your page is won't be refreshed again however you will get the response as a variable in JS.
Read all about this here.

PHP to Javascript: dirname and document_root

I got a little problem. I am working on a project and I got my root, then I got 2 folders: website1 and website2.
website1 is the staff panel, where the upload script is on (where this problem is on). website2 is the website the 'customer' will see. Here all the uploaded images, PDFs etc. are uploaded to.
So, I have a few notes before I start:
I cannot use PHP in my Javascript. The Javascript is in .js files and not in my .php file.
I cannot do this at my AJAX request as something client-side has to be done correctly to see the image.
So basically, I am in website1 and when I upload a file (with DropzoneJS (http://www.dropzonejs.com/)) it does a AJAX request. Here it has to upload a file to a directory in website2. In website1 I have to see that image after uploading it in a IMG tag.
So in PHP I got this:
$uploadPath = filter_input(INPUT_POST, 'uploadPath');
$uploadPath = dirname(getenv('DOCUMENT_ROOT')) . '/' . $uploadPath;
This works and the file gets uploaded to that directory. $uploadPath is this (in Javascript, sent as POST parameter in AJAX request):
/SITE2/assets/uploads/
Now I need to translate the dirname and getenv('DOCUMENT_ROOT') into Javascript. I tried document.location.hostname but this does return a different value than getenv('DOCUMENT_ROOT').
document.location.hostname: 127.0.0.1
getenv('DOCUMENT_ROOT'): D:/xampp/htdocs
How can I get the same value as getenv('DOCUMENT_ROOT') in Javascript?
In php file you can create script with JavaScript variable like this:
<script>
var DOCUMENT_ROOT = "<?= getenv('DOCUMENT_ROOT') ?>";
</script>
the other option is to have your JavaScript file as PHP script then you can use PHP code inside.
You have two options, but I caution you sending the actual file path on the server/disk to the client side is a bad idea.
Use Ajax so the client sends a request like $.ajax({url: "getdir.php", success: someJSFuntion } );
Change your JavaScript file to be a ".php" file and then include the code right in there.
There is no spec that says you can't have your JS file be a ".php" file. So you'd link to it like this:
<script type="text/javascript" src="/js/myjs.php"></script>
And that would let you use PHP directly in your JS file.
Check out php.js for a JavaScript port of dirname...
http://phpjs.org/functions/dirname/
As for getenv('DOCUMENT_ROOT'), you can't get this variable value via JavaScript. The reason for this is the client-side "location" of the JavaScript file says nothing about it's actual location on the server. The best you can do is get the parent directory of the file or the domain name.
For example:
http://example.com/path/to/file
The "DOCUMENT_ROOT" as far as JavaScript is concerned is...
/path/to
It seems very improper to even assume the need to have the server-side location of the file available to the JavaScript. I would simply use either a specific URL query that indicates what location to use, or to send the location back with the AJAX response since you can add whatever you need there.
{
"document-root": "/SITE2/assets/uploads/",
"normal-response": {
...
}
}
And then wherever you would normally use what is normally received, this use...
// For example
var response = JSON.parse (request.responseText);
// Normal response
console.log (response['normal-response'][...]);
// Document Root
console.log (response['document-root']);

cannot find js file

I have stucture code like this:
I try to load javascript into php file like this:
But i have an error like this:
This is my html :
And this is another javascript:
And i try to copy paste the link, and i got an error 404 not found. How can i fix it? Thanks.
Permissions
When the host is correct, and the file is in the right place, and you have no other networking problems, you may sometimes still get a 404 because of bad file permissions. If a server does not have permission to access a file, it may send out a 404 error in response. The reason why some "Not Authorized" error is not given instead, is that this would reveal more information about the files than you, the owner of the server, may intend. The way to respond to requests for privileged files without revealing whether or not they exist is to give a 404.
On Windows, you can view and change the permissions from the File Explorer by right-clicking on the file or folder, then going to Properties -> Security -> Edit. For more information, see the notes on permissions on Microsoft's site.
File Types
Besides permissions, a server must also be configured to serve the type of file you are accessing. If files with different extensions are served, but .js files are not, check the configuration of your server to make sure that .js files aren't blacklisted (or not whitelisted, as the case may be).
Directory Location
You should also verify that the files are actually stored in the top-most directory of the web server if that's how you are accessing them. If they aren't, you may need to prefix the path with the path from the webserver root to your application directory. E.g., instead of fusioncharts/..., you may need /path/to/fusioncharts/... or ../../path/to/fusioncharts.
Other Considerations
In your particular case, you should also verify that the files inside the fusioncharts folder are actually structured the way you think. (E.g., is there really a js/[insert name here].js file inside the fusioncharts folder?
If none of that solves your problem, try to take something that is working and gradually make it more and more similar to the files that aren't working. By figuring out at which point you go from a working setup to a not working setup, you may discover the problem.
If you are referring to a file with path: /ui/new-file.js
then,
1.In html file include
<script type="text/javascript" src="/ui/new-file.js"></script>
2.In server.js or app.js whichever you have, include
app.get('/ui/new-file.js', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', 'login-file.js'));
});
Assuming you are using codeigniter, you can use base_url() function to include JS files in your codeignitor view template file.
<script src="<?php echo base_url(); ?>fusioncharts/js/fusioncharts.js" type="text/javascript"></script>
codeigniter default view template is called welcome_message.php and this file is located in application/view folder.
This is how I include js files in my codeigniter projects. Hope this will help.
In the html you can write *script** in the head or in the body, but not in your file js, delete this in fusionCharts.js
<script type=text/javascript>
In fusionCharts.js write only the function without the script
If you are developing locally, try clearing your cache.
Google Chrome likes to use the cached JavaScript files instead of the real ones.
Clearing your cache should resolve the issue.

Categories

Resources