donglusou3335 2011-12-05 22:52
浏览 79
已采纳

使用php中的ajax检索数据

I have an ajax code which passes data to php and then retrieves data from php...

function updateDatabase(syncData){
    $.ajax({
            type : 'POST',
            async: false,
            url : 'php/syncData.php',
            dataType : 'json',
            data: {myData : syncData}, //i even used this "mydata"+syncData
            success : function(result){
                res = result;                               
            },
            error : function() {
                res = false;
            }
    });

    return res;
}

And here's my php code...

<?php

    include("database.php");

    if(isset($_GET['myData'])){
        $data = $_GET['myData'];

        $insert = "INSERT INTO `mydata` VALUES (NULL,'$data')";
        mysql_query($insert);


       $data = mysql_insert_id();
       echo json_encode($data);

}

?>

Here's javascript function that pass/calls ajax...

<script>

var theData = "blahblah";
var alertData = updateDatabase(theData);

alert(alertData);

</script>

the problem is that when i use htmt/text as data type it alerts empty/blank output, but when i used json as data type it alerts false where i conclude it went to error function not to success... anyone knows what's the proper way to do this? or maybe i'm just missing something? Thanks!

  • 写回答

2条回答 默认 最新

  • doukengzi3517 2011-12-05 22:59
    关注

    Well, you're posting the data using jQuery, but you're looking for a $_GET value in the PHP. Fix that and see if it changes anything.

    Edit: It should look like this:

    ...
        if(isset($_POST['myData'])){
            $data = $_POST['myData'];
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度