douliao8402 2015-12-02 11:03
浏览 51
已采纳

jQuery $ .post无法处理特定条件

I'm trying to insert some data into mySQL database using AJAX and jQuery $.post. My attempt to test the functionality is as follows

index.html: code fragment (already using jQuery for many other functions)

$.post( 
    "updateDatabase.php",
    { name: "Zara" },
    function(data) {
         //$('#stage').html(data);
         alert('Success');
    }
);

updateDatabase.php

<?php
if( $_REQUEST["name"] ) {

   $name = $_REQUEST['name'];
   echo "Welcome ". $name;
   echo "<script>alert('Im here')</script>";
}
?>

What I'm getting is an alert with "Success" and nothing else(It is the callback function of $.post). I already researched a lot and found some similar but different questions. However my problem left unsolved because there is no satisfactory solution for this particular issue.

  • 写回答

3条回答 默认 最新

  • dougong7850 2015-12-02 11:07
    关注

    You are echoing a string echo "alert('Im here')";. so,

    1. It can't be treated as a javascript function.
    2. You are not using the response in the success callback.

    What you can do is:

    function(data) {
         alert('Success: '+data);
         // outputs: Success: Welcome Zara I'm here
    }
    

    and at php:

    echo "Welcome ". $name . " I'm here";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)
  • ¥15 主要进行描述泥浆在管路不同区段泥浆的密度不相同,泥浆的密度有高有低,此时管路的摩阻分布需要怎么计算,(标签-matlab)
  • ¥40 通过编制程序计算圆管内层流充分发展对流换热,参数如图5-4,以及公式5-16所示,要求用表5-6对程序计算准确性进行验证
  • ¥20 该学习哪个编程语言? AI会取代程序员吗?
  • ¥15 ensp如何拼通IP地址
  • ¥15 saber软件导入Ibis模型报错
  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题