Sending data using ajax, blank response from server side PHP


confused boy

I am using Ajax to send data to PHP. But the received result is blank.

client code

 <script>
     //on click of a button, Below jQuery Ajax code running.
      var prodStock= $("#product").html(); 
            $.ajax({
                data: prodStock,
                url: url,
                type: 'POST',
                async: false,
                success: function(response) {
                    if (response) {
                        alert(response);
                    }
                }
            });
    </script>

    <?php 
        foreach($data['Products'] as $val){
            $pro.= $val['product_id'].':'.$val['quantity'].',';
        }
    ?>  
    <div id='product'>{<?php echo rtrim($pro, ',');?>}</div>

The browser provides:

<div id="product">{18:1,10:1}</div>

server side code

public function update(){
    if ($this->RequestHandler->isAjax()) {
        print_r($this->request->data);
    }
}

test:

If I send statically data: {18:1,20:1},in ajax it works fine. Return an array.

confused boy

Alternative solutions that I have tried and succeeded.

Client code:

<script>
     //on click of a button, Below jQuery Ajax code running.
     <?php 
        foreach($data['Products'] as $val){
            $pro.= $val['product_id'].':'.$val['quantity'].',';
        }
    ?>  

      var prodStock= $("#product").html(); 
            $.ajax({
                data: <?php echo "{".rtrim($pro, ',')."}";?>,
                url: url,
                type: 'POST',
                async: false,
                success: function(response) {
                    if (response) {
                        alert(response);
                    }
                }
            });
    </script>

Related


jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

jQuery ajax post not sending data to server side (using PHP)

username HTML <a id="1">Add to notebook</a> <a id="2">Add to notebook</a> <a id="3">Add to notebook</a> <a id="4">Add to notebook</a> ... <a id="40">Add to notebook</a> javascript <script> $(document).ready(function(){ //callback handler for post submit

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

Issues sending data back from PHP using AJAX calls

Kevin I'm new to AJAX and php and having trouble returning data from my php script to HTML. My Select statement works so I know there is data, but for some reason I can't figure out why it doesn't return anything. I tested it with "JSON.stringify(data)" and it

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Error sending data to php using Ajax in Wordpress

Bruno I have this form: <select id="mudar_produto"> <option></option> <option value="#produto_1">Novo Produto Higiene</option> </select> <section class="hide-section" id="produto_1"> <form class="form-validate" id="feedback_form"> <div class="ca

Issue with sending data to php script using ajax

Andrew I'm having trouble sending form data to a PHP script via Ajax. I get this error when sending data Fatal error: Class Libs\Controller not found I have written my own MVC project structure and it works fine if I only send data using POST request but when

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Error sending data to php using Ajax in Wordpress

Bruno I have this form: <select id="mudar_produto"> <option></option> <option value="#produto_1">Novo Produto Higiene</option> </select> <section class="hide-section" id="produto_1"> <form class="form-validate" id="feedback_form"> <div class="ca

Sending data using POST via AJAX and PHP

Benjamin Oatmeal I have an object that I want to send in an AJAX request: function send_value() { $.ajax({ type: 'post', url: 'get.php', data: { source1: "some text", source2: "some text 2", uniI

Capture data from ajax on php side

learning success I'm confused. How to capture value on php side after passing data from ajax. Initially, I put the data into an Array of objects. When passing data via Ajax, do I pass it as stringArray or ObjectArray? Does this mean I need stringify()? I'm doi

Get response from web server using ajax

Ilya I'm a beginner in ajax, I decided to use jquery ajax for now, before using ajax, I created simple login/register page, I used bodyparser to make requests, when password or username is incorrect, it reloads, then send response to user which is not correct,

PHP jQuery aJax from response and data

james I'm not sure what I'm missing, but I'm having some issues trying to build the code. Data is not being passed from the form to the database (blank entry) It will also go into repsonse.php instead of staying on the form page How can I pass success and erro

PHP jQuery aJax from response and data

james I'm not sure what I'm missing, but I'm having some issues trying to build the code. Data is not being passed from the form to the database (blank entry) It will also go into repsonse.php instead of staying on the form page How can I pass success and erro

PHP jQuery aJax from response and data

james Not sure what I'm missing, but I'm having some issues with my code trying to build. Data is not being passed from the form to the database (blank entry) It will also go into repsonse.php instead of staying on the form page How can I pass success and erro

PHP jQuery aJax from response and data

james Not sure what I'm missing, but I'm having some issues with my code trying to build. Data is not being passed from the form to the database (blank entry) It will also go into repsonse.php instead of staying on the form page How can I pass success and erro