dongxiusuo9881
2017-02-24 19:10
浏览 286

简单的Ajax请求将数据从文本文件回显到HTML获取GET 500(内部服务器错误)

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)

enter image description here enter image description here

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>";
}

?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsao8279 2017-02-24 19:26
    已采纳

    I believe this is not an ajax issue. If you visit http://localhost:8888/p2/showall.php I think you will get the same 500 error. Try to check your server, if its a php issue, create a html file to return same content as you want, will be easier to debug.

    点赞 打赏 评论

相关推荐 更多相似问题