℡Wang Yan 2013-07-07 17:13 采纳率: 100%
浏览 14

发送数据ajax php

Trying to send a variable with ajax to php.

The js:

var test1 = "test"
$.ajax({
    type : "POST",
    url : "getid.php",
    data:  {test1 : test1},
    success: function() {
         console.log("message sent!");
    }
}); 

"message sent!" comes up in the console

The php:

<?php 
$test1 = $_POST['test1'];
echo $test1; 
?> 

Error message:

Notice: Undefined index: test1...

I don't really see what i have done wrong here... Any ideas?

UPDATE* when doing `

$.ajax({    
        type : "POST",
        url : "getid.php",
        data:  {"test1" : test1},
        success: function(msg) {
            console.log("message sent!");
            console.log(msg);
        }
}); 

This logs "test"

Still getting the same error in the php though..

  • 写回答

2条回答 默认 最新

  • weixin_33724059 2013-07-07 17:15
    关注

    Alter you jQuery code:

    var test1 = "test"
    $.ajax({
        type : "post",
        url : "getid.php",
        data:  {"test1" : test1}, // this is the row that was causing the problem
        success: function(msg) {
             console.log(msg);
        }
    }); 
    

    You had to put test1 in quotes because it was a defined variable containing "test" which resulted in data being {"test":"test"}

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型