1

I'm trying to set a session variable in php file through an AJAX call. But I am getting an undefined index error.

This is the jquery code:

$('.selectcpno li').click(function(){

            //Get the value
             var value = $(this).data("value");
            //Put the retrieved value into the hidden input
             $('input[name=cpnoselected]').val(value);


             $.ajax({
                type: "post",
                url: "../web/cpnoselected.php",
                dataType: "text",
                data:"{'cpno':'" +value+ "'}",
                success: function( data ){
                alert("hellosuccess");
                    document.getElementById("cpno").innerHTML=data;
               },
                error: function( jqXhr, textStatus, errorThrown ){
                alert(value);
                    console.log( errorThrown );
                    console.log( jqXhr );
                    console.log(textStatus);

                }
            });
        });

This is what i have in php file:

<?php

 include("../config/config.php");
 include("../inc/functions.php");

 $cpnoselected=  $_POST['cpno'];
 $_SESSION['cpno']=$cpnoselected;
 echo $cpnoselected;
 ?>

When I run it I get undefined index: cpno in C:/...../cpnoselected.php

Please help to resolve it

vinni
  • 306
  • 1
  • 13
  • Your `data` needs to be an object, not a JSON string for the data to be sent in the form that PHP will accept in `$_POST`. Alternately, if you insist in passing data as JSON in body, then in PHP [read the body](https://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php), then parse the JSON, and access that array's field. (Also, `dataType: "json"` suggests the server will return JSON, but you are just echoing a string, so this is also something to fix.) – Amadan May 17 '19 at 05:29
  • Sorry, ignore about `dataType: "json"`, that's not what you did. – Amadan May 17 '19 at 05:40

4 Answers4

7

Change

data:"{'cpno':'" +value+ "'}",// it needs to be object not string

To

data:{'cpno':value}, //now its object
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
2

you are making ajax request correct but you posting the data that is not in proper format. You need to send that data as an object but you are sending it as string value like Your code : data:"{'cpno':'" +value+ "'}"

Just remove the double quote from it like

 data:{'cpno':value}
Pupil
  • 23,834
  • 6
  • 44
  • 66
1

use isset example

   if (isset($_POST['cpno'])) {
  echo "This var is set so I will print.";
}
Pol Carlo
  • 61
  • 7
0

You are getting this error undefined index: cpno in C:/...../cpnoselected.php because you are sending your data as a string, not as a JSON. Do this changes,

$('.selectcpno li').click(function(){

            //Get the value
             var value = $(this).data("value");
            //Put the retrieved value into the hidden input
             $('input[name=cpnoselected]').val(value);


             $.ajax({
                type: "post",
                url: "../web/cpnoselected.php",
                dataType: "json",
                data:{'cpno':value},
                success: function( data ){
                alert("hellosuccess");
                    document.getElementById("cpno").innerHTML=data;
               },
                error: function( jqXhr, textStatus, errorThrown ){
                alert(value);
                    console.log( errorThrown );
                    console.log( jqXhr );
                    console.log(textStatus);

                }
            });
        });
Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
  • 1
    JSON _is_ a string. PHP expects `application/x-www-form-urlencoded` for `$_POST` parsing, not JSON. Your code is correct, but your explanation is wrong. – Amadan May 17 '19 at 05:35
  • @Amadan OP had set dataType as text. – Ropali Munshi May 17 '19 at 05:39
  • Yeah, which was correct (and remembering wrong, I told OP `dataType: "json"` was incorrect for what they were doing). Now I get to tell _you_ the same thing: `dataType` concerns what the server is returning in the AJAX response, not what you are sending to the server. Since OP is just having a simple `echo` of a string, `dataType: "text"` is appropriate. It would also be okay if it was left out, because the default content type would suggest the same thing. With `JSON`, jQuery would try to parse it, and likely throw an error. And this is completely separate from my first comment to you. – Amadan May 17 '19 at 05:43