PHP + JQuery-如何一起使用这两个?请看我的例子 [英] PHP + JQuery - How to use these two together? Please see my example

查看:79
本文介绍了PHP + JQuery-如何一起使用这两个?请看我的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于php的网站.与之类似,所有html页面都是通过php输出的.

I have a php based website. As in, all of the pages html is output via php.

这是一个简单的例子:

<?php
ob_start();

$pageStart = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MY WEBSITE PAGE</title>
</head>
<body>
<p>CONTENT</p>
</body>
</html>
';

echo $pageStart;
exit;
?>

我想做的是利用此页面中的一些jquery.

What i would like to do is make use of some jquery in this page.

自然地,我的第一个尝试是将脚本包含在php变量中,如下所示:

So naturally my first attempt was to include the script inside of the php variable like so:

<?php
ob_start();

$pageStart = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">
    var datefield=document.createElement("input")
    datefield.setAttribute("type", "date")
    if (datefield.type!="date"){ //if browser doesn't support input type="date", load files for jQuery UI Date Picker
        document.write('<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><\/script>\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"><\/script>\n')
    }
</script>

<script type="text/javascript">
if (datefield.type!="date"){ //if browser doesn't support input type="date", initialize date picker widget:
    jQuery(function($){ //on document.ready
        $('#birthday').datepicker();
    })
}
</script>

</head>
<body>
<p>CONTENT</p>
</body>
</html>
';

echo $pageStart;
exit;
?>

现在,我使用Dreamweavers代码编辑器,由于其语法高亮功能,它将指出由此产生的大量语法错误.

Now, I use dreamweavers code editor, which due to the syntax highlighting feature, will point out the masses of syntax errors produced in this.

因此,我起初试图消除这些错误.这失败了.

So i at first attempted to slash out these errors. This failed.

因此,我尝试将"s"更改为,反之亦然,直到错误消失.这也失败了,因为脚本似乎无法以这种方式进行验证.

So i tried changing the "s to 's, and visa versa, until the errors were gone. This failed too as it seems the script will not validate in this manner.

因此,我阅读了一些教程,并发现了其中一个:

So i read a few tutorials, and found this one:

JavaScript和PHP可能为任何Web开发工作带来巨大潜力,但是他们并不总是在一起玩的很好.了解问题所在.

据我了解,您需要包含js,而不是直接将其作为php的一部分.

And the way I understand it, is that you'd need to include the js rather than have it as part of your php directly.

因此有一个名为page.php的文件和另一个名为jquery.php的文件.因此,我决定尝试修改此想法以适合我的问题.

So have a file called page.php and another called jquery.php. So I decided to try and modify this idea to suite my problem.

所以我从这样的东西开始-index.php:

So i started with something like this - index.php:

<?php
ob_start();

$pageStart = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MY WEBSITE PAGE</title>
'. include $_SERVER['DOCUMENT_ROOT'] . '/../../path/to/include/datepicker.php'.'
</head>
<body>
<p>CONTENT</p>
</body>
</html>
';

echo $pageStart;
exit;
?>

还有-datepicker.php:

And - datepicker.php:

<script language="Javascript">
    var datefield=document.createElement("input")
    datefield.setAttribute("type", "date")
    if (datefield.type!="date"){ //if browser doesn't support input type="date", load files for jQuery UI Date Picker
        document.write('<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><\/script>\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"><\/script>\n')
    }
</script>
<script language="Javascript">
if (datefield.type!="date"){ //if browser doesn't support input type="date", initialize date picker widget:
    jQuery(function($){ //on document.ready
        $('#birthday').datepicker();
    })
}
</script>

现在在任何一页中都没有语法错误,太好了...所以我尝试了一下,一半期望事情终于可以解决...

Now there are no syntax errors in either page, great... So I try it, half expecting things to finally just work...

不,...致命错误:无法打开流:没有这样的文件或目录.此错误与我添加的include有关.好的,即使文件存在,也不会因为没有打开和关闭php标签而被验证为php.

Nope... Fatal Error: failed to open stream: No such file or directory blah blah. This error relates to the include that i added. So okay, even though the file is there, it is not being validated as php because there are no opening and closing php tags.

所以我添加它们... Ann ...再次充满语法错误的页面!

So I add them... Annnd... A page full of syntax errors again!

所以我的问题是这个

可以请一些人分享一些知识并向我粗略地解释一下此过程的实际工作方式.所有语法错误如何处理?您应该如何执行这样的任务?

Could some please share some knowledge and explain to me roughly how this process actually works. Whats the deal with all the syntax errors, and how are you supposed to go about a task like this?

我相信这是可能的,是的,如果我不通过php输出整个html,那么肯定会容易一些.但这是一个简单的示例,我的实际设计要复杂得多.它要求将页面的不同部分分解为变量,以便在需要时动态地放置这些位和对象.

I believe it is possible, and yes it would ceratinly be easier if i was not outputting the entire html via php. But this is a simple example and my actual design is alot more complex. It requires for the different parts of the page to be broken up into variables, in order to place those bits and peices when needed, dynamically.

任何意见,建议或见解将不胜感激;以及涉及此内容的页面或教程的任何链接也将不胜感激.

Any input, suggestions, or insight would be greatly appreciated; and any links to pages or tutorials that cover this would also be greatly appreciated.

谢谢!

推荐答案

将jQuery与PHP结合使用所需要做的就是在head文档的HTML文档中包含jQuery javascript文件.实际上,我一直都在将PHP和jQuery一起使用.这就是我的方法.在上面的代码中,您的代码似乎存在一些转义问题.而且看起来您还想将页面的标题保留在PHP变量中,然后将其打印出来.您不必做所有的事情.只需将纯文本放入没有任何php标记的PHP文件中,它将起作用.另外,您正在使用旧版本的jQuery.应该使用最新版本.但是,如果需要将其存储在PHP变量中以便可以将其打印出来,请执行以下操作:

All you need to do to use jQuery with PHP is to include the jQuery javascript file in your HTML document in the head tag. I actually use PHP along with jQuery all the time. And this is how I do it. In your code above, it looks like you have some escaping issues with your code. And it also looks like you want to hold the header of the page in a PHP variable then print it out. You don't have to do all that. Just put the plain text in your PHP file without any php tags and it will work. Also, you are using an old version of jQuery. Should probably use the latest version. But, if you need it stored in a PHP variable so that you can print it out, do this:

所以,这里有一些代码可以帮助您入门.

SO, here is some code to get you started.

<?php
$pageStart = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MY WEBSITE PAGE</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
    $("#date").datepicker();
});
</script>
</head>
<body>
<input type="text" id="date" name="date" />
</body>
</html>';

print $pageStart;

?>

这篇关于PHP + JQuery-如何一起使用这两个?请看我的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆