1

I am trying to pass data to php code using ajax. but I cant get is success.

<?php


$message = $_POST["message"];
 $buyer_name = $_POST["buyer_name"];
  $order_number =  $_POST["order_number"];
   $account = $_POST["account"];
    $designer = $_POST["designer"];
     
echo $message; ?>

this is my js code.

var formdata = {buyer_name:byrName,order_number:orderNum.trim(),account:account,designer:'admin',message:'testing'}
    
    if(autoMode){
            
        $.ajax({

            type:'POST',
            url: 'msgHandle.php',
            data: formdata,
            contentType: false,
            cache: false,
            processData: false,
            beforeSend: function() {

                
            },
            success: function(data) {
                alert(data);


            },
            error: function() {
                alert('failed');

            }

        });

I have set a button to click and when clicked in runs this ajax code. the output is a alert with empty message. that means it success but seems like variables does not pass correctly to the php code. what is wrong in my code, I can't find.

7
  • 1
    Get rid of processData: false. That's only used when you're passing a FormData object, not a plain object. Commented Jul 13, 2020 at 18:05
  • 1
    Also delete contentType: false. Commented Jul 13, 2020 at 18:05
  • did it. but still getting empty alert Commented Jul 13, 2020 at 18:10
  • 1
    Put var_dump($_POST); in the PHP script. Commented Jul 13, 2020 at 18:12
  • 2
    And check the network tab in the development pane of your browser to check the correct data is being sent to your script Commented Jul 13, 2020 at 18:22

1 Answer 1

1

I tried your code and found that by removing all the three parameters

 contentType: false,
 cache: false,
 processData: false,

From the code posts your data onto another page.

Tried for just a sample array.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.