【问题标题】:Undefined index in php css with ajax post带有ajax帖子的php css中未定义的索引
【发布时间】:2013-06-13 13:59:38
【问题描述】:

过去五天我一直在尝试解决这个问题,但没有任何效果。我已经查看了有关该主题的每一篇文章,但没有任何帮助。我正在使用 OpenCart CMS,并希望使用 ajax 将一个简单的 jquery 变量从一个文件发送到另一个文件并发布,以便它可以更改 css 文件的背景颜色。要弄清楚为什么该文件不带有 .php 扩展名,请阅读本教程http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/,尤其是优雅的方法。所以这是ajax的代码

$(document).ready(
    function() 
      {
        var headerDefaultColor = "#126FA8";
      $.ajax({
        url:  "view/stylesheet/supercharge.css",
        data: {cell: headerDefaultColor},
        type: "POST",
        async: false,
        success: function(data) {
          console.log("Send");
        },
        error: function(data){
          console.log("Error");
        } 
        })
        .done(function(cell) { console.log("Done success: " + cell); })
        .fail(function() { console.log("error"); })
        .always(function() { console.log("complete"); })
        });

这是css的代码

    <?php header("Content-type: text/css"); ?> 

<?php
    $headerColor = null;
    var_dump($_POST['cell']);
    if(isset($_POST['cell'])){
        $headerColor = $_POST['cell'];
    } else {
        $headerColor = "Nothing happends, again!!"; 
    }
?>

#header {
    background-color: <?php echo $headerColor;  ?>;
}

这是我在控制台中得到的:

    Send index.php:103
Done success:  

    <pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'#126FA8'</font> <i>(length=7)</i>
    </pre>
    #header {
        background-color: #126FA8;
    }

但是,这是我在 css 文件中得到的。它应该发送十六进制代码,但它没有,它生成了一个通知:未定义索引:单元格,这是输出:

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: cell in C:\Program Files (x86)\wamp\www\opencart-1.5.5.1\upload\admin\view\stylesheet\supercharge.css on line <i>14</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>250160</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\Program Files (x86)\wamp\www\opencart-1.5.5.1\upload\admin\view\stylesheet\supercharge.css' bgcolor='#eeeeec'>..\supercharge.css<b>:</b>0</td></tr>
</table></font>
<pre class='xdebug-var-dump' dir='ltr'><font color='#3465a4'>null</font>
</pre>
#header {
    background-color: Nothing happends, again!!;
}

我尝试使用 isset() 但它不起作用所以我希望将 var headerDefaultColor 发送到 .css 文件并使用 php 而不是使用 jquery 更改背景颜色(效果很好)。我在这里错过了什么?

【问题讨论】:

  • 你的意思是这样做吗? $( "#header" ).css( "background-color", headerDefaultColor )
  • 我不想用 JQuery 完成它,因为那不是重点。颜色应该通过 php 变量来改变。我知道 jquery 工作正常。

标签: php css ajax content-management-system jquery-post


【解决方案1】:

把你的 PHP 文件改成这个

<?php 

header("Content-type: text/css"); 

// checks if cell is received from GET or POST, if not assign default value as #fff
$headerColor = isset($_REQUEST['cell']) ? $_REQUEST['cell'] : '#fff';

?>

#header {
    background-color: <?php echo $headerColor;  ?>;
}

当你调用 CSS 文件时,如下调用它

&lt;link href='view/stylesheet/supercharge.css?cell=#F00' rel='stylesheet' type='text/css' /&gt;

【讨论】:

  • 不工作我也尝试了 $_REQUEST,上面的代码在其他一些帖子中它与我的代码基本相同。
  • 是否有指向您工作网站的链接?你是怎么得到最后一个输出的?如果 .htaccess 文件设置正确,PHP 文件应正确捕获 GET 变量。
  • 它是一个本地主机,并且在安装过程中默认设置了所有内容。我没有对主 .htaccess 文件进行任何更改,除了我在样式表文件夹 AddHandler application/x-httpd-php .css 中添加了另一个 .htaccess。
【解决方案2】:

您应该将您的 url 更改为 php 文件,而不是 .css,因为您将在 php 文件中而不是在 css 文件中获得请求

url:  "view/stylesheet/supercharge.php

【讨论】:

  • 也试过了,当用 cosole 查看时,我仍然得到未识别的索引:css/php 文件中的单元格。要了解它是如何工作的,请阅读链接教程。
  • 我改css成功了,但是浏览器好像没有使用新值。
  • 你检查路径了吗?意味着您在文件夹视图/样式表中有名为 supercharge.php 的文件?
【解决方案3】:

确保您没有及时向同一个 url 发送两个或多个 ajax 请求,因为其中一个可以覆盖从第二个发送的数据。 例如:

function first_ajax(searchQuery) {
         $.ajax({
                url: '/search/index.php',
                type: 'post',
                data: {query: searchQuery},
                dataType: "JSON",
                success: function (data) {
                    console.log(data.objD);
                }
            });
};

function second_ajax(searchQuery) {
              first_ajax(searchQuery);    
              $.ajax({
                        url: '/search/index.php',
                        type: 'post',
                        data: {query:searchQuery, paging:paging, start:start},
                        dataType: "JSON",
                        beforeSend: function() {
                            loader.show();
                        },
                        complete: function() {
                            loader.hide();
                        },
                        success: function(data) {
                            console.log(data);
                        }
                    });
                };
    }

在这种情况下,从 second_ajax() "Paging" 和 "Start" 发送的数据将是 PHP 的未定义索引,因为它们将被 first_ajax() 覆盖

【讨论】:

    猜你喜欢
    • 2012-05-04
    • 1970-01-01
    • 2019-01-20
    • 2018-01-25
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    相关资源
    最近更新 更多