简体   繁体   English

我可以在扩展名为.php的同一文件中使用php和jQuery吗

[英]Can I use php and jQuery in the same file with .php extension

I am new at programming websites and have spent the last month learning jQuery and PHP. 我是编程网站的新手,并且上个月一直在学习jQuery和PHP。 I built a website using jQuery, but now realise that i need to use php as well but mainly for the purpose of authentication. 我使用jQuery建立了一个网站,但现在意识到我也需要使用php,但主要是为了进行身份验证。 I don't want to try to redo the jQuery code in php. 我不想尝试在php中重做jQuery代码。

I know that most people recommend separating server-side and browser-side scripts and HTML, but can I use both jQuery and php in the same file for separate purposes with out any conflicts or issues aising? 我知道大多数人建议将服务器端脚本和浏览器端脚本以及HTML分开,但是我可以在同一个文件中同时使用jQuery和php来实现单独的目的,而不会产生任何冲突或问题吗? The file extension will be .php and I expect the structure to be along the lines of: 文件扩展名为.php,我希望其结构符合以下要求:

<?php
//php code for authentication 
?>

<html>
<head>
//header code

<sctipt>
//jquery code to control layout including hiding divs and changing the information 
//shown based on conditional statements

</script>
</head>

//other html

Sorry if this is a dumb question, but I would like to know the best way forward before I get too far. 抱歉,这是一个愚蠢的问题,但是我想知道最好的解决方法,然后再说清楚。 I appreciate your help. 我感谢您的帮助。

Thank you all for your quick responses, much appreciated. 谢谢大家的快速反应,非常感谢。

The purpose of the php is so that only people who are logged in can see the page. php的目的是使只有登录的用户才能看到该页面。 My current understanding is that authentication can only be done by using cookies (still have to learn about sessions) and that these require a php script. 我目前的理解是,只能使用Cookie(仍然必须了解会话)来进行身份验证,并且这些都需要php脚本。 Is there a better method so I don't have to mix the two languages on the one page? 有没有更好的方法,所以我不必在一页上混合两种语言? or is my method easier and equally valid - even though it's bad practice? 还是我的方法更简单且同样有效-即使这是错误的做法?

yes .. you can.. just need to make sure , you have all your jquery(javascript) codes inside the <script> tag.... (though it is a bad pratice but you can )... 是的..您可以..只需确保,您的所有jquery(javascript)代码都在<script>标记内....(尽管这是很糟糕的做法,但是您可以)...

<?php
 $test="test";
?>

<script>
   var data= '<?php echo $test ?>';
   alert(data);
</script>

Yes. 是。 This is possible. 这个有可能。 You'd write it just like you gave an example. 您将像编写示例一样编写它。 Just test it out ;-) 只是测试一下;-)

The answer is yes, you can. 答案是可以的。 PHP is a method of dynamically deciding what mark-up (HTML) to send to the browser, so PHP and HTML are generally intrinsically linked. PHP是一种动态决定发送给浏览器的标记(HTML)的方法,因此PHP和HTML通常是固有链接的。 Furthermore JavaScript is generally intrinsically linked in HTML. 此外,JavaScript通常在HTML中固有地链接。 Note that this all happens independently of what file extension it came from - your web server sends the result of the script you write, not any file of a given extension. 请注意,这一切都与文件扩展名无关-网络服务器发送您编写的脚本的结果,而不是给定扩展名的任何文件。

The principle being that it doesn't matter how the mark-up is generated, once it gets to the browser, it will be executed. 原理是,标记的生成方式无关紧要,一旦标记到达浏览器,它将被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM