An " if statement " (or something similar) for a textfield

7 posts by 3 authors in: Forums > CMS Builder
Last Post: October 6, 2013   (RSS)

By CarlosPinedo - October 5, 2013 - edited: October 6, 2013

Hi,

In an editor, I have a textfield to enter urls. This textfield it's not required, so, users of the cmsb may leave it blank.

This is the code in the frontend:

<p><span> Website: </span><a href="<?php echo htmlencode($record['info_map_link']) ?>" target="_blank"> click here </a></p>

When they enter the urls there's no problem. The problem appears when they don´t write an URL. Because, when the website visitors see the text "click here " and they click it, obviously, nothing happens.

I want the text "click here" to appear only if the cmsb user writes an url.

How can i do this?

Carlos

By jenolan - October 6, 2013

<p><span> Website: </span> <?php if( ! empty( trim( $record['info_map_link'] ) ) : ?><a href="<?php echo 
htmlencode($record['info_map_link']) ?>" target="_blank"> click 
here </a><?php endif ?></p>

---



Peace and Long Life

Hi Larry , i´m getting this error: Fatal error: Can't use function return value in write context in /home/observat/public_html/test.php on line 320

 At line 320 is your code:

<p><span> Website: </span> <?php if( ! empty( trim( $record['info_map_link'] ) ) : ?><a href="<?php echo 
htmlencode($record['info_map_link']) ?>" target="_blank"> click 
here </a><?php endif ?></p>

Hi Carlos,

Try this...

<?php if($record['info_map_link'] ): // checks for existence of a map link ?>
<?php
if(!preg_match("/^https:\/\//i", $record['info_map_link'] )): // These check for a properly formatted link in the field ?>
<?PHP if (!preg_match("/^http:\/\//i", $record['info_map_link'])) {
$record['info_map_link'] = "http://" . $record['info_map_link']; } ?>
<?PHP endif ?>
<p><span> Website: </span><a href="<?php echo htmlencode($record['info_map_link']) ?>" target="_blank">click here</a></p>
<?php endif ?>

BTW: you'll need to supply at least a class value for the span before Website:

Hope this helps

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Great, that works!, thanks a lot!

Carlos

By jenolan - October 6, 2013

Blargh I always forget that trim aint allowed.

<p><span> Website: </span> <?php if( ! empty(  $record['info_map_link'] ) : ?><a href="<?php echo 
htmlencode($record['info_map_link']) ?>" target="_blank"> click 
here </a><?php endif ?></p>here </a><?php endif ?></p>

Try thsi to use trim alone to make sure someone putting a few spaces in doesn't break it...

<?php
$record['info_map_link'] = trim( $record['info_map_link'] );
?>
<p>
    <span> Website: </span> 
        <?php if( ! empty(  $record['info_map_link'] ) ) : ?>
            <a href="<?php echo htmlencode( $record['info_map_link'] ) ?>" target="_blank"> click here </a>
        <?php endif ?>
</p>

That should be better, but this one will 'validate' a url, assuming your server allows it...

<?php
$record['info_map_link'] = trim( $record['info_map_link'] );
if( $record['info_map_link'] )
{
    $array = get_headers( $record['info_map_link'] );
    if( ! strpos( $array[0], "200" ) ) $record['info_map_link'] = '';
?>
<p>
    <span> Website: </span> 
        <?php if( $record['info_map_link'] ) : ?>
            <a href="<?php echo htmlencode( $record['info_map_link'] ) ?>" target="_blank"> click here </a>
        <?php endif ?>
</p>

---



Peace and Long Life