Problem
I'm new to AJAX request so I believe I might be making a simple mistake, but whenever I want to run my script using an AJAX request I get a 500 (Internal Server Error)
Basically what I want to happen is if the user presses the #show-all
button I will run a showall.php
script that reads tasks from a data.txt
file and prints the tasks in HTML on the webpage.
Any advice would be greatly appreciated!
PS: Line 24 refers to $.ajax({
and Line 27 refers to console.log("Error: " + error);
Code
Ajax request
$("#show-all").on("click", function () {
console.log("button pressed");
$.ajax({
url: '../p2/showall.php'
, error: function (error) {
console.log("Error: " + error);
}
, success: function (response) {
console.log("Success: " + response);
}
});
});
showall.php
<?php
$filename = 'data.txt';
include('file.php');
include('add.php');
$tasks = read_file($filename);
foreach($tasks as $task){
echo_task($task);
}
?>
file.php
<?php
//Write task element to file
function write_file($filename, $task){
$arr = array($task->title, $task->due_date, $task->priority, $task->course, $task->note);
$line = implode("\t", $arr);
$line .= "
";
$file_pointer = fopen($filename, 'a' );
if ( ! $file_pointer ) { echo( 'error' ); exit; }
$error = fputs($file_pointer,$line);
fclose($file_pointer);
}
//Read file $filename and return array of Tasks
function read_file($filename){
$lines = file($filename);
if(!$lines){
print( 'error' );
exit;
}
$tasks = array();
foreach($lines as $line){
//Assume every entry should have notes
$arr = explode("\t", $line);
//Assume notes should not have
in it
$arr[4] = str_replace("
", "", $arr[4]);
$task = new Task($arr[0], $arr[1], $arr[2], $arr[3], $arr[4]);
$tasks[] = $task;
}
return $tasks;
}
?>
add.php
<?php
//Returns true if text field input isset & not empty string, otherwise returns false & echos issue to use
function validText($field){
if(isset($_POST['add'])){
if(isset($_POST[$field])){
if($_POST[$field] !== ''){
return true;
}
echo "<h3 class='error'>*Task $field cannot be empty</h3>";
return false;
}
echo "<h3 class='error'>*Task $field must be set</h3>";
return false;
}
return true;
}
//Return task from form elements
function task_from_form(){
if(isset($_POST['add']) && isset($_POST['title']) && isset($_POST['note'])){
if($_POST['title'] !== '' && $_POST['note'] !== ''){
$title = $_POST['title'];
$note = $_POST['note'];
$title_trim = trim($title);
$note_trim = trim($note);
$title_html = htmlentities($title_trim);
$note_html = htmlentities($note_trim);
$due_date = $_POST['due-date'];
$priority = $_POST['priority'];
$course = $_POST['course'];
$course_space = str_replace("-", " ", $course);
$task = new Task($title_html, $due_date, $priority, $course_space, $note_html);
return $task;
}
}
}
//Echo task
function echo_task($task){
echo "<div class='task row'>
<div class='task-title row'>
<button type='button' class='checkbox col'><span class='icon-checkbox-box' aria-hidden='true'></span></button>
<h1 class='col'>{$task->title}</h1>
<div class='task-info'>
<h2 class='due-date col task-date'>{$task->due_date}</h2>
<button type='button' class='priority {$task->priority} col'><span class='icon-circle' aria-hidden='true'></span></button>
</div>
</div>
<div class='task-details'>
<div class='row'>
<h2 class='col'>{$task->course}</h2> </div>
<div class='row'>
<p class='note col'>{$task->note}</p>
</div>
</div>
</div>";
}
?>