Debug School

rakesh kumar
rakesh kumar

Posted on

How to add static value and dynamic value that returned inside ajax success after server side process in json form in laravel

Here's an example of jQuery code that sends an AJAX request to a Laravel controller and receives JSON data back. The code adds a static value to the data received from the server, and displays the result in an alert message.

$.ajax({
    type: "POST",
    url: "/path/to/controller",
    data: { someData: "example" },
    success: function(response) {
        // Add a static value to the response
        response.staticValue = "hello";

        // Use the dynamic value returned from the server
        var dynamicValue = response.dynamicValue;

        // Display the result in an alert message
        alert("Static value: " + response.staticValue + ", dynamic value: " + dynamicValue);
    }
});
Enter fullscreen mode Exit fullscreen mode

In your Laravel controller, you can return JSON data using the json method of the response object:

public function someAction(Request $request)
{
    // Process the request data
    $dynamicValue = $request->input('someData') . " processed";

    // Return JSON data with static and dynamic values
    return response()->json([
        'staticValue' => 'world',
        'dynamicValue' => $dynamicValue,
    ]);
}
Enter fullscreen mode Exit fullscreen mode

In this example, the controller receives a POST request with a parameter named someData, processes the data and returns JSON data with a static value of world and a dynamic value that depends on the processed data.

ANOTHER WAY

Here's an example of jQuery code that sends an AJAX request to a Laravel controller and receives JSON data back. The code adds a static value to the data received from the server, and displays the result in a span element on the page.

$.ajax({
    type: "POST",
    url: "/path/to/controller",
    data: { someData: "example" },
    success: function(response) {
        // Add a static value to the response
        response.staticValue = "hello";

        // Use the dynamic value returned from the server
        var dynamicValue = response.dynamicValue;

        // Display the result in a span element
        $("#result").text("Static value: " + response.staticValue + ", dynamic value: " + dynamicValue);
    }
});
Enter fullscreen mode Exit fullscreen mode

In your Laravel controller, you can return JSON data using the json method of the response object:

public function someAction(Request $request)
{
    // Process the request data
    $dynamicValue = $request->input('someData') . " processed";

    // Return JSON data with static and dynamic values
    return response()->json([
        'staticValue' => 'world',
        'dynamicValue' => $dynamicValue,
    ]);
}
Enter fullscreen mode Exit fullscreen mode

In this example, the controller receives a POST request with a parameter named someData, processes the data and returns JSON data with a static value of world and a dynamic value that depends on the processed data.

ANOTHER WAY

Here's an example of jQuery code that sends an AJAX request to a Laravel controller and receives JSON data back. The code adds a static value to the data received from the server, and displays the result in a span element on the page.

$.ajax({
    type: "POST",
    url: "/path/to/controller",
    data: { someData: "example" },
    success: function(response) {
        // Add a static value to the response
        response.staticValue = "hello";

        // Use the dynamic value returned from the server
        var dynamicValue = response.dynamicValue;

        // Display the result in a span element
        $("#result").text("Static value: " + response.staticValue + ", dynamic value: " + dynamicValue);
    }
});
Enter fullscreen mode Exit fullscreen mode

In this example, the code finds the span element with the id of result and sets its text content to the concatenated string of the static and dynamic values.

In your Laravel controller, you can return JSON data using the json method of the response object:

public function someAction(Request $request)
{
    // Process the request data
    $dynamicValue = $request->input('someData') . " processed";

    // Return JSON data with static and dynamic values
    return response()->json([
        'staticValue' => 'world',
        'dynamicValue' => $dynamicValue,
    ]);
}
Enter fullscreen mode Exit fullscreen mode

In this example, the controller receives a POST request with a parameter named someData, processes the data and returns JSON data with a static value of world and a dynamic value that depends on the processed data.

Assuming you have a span element on your page with an id of result, the resulting HTML should look something like this:

html

<div>
    Result: <span id="result"></span>
</div>
Enter fullscreen mode Exit fullscreen mode

=============================
jquery

Image description
** in Html**

<b><span id="totalcosts"  style="color:green;font_size:24px;"></span>  <span  style="color:green"></span></b>
Enter fullscreen mode Exit fullscreen mode

Image description

===================================
jquery

Image description

Html

Image description

Top comments (0)