简体   繁体   中英

send data to php using ajax post method

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.

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM