Is this a viable back-end for a project?
Posted: Sat Feb 16, 2013 1:25 pm
I'm currently working on a personal project of building a portal from scratch to assist in understanding php better, it's a consolidation of certain tasks I perform.
Originally, I discovered jQuery and quickly implemented it into everything I did. What I was doing before was for every task I created, I made two php scripts. The first one consisting of the actual PHP and HTML elements, the second was a parser that was used when it was called via AJAX, which quickly became very annoying to manage with the different queries and javascript on different pages, when these I started added more and more tasks. So, I consolidated a lot of the functions into a two files.
Now, my new setup is that I still have specific PHP files for the content/HTML, but the back-end now consists of an AJAX.js and AJAX.php. So, the process would be. User visits website, runs a task, which is typically an onClick residing on an HTML button, that executes a JavaScript function located in AJAX.js. The function is comprised of an ajax request sent to AJAX.PHP where the data is parsed and on success, an HTML <div> is updated to display the results on the original page. From a performance standpoint, assuming I wanted to continually scale this. Is this viable?
AJAX.js consists of all my ajax request functions and AJAX.PHP utilizes a case/switch based on a variable passed along from the AJAX.js AJAX request. To give you a visualization of these two files, I edited them a bit to make it easier to understand since some of these tasks are specific to things I'm working on.
ajax.js
ajax.php (This is just an example, typically the task will actually perform a query or run a task here.)
Bonus question: Any pointers on this index page I created? It uses a MySQL database where the name, file and access level is stored. This allows a dynamic page creation, where the portal will stay the same, but I can manipulate the Main <div> to display content I'd like to view based on the switch function. It also serves as access control to view certain tasks based on the user's access level.
EDIT: requinix if you read this - I just read a post by you about SQL injection and not putting "$_GET" directly into a query, I'm going to be adjusting this later tonight.
Originally, I discovered jQuery and quickly implemented it into everything I did. What I was doing before was for every task I created, I made two php scripts. The first one consisting of the actual PHP and HTML elements, the second was a parser that was used when it was called via AJAX, which quickly became very annoying to manage with the different queries and javascript on different pages, when these I started added more and more tasks. So, I consolidated a lot of the functions into a two files.
Now, my new setup is that I still have specific PHP files for the content/HTML, but the back-end now consists of an AJAX.js and AJAX.php. So, the process would be. User visits website, runs a task, which is typically an onClick residing on an HTML button, that executes a JavaScript function located in AJAX.js. The function is comprised of an ajax request sent to AJAX.PHP where the data is parsed and on success, an HTML <div> is updated to display the results on the original page. From a performance standpoint, assuming I wanted to continually scale this. Is this viable?
AJAX.js consists of all my ajax request functions and AJAX.PHP utilizes a case/switch based on a variable passed along from the AJAX.js AJAX request. To give you a visualization of these two files, I edited them a bit to make it easier to understand since some of these tasks are specific to things I'm working on.
ajax.js
Code: Select all
function task1()
{$(function(){
var taskid=task;
$.ajax(
{
url: 'inc/ajax.php?p=' + taskid,
type: 'POST',
data: '',
success: function(html)
{
$('#container').html(html);
}
});
});
}Code: Select all
switch($_GET['p']){
case 'task1':{
echo "success!";
}
break;Bonus question: Any pointers on this index page I created? It uses a MySQL database where the name, file and access level is stored. This allows a dynamic page creation, where the portal will stay the same, but I can manipulate the Main <div> to display content I'd like to view based on the switch function. It also serves as access control to view certain tasks based on the user's access level.
EDIT: requinix if you read this - I just read a post by you about SQL injection and not putting "$_GET" directly into a query, I'm going to be adjusting this later tonight.
Code: Select all
<?php
// require_once to have HTML headers and menu wrapper.
require_once('inc/header.php');
// require_once wrapper, menu bar.
require_once('inc/wrapper.php');
// start main div.
echo "<div id='main'> ";
// sets access_level variable.
$access = $_GET['p'];
// prepares the query for access_level_control and stores it in an array.
$query = "select * from users_access_control WHERE file='$access'";
$result=mysqli_query($db,$query);
$row = mysqli_fetch_array($result);
// uses the above array to pull a case/switch with access_level to arrange content for website, on an access level.
if(!isset($_GET['p']))
{
?>
<script type='text/javascript'>
//<![CDATA[
document.title = 'Portal';
$(document).ready(function(){
document.getElementById('subpagetitle').innerHTML='Main';
});
//]]>
</script>
Please choose a task from the menu to the left.</br>
<?php
}
elseif($_SESSION['accesslevel'] < $row['access_level'])
{
echo "You are not authorized to use this task."; // unauthorized message for users.
}
else
{
require_once $_GET['p'] . ".php"; // opens the php file based on request.
}
// end main.div.
echo "</div>";
// require_once footer.
require_once("inc/footer.php");
?>