【问题标题】:Posting Variables from jQueryUI Dialog to PHP with AJAX使用 AJAX 将 jQueryUI 对话框中的变量发布到 PHP
【发布时间】:2015-05-04 12:42:27
【问题描述】:

我正在尝试从对话框窗口中的输入文本字段中获取输入,以将它们用于 SQL 查询。

我的问题是我不能在 PHP 中使用数组。我没有收到任何错误或成功消息。 在 Firefox 的网络日志中,我可以看到具有正确值的 Post 方法。

这是我的 js 代码的 sn-p:

<script>
    // open popup window "dialog"
        $(function() {
            $( "#dialog" ).dialog({
                autoOpen: false ,
                width: 1000,
                buttons: {
                    "Quelle hinzufügen": function() {                           

                        var out = [document.getElementById("titelin"),document.getElementById("autorin"),document.getElementById("tagsin"),document.getElementById("linkin")];
                        var outv = [out[0].value,out[1].value,out[2].value,out[3].value];                       
                        ajax(outv);
                    },
                Abbrechen: function() {
                  $( this ).dialog( "close" );
                }   
                }   
            }); 
            $( "#opener" ).click(function(e) {
                 e.preventDefault();
                $( "#dialog" ).dialog("open");
            });
        });
        // posting to PHP
        function ajax(outv){

            $.ajax({
                type:"POST",
                url:"quellenverzeichnis.php",
                data: {output: outv},               
                sucess: function(){
                    alert("works");
                },
                error:  function(){
                    alert("fail");
                }
            });
        }; 
    </script>

这是我的 PHP 代码:

<?php

if (isset($_POST['output'])){
    hinzufuegen();
};
printf($_POST['output']);

?>

不知道我做错了什么,并为我糟糕的英语和代码中的德语单词感到抱歉。 感谢您的帮助

【问题讨论】:

  • data: {output: bla}, 这个 bla 是从哪里来的?
  • 为什么您的请求会变为空白,因为未定义的bla 不会发送output 字段
  • printf($_Post['output']);错了你应该使用 printf($_POST['output']);
  • 对不起,bla 的东西只是一个字符串而不是数组的测试
  • 请输入实际代码,这样我们就不必朝不同的方向挖掘了

标签: javascript php ajax jquery-ui dialog


【解决方案1】:

使用下面的代码sn-ps

         buttons: {
              "Quelle hinzufügen": function() {
                 var out = [];
                 out.push(document.getElementById("titelin").value);
                 out.push(document.getElementById("autorin").value);
                 out.push(document.getElementById("tagsin").value);
                 out.push(document.getElementById("linkin").value);
                 ajax(out);
         },

        function ajax(info){

            $.ajax({
                type:"POST",
                url:"quellenverzeichnis.php",
                data: {output: info},                
                success: function(data){
                    alert("works"+data);
                },
                error:  function(){
                    alert("fail");
                }
            });
        };

        $_POST['output']

【讨论】:

  • 感谢您的帮助,它可以正常工作...但我的问题是我希望在网站上打印,但没关系
  • 你试过回声吗?我又更新了代码,现在试试这个
  • 没有弹出成功消息,但是我在php中得到了数组并且可以执行sql请求
  • sucess 回调的代码中有错字,应该是success,请尝试我的新更新代码
猜你喜欢
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
相关资源
最近更新 更多