如何发送带有警报的 jquery $.post

发布于 2024-07-26 21:59:57 字数 2811 浏览 5 评论 0原文

我对以下脚本有问题。 它会生成一个可编辑、可删除的位置列表,您甚至可以创建一个新位置。

我想在创建一个新位置时发送 $.post 请求到 php 文件,该文件在数据库(MySQL)中输入一个条目,然后在 html 中列出该条目。 现在为什么连 $.post 也不发送警报消息来通知我数据已发送?

如您所见,剧本尚未完成,但如果您能帮助我,那就太好了!

JS 脚本

 $(function() {
      $(".edit").click(function() {
        $(this).css("display","none").prevAll(".place_name").css("display","none").prevAll(".inputfield_td").css("display","block").nextAll(".cancel").css("display","block").nextAll(".save").css("display","block").prevAll(".inputfield_td").css("display","block");
      });
      $(".cancel").click(function() {
        $(this).css("display","none").prevAll(".edit").css("display","block").prevAll(".place_name").css("display","block").prevAll(".inputfield_td").css("display","none").nextAll(".save").css("display","none");
      });
      $(".save").click(function() {
        var myvariable1 = $(this).siblings().find("input[type=text]").val();
        var myvariable2 = $(this).prevAll("td:last").attr("id");
        $(this).css("display","none").prevAll(".cancel").css("display","none").prevAll(".edit").css("display","block").prevAll(".place_name").css("display","block").prevAll(".inputfield_td").css("display","none");
        alert("save name: "+myvariable1+" save id: "+myvariable2);    
      });
      $(".delete").click(function() {
        var myvariable3 = $(this).prevAll("td:last").attr("id");
        alert(myvariable3);
      });
      $(".new").click(function() {
        var myvariable4 = $(this).prevAll("input[type=text]").val();
          $.post("place_list.php", {action: "create", name: myvariable4}, function(data){
            alert("Data Loaded: " + data);
          },"html");
        alert(myvariable4);
      });
    }); 

PHP 文件

<?php
  require_once "../../includes/constants.php";
  // Connect to the database as necessary
  $dbh = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD)
    or die ("Unaable to connnect to MySQL");

  $selected = mysql_select_db(DB_NAME,$dbh)
    or die("Could not select printerweb");

    echo "<table><tbody>";
    $result = mysql_query("SELECT * FROM place");
    while ($row = mysql_fetch_array($result)) {
      echo "<tr><td id=".$row["id"]." class=inputfield_td><input class=inputfield_place type=text value=".$row["name"]." /></td><td class=place_name>".$row["name"]."</td><td class=edit>edit</td><td class=cancel>cancel</td><td class=delete>delete</td><td class=save>SAVE</td></tr> \n";
    }
    echo "</tbody>";
    echo "</table>";
    echo "<input type=text class=inputfield_visible />";
    echo "<button class=new>New</button>";
?>

I have a problem with following script. It generates a list of places which are editable, deletable or you can even create a new one.

I want to send a $.post request when creating a new place to a php file which makes an entry into a database (MySQL) and then lists this entryes in html. Now why doesn't even the $.post send an alert message to notifi me that the data is been send?

The script isn't finished yet how you can see, but it would be great if you could give me a hand!

JS-Script

 $(function() {
      $(".edit").click(function() {
        $(this).css("display","none").prevAll(".place_name").css("display","none").prevAll(".inputfield_td").css("display","block").nextAll(".cancel").css("display","block").nextAll(".save").css("display","block").prevAll(".inputfield_td").css("display","block");
      });
      $(".cancel").click(function() {
        $(this).css("display","none").prevAll(".edit").css("display","block").prevAll(".place_name").css("display","block").prevAll(".inputfield_td").css("display","none").nextAll(".save").css("display","none");
      });
      $(".save").click(function() {
        var myvariable1 = $(this).siblings().find("input[type=text]").val();
        var myvariable2 = $(this).prevAll("td:last").attr("id");
        $(this).css("display","none").prevAll(".cancel").css("display","none").prevAll(".edit").css("display","block").prevAll(".place_name").css("display","block").prevAll(".inputfield_td").css("display","none");
        alert("save name: "+myvariable1+" save id: "+myvariable2);    
      });
      $(".delete").click(function() {
        var myvariable3 = $(this).prevAll("td:last").attr("id");
        alert(myvariable3);
      });
      $(".new").click(function() {
        var myvariable4 = $(this).prevAll("input[type=text]").val();
          $.post("place_list.php", {action: "create", name: myvariable4}, function(data){
            alert("Data Loaded: " + data);
          },"html");
        alert(myvariable4);
      });
    }); 

PHP-File

<?php
  require_once "../../includes/constants.php";
  // Connect to the database as necessary
  $dbh = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD)
    or die ("Unaable to connnect to MySQL");

  $selected = mysql_select_db(DB_NAME,$dbh)
    or die("Could not select printerweb");

    echo "<table><tbody>";
    $result = mysql_query("SELECT * FROM place");
    while ($row = mysql_fetch_array($result)) {
      echo "<tr><td id=".$row["id"]." class=inputfield_td><input class=inputfield_place type=text value=".$row["name"]." /></td><td class=place_name>".$row["name"]."</td><td class=edit>edit</td><td class=cancel>cancel</td><td class=delete>delete</td><td class=save>SAVE</td></tr> \n";
    }
    echo "</tbody>";
    echo "</table>";
    echo "<input type=text class=inputfield_visible />";
    echo "<button class=new>New</button>";
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-08-02 21:59:57

你安装了firefox和firebug吗?
如果是这样,您可以查看网络选项卡并查看是否向 place_list.php 发出了发布请求以及响应是什么。 我怀疑这是 404'ing,因为路径可能不正确。 您始终可以使用 .ajax 方法而不是 .post。 这允许您指定在 ajax 请求失败时调用的错误方法。

例如

$.ajax({
  url : "place_list.php",
  data : {action: "create", name: myvariable4},
  cache : false,
  error : function(XMLHttpRequest, textStatus, errorThrown){
     //console.log or alert error
     alert(errorThrown);
  },
  success: function(html){
     alert("Data Loaded: " + data);
  }
});

Have you got firefox and firebug installed?
If so you can view the net tab and see if the post request gets made to place_list.php and also what the response is. I suspect it is 404'ing as the path maybe incorrect. You can always use the .ajax method rather than .post. This allows you to specify an error method that gets called upon a failed ajax request.

e.g

$.ajax({
  url : "place_list.php",
  data : {action: "create", name: myvariable4},
  cache : false,
  error : function(XMLHttpRequest, textStatus, errorThrown){
     //console.log or alert error
     alert(errorThrown);
  },
  success: function(html){
     alert("Data Loaded: " + data);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文