Forums

[resolved] Conditional statements work fine when I put text in between them but not code (6 posts)

  1. TransPersonal
    Member
    Posted 2 years ago #

    Hello,

    When I use the following I get a parse error. It works fine if I put normal text inside the quotes after echo but gets broken when I put the html/php:

    <?php if (is_search()) {
    	echo "<input type="text" name="s" onblur="if (this.value == '') {this.value = 'Search';}" onfocus="if (this.value == 'Search') {this.value = '';}" value="Search" class="search_bar" />";
    } else {
    	echo "<input type="text" name="s" value="<?php the_search_query(); ?>" class="search_bar_2" />";
    }
    ?>

    Please help.

  2. iridiax
    Member
    Posted 2 years ago #

    It's the quotes. Use single quotes inside the enclosing echo double quotes.

  3. TransPersonal
    Member
    Posted 2 years ago #

    iridiax, not sure if I'm doing what you told right but this doesn't remove the error:

    <?php if (is_search()) {
    	echo "'<input type="text" name="s" onblur="if (this.value == '') {this.value = 'Search';}" onfocus="if (this.value == 'Search') {this.value = '';}" value="Search" class="search_bar" />'";
    } else {
    	echo "'<input type="text" name="s" value="<?php the_search_query(); ?>" class="search_bar_2" />'";
    }
    ?>
  4. iridiax
    Member
    Posted 2 years ago #

    You can't have any double quotes inside the enclosing echo double quotes.

    Bad: echo "<input type="text" name="s" value="

    Bad: echo "'<input type="text" name="s" value="

    Good: echo "<input type='text' name='s' value='

  5. TransPersonal
    Member
    Posted 2 years ago #

    Thanks iridiax, it displays now without the error. There are other problems though like the blur effect in the input field doesn't work, etc. So maybye using that format of conditional statement isn't the right one for this purpose. I have always used the following format effectively. There's no quotes that can conflict with the quotes in my html code. Do you know how to add a else to it:

    <?php if(is_search()){ ?>
    Stuff
    <?php } ?>
  6. TransPersonal
    Member
    Posted 2 years ago #

    Never mind, I'm using this now:

    <?php if (is_search()) { ?>
    stuff
    <?php } else { ?>
    more stuff
    <?php } ?>

    Resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.