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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了