-1

I have question regarding on my Inserting Module, Why does the response shows Internal Server Error 500 ? Is the front end side has a problem? It means something has gone wrong on the website? But I tried on my updating content module the ajax is working. however in my inserting module, the ajax gives the error. why does it happen? could someone can help me about this problem? thank you.

Error

Front End Ajax:

    $(document).ready(function(){
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    const email = localStorage.getItem('email');
    $('#hidden_auth_user').val(email);
    $('.about_psmid_button').on('click',function(){      

        const about_psmid_page = $('#about_psmid_page').val();
        const about_psmid_title = $('#about_psmid_title').val();
        //const about_psmid_content = $('#about_psmid_content').val();
        const hidden_auth_user = $('#hidden_auth_user').val();
        const ck_editor_content =  CKEDITOR.instances['about_psmid_content'].getData();
        const data = new FormData();
        data.append('about_psmid_page',about_psmid_page);
        data.append('about_psmid_title',about_psmid_title);
        data.append('about_psmid_content',ck_editor_content);
        data.append('hidden_auth_user',hidden_auth_user);

        Swal.fire({
            title: 'Are you sure to add this content?',
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#008B74',
            confirmButtonText: 'Okay'
          }).then((result) => {
            if (result.value) {
                $.ajax({
                    url:'./Function/aboutPMSIDAddFunction.php',
                    data:data,
                    type:'POST',
                    dataType:'JSON',
                    processData: false,
                    contentType: false,
                    success:function(response){
                        Swal.fire(
                        'Success!',
                        'Data information has been saved.',
                        'success'
                        )
                        if(response == 'Successfully Inserted') {
                            location.reload();
                        }
                    },
                    error:function(response) {
                        console.log(response);
                    }
                });

            }
        })



    });
});

Back End Php:

    <?php 
    require_once('../ConnectionString/require_db.php');
    session_start();
    //submit function
    if(isset($_POST['about_psmid_page']))
    {
        $about_psmid_page = $_POST['about_psmid_page'];
        $about_psmid_title = $_POST['about_psmid_title'];
        $about_psmid_content = $_POST['about_psmid_content'];
        $hidden_auth_user = $_POST['hidden_auth_user'];
        $now = (new \DateTime())->format('Y-m-d H:i:s');

        $stmt = $db->prepare('INSERT INTO tblcontent (title, content, posted_by, when_inserted, posttype) VALUES (?,?,?,?,?)');
        $stmt ->bind_param('sssss', $about_psmid_title, $about_psmid_content, $hidden_auth_user, $now, $about_psmid_page);
        $stmt ->execute();

        echo json_encode('Successfully Inserted'); 
    }
?>
DevGe
  • 1,381
  • 4
  • 35
  • 66

1 Answers1

1

At first you need to debug an issue. The shortest way is to enable error reporting. You can just add those lines on the top of php file

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

However looks like you are wrong in parsing json data. To receive json data you should get contents of php://input

$data = json_decode(file_get_contents("php://input"));

if(isset($data['about_psmid_page'])) {
...
Vitalii
  • 1,137
  • 14
  • 25
  • Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. LINE 14 – DevGe Jul 02 '19 at 16:06
  • error goes to $now = (new \DateTime())->format('Y-m-d H:i:s'); – DevGe Jul 02 '19 at 16:06
  • Take a look on this answer https://stackoverflow.com/questions/16765158/date-it-is-not-safe-to-rely-on-the-systems-timezone-settings – Vitalii Jul 02 '19 at 16:14