Welcome Guest, Not a member yet? Register   Sign In
Cannot get AJAX to work for Post/Create
#17

(This post was last modified: 06-11-2022, 08:51 PM by spreaderman.)

Really cannot figure this out but getting somewhat closer.  I have rewritten a lot of my code.  This is where I am at. 

This my controller:

Code:
public function task_create(){
    $validation = \Config\Services::validation();
$rules = [
"description" => "required",
];
    $this->validate($rules);
    if ($validation->run()==FALSE) {
    $errors = $validation->getErrors();
    log_message('error', implode($errors));
    $response = [
    'success' => 0,
    'msg' => $errors,
];
echo json_encode($response);
} else {
    $task = new TaskEntity();
        $data = [
            "user_id"      =>  $this->current_user->id,
"description"  => $this->request->getPost("description"),
];
log_message('error', 'id is'.$user_id);
log_message('error', 'description is'.$description);
if ($task->fill($data)) {
$response = [
'success' => 1,
'msg' => "Task created",
];
} else {
$response = [
'success' => 0,
'msg' => "Failed to create task",
];
}
echo json_encode($response);
}
}

This is my javascript;

Code:
$(function() {
        $('#form-task-create').on('submit', function(e) {
            e.preventDefault();
            var form = this;
            $.ajax({
                url:  'https://development.example.com/admin/tasks/create',
                type: $(form).attr('method'),
                data: new FormData(form),
                processData: false,
                contentType: false,
                dataType: 'json',
                beforeSend: function (xhr)  {     
                    //xhr.setRequestHeader('X-CSRF-Token' , tokenHash); 
                    $(form).find('span.error-text').text('');
                },
                success: function(data) {
                        if (data.success == 1){
                            $(form)[0].reset();
                            alert(data.msg);
                        }else{
                            $.each(data.msg, function(prefix, val){
                                $(form).find('span'+prefix+'_error').text(val);
                            });
                        }
                },
            });
        });
    });

When I submit the form without the description, I should see an error in the <span> area.  I do not.  I do see this in the network tab of chrome

Code:
{success: 0, msg: {description: "The description field is required."}}
msg: {description: "The description field is required."}
description: "The description field is required."
success: 0

Above is as expected.

When I submit the form with a description, for example "homework", I get an alert say, "development.example.com says: Task Created.  Unfortunately, it is not.  Under payload in the network tab, I can see; form data, description, homework and status 200 and then after I close the alter, I can see the response as  {"success":1,"msg":"Task created"}.  In my ci log files, I cannot see that the method is catching the vars as the log file say;  ERROR - 2022-06-10 04:47:06 --> id is
ERROR - 2022-06-10 04:47:06 --> description is
I watch expecting the vars to be written into the log.
Reply


Messages In This Thread
RE: Cannot get AJAX to work for Post/Create - by spreaderman - 06-11-2022, 08:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB