skip to Main Content

I am trying to pass array results from a php submission form back to jquery i can trigger a success or fail message using toast.

i am using


                $ret["icon"] = "success";
                $ret["title"] = "Request Deleted Successfully";
                echo json_encode($ret);

this is the result

{"icon":"success","title":"Request Deleted Successfully"}

but to trigget the toast object i need

Toast.fire({icon: 'success',title: 'Request Deleted Successfully.'})

Looking at the different i only need to remove the commas from the array key but i dont know how to do this in jquery or javascript

Can anyone please help

2

Answers


  1. Chosen as BEST ANSWER

    That worked great thanks

    function(data, status){ if(status === "success") { if(pageLink == "approverequest") {

                                    Toast.fire(JSON.parse(data));
                                    loadDataTableButtons("#requests-container","#project-requests","'.DIR.'admin/dashboard/requests");
                                } else {
                                    
                                    Toast.fire(JSON.parse(data));
                                    loadDataTableButtons("#requests-container","#project-requests","'.DIR.'admin/dashboard/requests");
                                }
                            }
                        });
    

  2. You receive a JSON string and need to convert it to an object.

    const string = '{"icon":"success","title":"Request Deleted Successfully"}';
    const object = JSON.parse(string);
    console.log(object.title);
    

    screenshot

    With sending the correct header from PHP (before any other output)

    header("Content-Type: application/json");
    

    you should directly receive an object from jQuery without need to parse it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search