1

I am Passing my user information http angularjs. backend code is PHP As I am the beginner I am searching lot for this Issue. and tried a lot methods since 2 days but I couldn't find the reason, and how to fix it?. it may be simple but I couldn't find.

1.I am posting my http post request in angularJS I have been debugged the value which I will send is

Debugging value are as follow: serializedParams:"send_id=78&send_name=Douby&send_phone=4528&send_status=Due"

url: "insert.php?send_id=78&send_name=Douby&send_phone=4528&send_status=Due" result: undefined

I think the url is correct. but the result is undefined

var app = angular.module("myapp", []);
app.controller("booking", function($scope, $http) {
  $scope.paidops = ["Paid", "Due"];
  $scope.value = "ADD";
  $scope.insertvalues = function() {
    alert($scope.id + ' , ' +
      $scope.name + ' ,' + $scope.phone +
      ' , ' + $scope.status);
    alert($scope.name);
    var Indata = {
      'send_id': $scope.id,
      'send_name': $scope.name,
      'send_phone': $scope.phone,
      'send_status': $scope.status
    };
    $http({
      method: 'POST',
      url: 'insert.php',
      params: Indata,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }).then(function(response) {
      alert(JSON.stringify(response));
    }, function(response) {
      alert(response);

    });
  }
});

In PHP I am getting data like this way:


     $connect = mysqli_connect("localhost:3307", "root", "", "ticket_booking");
     if($connect === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }
    
    $data = json_decode(file_get_contents("php://input"),true);
    if(count(array($data)) > 0)  
     
     {  
      $id_received = mysqli_real_escape_string($connect, $data->send_id);
     $name_received = mysqli_real_escape_string($connect, $data->send_name);
     $phone_received = mysqli_real_escape_string($connect, $data->send_phone);
     $status_received = mysqli_real_escape_string($connect, $data->send_status);
          $btnname_received = mysqli_real_escape_string($connect, $data->send_btnName);
    if($btnname_received == 'ADD'){
          $query = "INSERT INTO society_tour(id,name, phone, status) VALUES ('$id_received','$name_received', '$phone_received','$status_received')";  
          if(mysqli_query($connect, $query))  
          {  
               echo "Data Inserted...";  
          }  
          else  
          {  
               echo 'Error';  
          }  
         }
     ?>

  


2
  • need php part than handle this. We can't answer like this. For instance I see you do a POST but pass the value in the query string, are you sure you're reading the query string on the php side and not the request body ? (request body should be "data" parameters in angularJS from a 4 years old memory) Commented Jul 19, 2021 at 14:22
  • @Walfrat I have added my PHP code. . pls look at it. . Commented Jul 20, 2021 at 7:39

1 Answer 1

1

Not entirely sure about the PHP part, but as you have json_decode in PHP, its safe to assume that PHP expects a JSON content-type

If so, here is how to post data to a url

var postUrl = 'insert.php'; // please check whether the url is correct
var dto = {
  'send_id': $scope.id,
  'send_name': $scope.name,
  'send_phone': $scope.phone,
  'send_status': $scope.status
};
$http({
  url: postUrl,
  method: 'POST',
  data: dto,
  headers: {
    "Content-Type": "application/json"
  }
})
//...
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks!! It is giving the data but the data is with escape character . .like {"data":" string(74) \"{\"send_id\":\"45\",\"send_name\":\"Podhy\",\"send_phone\":125,\"send_status\":\"Paid\"} In my PHP data is null. I have added my php full code.Is it this escape character is the reason I didn't get the data in PHP?
I'm not sure that the JSON.stringify is necessary, @Pavi_Web did you try with data:dto simply ?
Thanks It is working now!! instead of data: JSON.stringify(dto) just tried data:dto Thanks @Naveen ,@Walfrat
Updated answer... Havent used angularjs for a bit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.