如何使用javascript从外部文件加载php变量

时间:2015-11-22 20:12:46

标签: javascript php mysql variables

我在显示信使的页面<section id="parent"> <header>Parent flex header </header> <section id="child" > <header>Child flex header</header> <article> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> <p>Lots of content.</p> </article> <footer>Child flex footer</footer> </section> <footer>Parent flex footer</footer> </section>内有此代码......

PHP:

<div id="chtmsg">

效果很好。所以,我想用js加载它来刷新div #chtmsg内的所有新消息,然后我创建了一个文件msg.php并且if($perguntas){ for($c=0;$c<count($perguntas);$c++){ $perguntas[$c]->tipo == 'F' ? $class = 'message_F' : $class = 'message_P'; $hora = substr($perguntas[$c]->hora, 0, 5); echo "<li class=\"".$class."\"><p>".$perguntas[$c]->mensagem."</p><span>".$pergunta->databr($perguntas[$c]->data)." - ".$hora."</span></li>"; if($perguntas[$c]->tipo=='F' and $perguntas[$c]->status == 0){ $pergunta->marcaRespLida($perguntas[$c]->id); } } } 它继续工作正常,但是使用js我需要放入路径...

HTML:

<?php include("msg");?>

但是它显示了de div中的错误......

  

注意:未定义的变量:第3行的/Applications/XAMPP/xamppfiles/htdocs/sisconbr-sistema-novo/site/modulos/produto/msg.php中的perguntas

我测试了msg.php文件中的其他代码,没有变量就可以正常工作......

2 个答案:

答案 0 :(得分:0)

只是一个想法...

PHP的第一行

if($perguntas){

或许应检查是否定义如此

if(isset($perguntas)){

我的建议在另一个答案中解释here

答案 1 :(得分:0)

为了获得更好的代码,您最好使用:

class MSC_CLItem_Tizer : MSC_CLItem
{
    var Title:String!
    var Video:MSC_CLItem_TizerVideo!
    var Detail:MSC_CLItem_TizerDetail!

    private init(title:String!)
    {
        super.init()
        self.Title = title
        super.Type = .Tizer
    }

    override func RenderUI(Point:CGPoint) -> UIView
    {
        let v = UIImageView()
        v.backgroundColor = UIColor.yellowColor()
        let screenSize: CGRect = UIScreen.mainScreen().bounds
        v.frame.size = CGSize(width: screenSize.width - 10, height: CGFloat(160))
        v.frame.origin = Point

        let vid = UIImageView()
        vid.image = UIImage(named: "default")
        vid.backgroundColor = UIColor.grayColor()
        vid.contentMode = .ScaleAspectFill
        vid.af_setImageWithURL(NSURL(string: "Image HTTP url")!)
        vid.frame.size = v.frame.size
        vid.frame.origin = CGPoint(x: 0, y: 0)
        vid.clipsToBounds = true
        vid.userInteractionEnabled = true
        v.userInteractionEnabled = true
        let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
        vid.addGestureRecognizer(tapRecognizer)
        v.addSubview(vid)
    }

    func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
        //Not detected to here
    }
}