0

Hello i used ajax function send data:itemShape and when i directly run url page or service page it's show error:

Notice: Undefined index: itemShape in C:\wamp64\www\inventory_software\get_item_types_dhk.php on line 5

please someone help how to solve this issue. i am going to upload pic of error for better understanding:

enter image description here

here is the url page code:

<?php
include 'config.php';


    $itemShape = $_POST["itemShape"];

    $mysql="SELECT product_id,item_shape from product_table where item_name="."'$itemShape'";
    //echo $mysql;
    $count=0;
    $myquantity = $conn->query($mysql);
    $new_array=array();
if ($myquantity->num_rows > 0) {
        while($myrow = $myquantity->fetch_assoc()) {
            $quantity_sum['count']=$count+1;
            $quantity_price['item_name']= $myrow['item_shape'];
            $quantity_price['item_id']= $myrow['product_id'];
            array_push($new_array,$quantity_price);


        }
    }
    $new_item=array_merge($quantity_sum,$new_array);
    echo json_encode($new_item);
?>

this is the ajax method which is used:

$('#item_name').change(function(){
        //alert();
        var Item_Name=$("#item_name option:selected").text();
        alert("item_name");
         $.ajax({
         type: 'POST',
         url: 'get_item_types_dhk.php', 
         data: {itemShape: Item_Name},
         dataType: "json",
         success: function(data){ 

Someone please help me how to solve this issue

toha
  • 5,095
  • 4
  • 40
  • 52

1 Answers1

0

You are sending your data as a json without setting contentType to json.

Solution 1: send you data in the default format:

Instead of data: {itemShape: Item_Name},

do data: "itemShape=" + Item_Name,

Solution 2: set content type for json:

Setting dataType to json is meant fo the data received and not the data you are sending; for setting the data type of the data you are sending you use contentType like this:

$.ajax({
  type: 'POST',
  url: 'get_item_types_dhk.php',   
  contentType: "application/json; charset=utf-8", //set data to be json
  // data: {itemShape: Item_Name}, // before
  data : JSON.stringify({itemShape: Item_Name}), //after
  dataType: "json", // this is the returned data type...
  success: function(data)

Read a more detailed answer the difference between dataType and contentType over here What is content-type and datatype in an AJAX request?

Fanckush
  • 143
  • 3
  • 10
  • The default content type is `"application/json; charset=utf-8"` – JYoThI Aug 10 '17 at 10:32
  • @JYoThI even if it is, he is not converting the json to a string with `JSON.stringify `, plus it is better to explicitly set the contextType to reduce the chances of errors – Fanckush Aug 10 '17 at 10:35
  • @JYoThI i just looked it up to be sure and according to jQuery documentation [link](http://api.jquery.com/jquery.ajax/): `'application/x-www-form-urlencoded; charset=UTF-8'` is the default contentType – Fanckush Aug 10 '17 at 10:40