2013-08-30 17 views
0

我想给data-prefilltext参数仅添加如果$_GET['text'] != "undefined"按钮。如何实现这一目标?如何在PHP和HTML混合中添加if语句?

HTML:

<button 
id="sharebutton" 
class="g-interactivepost" 
data-prefilltext="<?php echo $_GET['text'];?>" 
Send 
</button> 

回答

0

如果你想$_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> 
0
<button> 
id="sharebutton" 
class="g-interactivepost" 
data-prefilltext="<?php if (isset($_GET['text'] && !empty($_GET['text']) { echo $_GET['text']; } ?>" 
Send 
</button> 

这是你以后在做什么?

http://php.net/manual/en/function.isset.php

0

为了保持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'

0

你可以做到以下几点:在php.net

<button 
id="sharebutton" 
class="g-interactivepost" 
<?php echo ($_GET['text'] != 'undefined')? "data-prefilltext=\"".$_GET['text']."\" ": "";?> 
Send 
</button> 

退房Ternary Operator

0
<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>