[英]Modify PHP file after including it
I'm new to PHP but I think it's possible, thought unsure how to achieve this. 我是PHP的新手,但我认为有可能,不确定如何实现这一点。
I've got a basic HTML markup inside a PHP file called button.php which I'm injecting into a web page using PHP include. 我在一个名为button.php的PHP文件中有一个基本的HTML标记,我正在使用PHP include将其注入到网页中。
Here's the contents of the button.php file: 这是button.php文件的内容:
<div class="btn">
<div class="highlightBtn">
<div class="btnTag">
Submit
</div>
</div>
</div>
The question is - how do I the value inside the class="btnTag" tags AFTER I've included it? 问题是-在我将class =“ btnTag”标签中的值包含进去之后,该如何处理? (Ie after I include this file into the main page I want to be able to change it depending on where it's included, so it doesn't always have to say 'Submit'). (即,在将该文件包含到主页中之后,我希望能够根据包含文件的位置对其进行更改,因此不必总是说“提交”。)
So I imagine it might be something like 所以我想可能是这样的
<? php include("button.php");
//some code follows to change value inside the btnTag class
?>
Thanks for your help! 谢谢你的帮助!
It really should be done beforehand, as in 确实应该事先完成,例如
// button.php
<div class="btn">
<div class="highlightBtn">
<div class="btnTag">
<?php echo isset($caption) ? $caption : 'Submit' ; ?>
</div>
</div>
</div>
And then 接着
<?php
$caption = 'Click me'; // or comment out to get the default
include('button.php');
?>
It would be much better to have the file just define a function when included, which you can then call whenever you need a button. 最好在文件包含时定义一个函数,然后在需要按钮时可以调用该函数。 For example: 例如:
// button.php
<?php
function button($caption = 'Submit') {
echo <<<END_HTML;
<div class="btn">
<div class="highlightBtn">
<div class="btnTag">$caption</div>
</div>
</div>
END_HTML;
}
You can then include button.php
without any immediate effects, and whenever you want a button you can just call button('Click me')
or just button()
. 然后,您可以包含button.php
而不会立即产生任何效果,并且每当您想要按钮时,都可以仅调用button('Click me')
或button()
。
You should do it before including it: 您应该先包括以下步骤:
$string = 'Submit';
include 'button.php';
and inside button.php: 在button.php内部:
<div class="btn">
<div class="highlightBtn">
<div class="btnTag">
<?php echo $string; ?>
</div>
</div>
</div>
If you want to change it after the page load then you should look at AJAX . 如果要在页面加载后进行更改,则应查看AJAX 。 Other solutions (for dealing with it after inclusion) would require output buffer handling, DOM handling and other painful routines that are not really an option. 其他解决方案(在包含之后对其进行处理)将需要输出缓冲区处理,DOM处理和其他痛苦的例程,这些并不是真正的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.