Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <?php
  2. //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.
  3. //You may wish to restructure this or do this in your own way.
  4.  
  5. //set the Content-Type. Remember this must be done before any other output. It should be the FIRST line of your application.
  6. header("Content-Type: application/json");
  7.  
  8.  
  9. /*
  10. simply listing tasks via GET method can be performed with:
  11. $.get("index.php?action=list", callbackFunction);
  12.  
  13. An example of a jquery POST
  14. $.post("index.php?action=xxxx", serializedDataToSubmit, callbackFunction);
  15.  
  16. In both cases, the updated table data should be returned, and callBackFuntion should be used to replace the front end HTML.
  17.  
  18. 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
  19.  
  20. */
  21.  
  22.  
  23.  
  24. if (isset($_GET['action']) && !empty($_GET['action']))
  25. {
  26. $servername = "localhost";
  27. $username = "webconnect";
  28. $password = "password";
  29. $database = "lamp2proj1";
  30.  
  31. // Create connection
  32. $connection = new mysqli($servername, $username, $password, $database);
  33.  
  34. if ($connection->connect_error)
  35. {
  36. die("Connection failed: " . $connection->connect_error);
  37. }
  38.  
  39.  
  40. if( $_SERVER['REQUEST_METHOD'] == "GET")
  41. {
  42.  
  43. /*
  44. Based on table 2 and 3,
  45. This section of code would be requested if the action is GET.
  46. The specific action is included in the URL, such as "index.php?action=xxxxx"
  47. */
  48. if ($_GET['action'] == "list")
  49. {
  50. $sortBy = null;
  51. $includeCompleted = null;
  52. if (isset($_GET['sortBy'])
  53. {
  54. $sortBy == $_GET['sortBy'];
  55. }
  56. else if (isset($_GET['includeCompleted'])
  57. {
  58. $includeCompleted == $_GET['includeCompleted'];
  59. }
  60. //repeat for include completed
  61.  
  62. reloadPage($sortBy, $includeCompleted);
  63. }
  64. }
  65. else if ($_SERVER['REQUEST_METHOD'] == "POST"])
  66. {
  67. if ($_GET['action'] == "new" )
  68. {
  69. && $_SERVER['REQUEST_METHOD'] == 'POST')
  70. {
  71. newPost($connection);
  72. }
  73. }
  74. else if ($_GET['action'] == //COMPLETE REMAINING CASES HERE IN THE SAME FASHION
  75.  
  76.  
  77. }
  78.  
  79. }
  80. else
  81. {
  82. //action is not set, so just load the page
  83. reloadPage();
  84. }
  85.  
  86.  
  87.  
  88. //this function should be called after every action that modifies the database AND on first page load.
  89. function reloadPage($sortBy = "dateCreated", $includeCompleted = false)
  90. {
  91.  
  92. //include the code to fetch the content from the database here.
  93. //i would suggest putting this code within a function so it can be called after each other action
  94. //however you may also include it on it's own at the end of the file if you wish
  95.  
  96. //return table in JSON format. ENSURE YOU ENCODE IT IN JSON
  97. //This JSON will be available for manipulation in the callback function.
  98.  
  99. }
  100.  
  101. function newPost($connection)
  102. {
  103. $formVals = array();
  104. $formVals['description'] = (isset($_POST['description']) ? $_POST['description'] : "");
  105. //$formVals['postContent'] = (isset($_POST['postContent']) ? $_POST['postContent'] : "");
  106.  
  107. $newQry = sprintf("INSERT INTO wallpost (description, /*postContent,*/ active) VALUES ('%s', /*'%s',*/ 1)",
  108. addslashes($formVals['description'])/*, addslashes($formVals['postContent']))*/;
  109. $connection->query($newQry);
  110. if($connection->affected_rows > 0)
  111. {
  112. echo "Insert Successful!";
  113. }
  114. }
  115.  
  116. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement