Hi
I’ve been doing PHP for a bit but I"m getting into the nitty grit and need to start off by using proper paths so my application will run no matter where I put it.
I’m starting to build my first CRUD and the paths are really throwing me. Locally in MAMP my relative paths work correctly, i.e. if I use …/myfile.php in my functions file that is one level deep in an includes folder, works fine. This also works on a subdomain. But, if I instead access the folder through the main domain and not using the subdomain URI, then all the paths break. At the same time, none of the page assembly breaks, just when I try to load the php files into the content area on clicking nav links.
I’m running a LAMP server with WHM and Cpanel, latest versions of all software are setup.
In this instance, I’m loading separate PHP files into a tag (main content area) in the html. I’m using all PHP files and assembling them, header, page, footer setup. I’m using Javascript and jQuery to trigger a function that looks in my /includes/functions.php and then includes the files based on a variable when the navigation links are clicked.
Here is how that main page is setup:
[php]<?php include ("includes/header.php");?>
<?php include ("includes/sidebar.php");?>
<!--Page Content-->
<section class="content" id="content">
<!-- this is where the external php files load -->
</section>
<?php include ("includes/footer.php");?>
[/php]
When the page first loads I have a javascript fire off to load in the welcome.php:
[php]// load welcome
window.load = swapContent(‘con1’);[/php]
My navigation is setup in an unordered list with a tags in the li: <li class="navlink"><a href="#" onClick="return false" onmousedown="javascript:swapContent('con1')">my link</a></li>
When clicked, it is using this Javascript and jQuery(latest v2.1):
function swapContent(cv){
$("#content").html("blah").show();
var url = "includes/functions.php";
$.post(url, {contentVar:cv}, function(data){
$("#content").html(data).show();
});
}
Here are some paths that didn’t work.
[php]// Javascript Variable
$contentVar = $_POST[‘contentVar’];
// Load Content
// Load Welcome Screen - Restart
if ($contentVar==“con1”){
include ("…/welcome.php");
}[/php]
this I tried next:
[php]$path = $_SERVER[‘DOCUMENT_ROOT’].’/myFolder’;
// Javascript Variable
$contentVar = $_POST[‘contentVar’];
// Load Content
// Load Welcome Screen - Restart
if ($contentVar==“con1”){
include ("$path/welcome.php");
}[/php]
and then this:
[php]$path = $_SERVER[‘DOCUMENT_ROOT’];
// Javascript Variable
$contentVar = $_POST[‘contentVar’];
// Load Content
// Load Welcome Screen - Restart
if ($contentVar==“con1”){
include ("$path/myFolder/welcome.php");
}
[/php]
I’ve been looking online for 2 days about paths, but the way people are explaining it makes no sense to me. I should mention I’m an artist and not a pragrammer, so maybe my brain isn’t wired right for this.
Please be patient, most of this stuff I’m just piecing together from web searches.
thanks