Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //The following is merely a suggestion of where to start (pseudocode). It is not meant to be completely usable code - Syntax may need to be adjusted.
- //You may wish to restructure this or do this in your own way.
- //set the Content-Type. Remember this must be done before any other output. It should be the FIRST line of your application.
- header("Content-Type: application/json");
- /*
- simply listing tasks via GET method can be performed with:
- $.get("index.php?action=list", callbackFunction);
- An example of a jquery POST
- $.post("index.php?action=xxxx", serializedDataToSubmit, callbackFunction);
- In both cases, the updated table data should be returned, and callBackFuntion should be used to replace the front end HTML.
- HINT - even if a form is submitted with POST, parameters in the URL are still found in the $_GET array. This mean $_GET and $_POST may contain data, though $_SERVER['REQUEST_METHOD'] is only one or the other
- */
- if (isset($_GET['action']) && !empty($_GET['action']))
- {
- $servername = "localhost";
- $username = "webconnect";
- $password = "password";
- $database = "lamp2proj1";
- // Create connection
- $connection = new mysqli($servername, $username, $password, $database);
- if ($connection->connect_error)
- {
- die("Connection failed: " . $connection->connect_error);
- }
- if( $_SERVER['REQUEST_METHOD'] == "GET")
- {
- /*
- Based on table 2 and 3,
- This section of code would be requested if the action is GET.
- The specific action is included in the URL, such as "index.php?action=xxxxx"
- */
- if ($_GET['action'] == "list")
- {
- $sortBy = null;
- $includeCompleted = null;
- if (isset($_GET['sortBy'])
- {
- $sortBy == $_GET['sortBy'];
- }
- else if (isset($_GET['includeCompleted'])
- {
- $includeCompleted == $_GET['includeCompleted'];
- }
- //repeat for include completed
- reloadPage($sortBy, $includeCompleted);
- }
- }
- else if ($_SERVER['REQUEST_METHOD'] == "POST"])
- {
- if ($_GET['action'] == "new" )
- {
- && $_SERVER['REQUEST_METHOD'] == 'POST')
- {
- newPost($connection);
- }
- }
- else if ($_GET['action'] == //COMPLETE REMAINING CASES HERE IN THE SAME FASHION
- }
- }
- else
- {
- //action is not set, so just load the page
- reloadPage();
- }
- //this function should be called after every action that modifies the database AND on first page load.
- function reloadPage($sortBy = "dateCreated", $includeCompleted = false)
- {
- //include the code to fetch the content from the database here.
- //i would suggest putting this code within a function so it can be called after each other action
- //however you may also include it on it's own at the end of the file if you wish
- //return table in JSON format. ENSURE YOU ENCODE IT IN JSON
- //This JSON will be available for manipulation in the callback function.
- }
- function newPost($connection)
- {
- $formVals = array();
- $formVals['description'] = (isset($_POST['description']) ? $_POST['description'] : "");
- //$formVals['postContent'] = (isset($_POST['postContent']) ? $_POST['postContent'] : "");
- $newQry = sprintf("INSERT INTO wallpost (description, /*postContent,*/ active) VALUES ('%s', /*'%s',*/ 1)",
- addslashes($formVals['description'])/*, addslashes($formVals['postContent']))*/;
- $connection->query($newQry);
- if($connection->affected_rows > 0)
- {
- echo "Insert Successful!";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement