我想给data-prefilltext
参数仅添加如果$_GET['text'] != "undefined"
按钮。如何实现这一目标?如何在PHP和HTML混合中添加if语句?
HTML:
<button
id="sharebutton"
class="g-interactivepost"
data-prefilltext="<?php echo $_GET['text'];?>"
Send
</button>
我想给data-prefilltext
参数仅添加如果$_GET['text'] != "undefined"
按钮。如何实现这一目标?如何在PHP和HTML混合中添加if语句?
HTML:
<button
id="sharebutton"
class="g-interactivepost"
data-prefilltext="<?php echo $_GET['text'];?>"
Send
</button>
如果你想$_GET['text']
用绳子连接"undefined"
<button
id="sharebutton"
class="g-interactivepost"
<?php if (isset($_GET['text']) && !empty($_GET['text']) && $_GET['text'] !== 'undefined') { ?>
data-prefilltext="<?php echo $_GET['text'];?>"
<?php } ?>
>
Send
</button>
<button>
id="sharebutton"
class="g-interactivepost"
data-prefilltext="<?php if (isset($_GET['text'] && !empty($_GET['text']) { echo $_GET['text']; } ?>"
Send
</button>
这是你以后在做什么?
为了保持HTML清洁,我可能会定义字符串中的实际HTML按钮元素之外,因为这样的:
<?php $prefilltext = isset($_GET['text']) ? 'data-prefilltext="' . $_GET['text'] . '"' : ''; ?>
<button id="sharebutton" class="g-interactivepost" <?php echo $prefilltext;?>>
Send
</button>
PS:我只是检查是否存在$_GET['text']
。当然,您可以更改该选项以检查empty
或任何其他比较,例如$_GET['text'] != 'undefined'
。
你可以做到以下几点:在php.net
<button
id="sharebutton"
class="g-interactivepost"
<?php echo ($_GET['text'] != 'undefined')? "data-prefilltext=\"".$_GET['text']."\" ": "";?>
Send
</button>
<button
id="sharebutton"
class="g-interactivepost"
data-prefilltext="<?php if(isset($_GET['text']) && !empty($_GET['text']) { if($_GET['text'] == "undefined") { echo $_GET['text']; } else { // if text is not equal to undefined }?>"
Send
</button>