• Resolved transpersonal

    (@transpersonal)


    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • It’s the quotes. Use single quotes inside the enclosing echo double quotes.

    Thread Starter transpersonal

    (@transpersonal)

    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" />'";
    }
    ?>

    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=’

    Thread Starter transpersonal

    (@transpersonal)

    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 } ?>
    Thread Starter transpersonal

    (@transpersonal)

    Never mind, I’m using this now:

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

    Resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Conditional statements work fine when I put text in between them but not code’ is closed to new replies.