【问题标题】:jQuery UI dialog form - Missing variable valuesjQuery UI 对话框表单 - 缺少变量值
【发布时间】:2012-08-28 13:35:05
【问题描述】:

在我的网站上,我正在使用 jQuery ui 框架制作一个对话框。

对话框请求一个主题、评论内容、名称和城市,这些应该插入到 mySQL 中。但是,当我想将变量传递给 PHP 时,变量中没有值。

这是处理对话框的整个js函数:

$( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 600,
    width: 550,
    modal: true,
    buttons: {
        "Skriv Review": function() {
            var bValid = true;
            allFields.removeClass( "ui-state-error" );

            bValid = bValid && checkLength( topic, "topic", 3, 16 );
            bValid = bValid && checkLength( review, "review", 1, 10000 );
            bValid = bValid && checkLength( name, "name", 1, 25 );
            bValid = bValid && checkLength( city, "city", 1, 16 );

            bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Bruger skal være a-z, 0-9, underscores, begynde med et bogstav." );

            if ( bValid ) {
                $( "#users tbody" ).append( "<tr>" +
                    "<td>" + topic.val() + "</td>" + 
                    "<td>" + review.val() + "</td>" + 
                    "<td>" + name.val() + "</td>" +
                    "<td>" + city.val() + "</td>" +
                "</tr>" ); 

                var data = {
                    topic: topic.val(),
                    review: review.val(),
                    name: name.val(),
                    city: city.val()
                }

                $.ajax({
                    type : 'POST',
                    url : 'bruger-reviews.php',
                    dataType : 'json',
                    data: data,
                    success : function(data){
                        //success
                    }
                });
                $( this ).dialog( "close" );
            }
        },
        Cancel: function() {
            $( this ).dialog( "close" );
        }
    },
    close: function() {
        allFields.val( "" ).removeClass( "ui-state-error" );
    }
});

这是我在 PHP 中接收数据的方式:

$ins_topic = $_POST['topic'];
$ins_review = $_POST['review'];
$ins_name = $_POST['name'];
$ins_city = $_POST['city'];

我没有问题像在 jQuery UI 的演示中那样显示值。

当我 $_GET 或 $_POST 变量时没有数据。为什么我无法将值传递给 PHP?有没有其他方法可以试试?

谢谢。

【问题讨论】:

  • 你能附上你的完整的javascript代码吗?因为你的问题还不清楚,如有必要也附上你的php
  • bungdito,我已经附上了整个js函数和PHP。
  • 我尝试使用json_decode方法访问数据,但还是没有。

标签: php javascript ajax jquery-ui dialog


【解决方案1】:

您应该在关闭对话框之前将值传递给一个对象或变量。

此代码从 DOM 中删除了删除对话框; $( this ).dialog( "close" );

使用这个;

if ( bValid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + topic.val() + "</td>" + 
"<td>" + review.val() + "</td>" + 
"<td>" + name.val() + "</td>" +
"<td>" + city.val() + "</td>" +
"</tr>" ); 
var data = {
    topic: topic.val(),
    review: review.val(),
    name: name.val(),
    city: city.val()
}
$( this ).dialog( "close" );

比在 ajax 请求上使用这个数据对象

$.ajax({
                        type : 'POST',
                        url : 'bruger-reviews.php',
                        dataType : 'json',
                        data: data,
                        success : function(data){
                            //success
                        }
                    });

【讨论】:

  • 似乎仍然无法接收 PHP 中的值。 ajax 函数运行后是否需要刷新页面才能执行 $_POST?
  • 刷新页面是违反使用ajax的主要原因。你能在 $.ajax 语句之前 console.log(data) 吗?并尝试在 php 上回显这个请求; $.ajax({type:'post', url : 'bruger-reviews.php',data:{test:'test'},success: function(res){console.log(res);}})跨度>
猜你喜欢
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
相关资源
最近更新 更多