Question

This is part of my PHP code, and inside of it there is an echo that will print some HTML code, with the onblur and onfocus conditions. This code works when it's outside of echo but not inside of it.
I already tried to add the double quotes on the first input in my code (Username field) but it still doesn't work...
Any ideas?!

elseif ($_GET['register'] == "true"){ echo "

    <div class='index_register'>
        Register to Play or <a href='login.php'>login</a>
        <p></p>
        <!-- Registration Form -->
        <form action='?do=register' method='post' autocomplete='off'>
            <input class=\"input_reg\" type=\"text\" name=\"username\" size=\"40\" maxlength=\"12\" value=\"Username\" onblur=\"if (this.value == \"\") {this.value = \"Username\";}\" onfocus=\"if (this.value == \"Username\") {this.value = \"\";}\">
            <input class='input_reg' type='text' name='email' size='40' maxlength='50' value='Email Address' onblur='if (this.value == '') {this.value = 'Email Address';}' onfocus='if (this.value == 'Email Address') {this.value = '';}'>
            <input class='input_reg' type='password' name='password' size='40' maxlength='12' value='Password' onblur='if (this.value == '') {this.value = 'Password';}' onfocus='if (this.value == 'Password') {this.value = '';}'>
            <input class='submit' type='submit' value='Register' name='Register'>
        </form>
    </div>

"; }
Was it helpful?

Solution

This code works when it's outside of echo but not inside of it.

don't use echo then

elseif ($_GET['register'] == "true"){ 
?>
    <div class='index_register'>
        Register to Play or <a href='login.php'>login</a>
        <p></p>
        <!-- Registration Form -->
        <form action='?do=register' method='post' autocomplete='off'>
...
        </form>
    </div>
<?
 }

OTHER TIPS

Here is what you need. You need to correctly escape quotes inside onblur/onfocus:

try this inside php code:

<input class=\"input_reg\" type=\"text\" name=\"username\" size=\"40\" maxlength=\"12\" value=\"Username\" onblur='if (this.value == \"\") {this.value = \"Username\";}' onfocus='if (this.value == \"Username\") {this.value = \"\";}'>

.

<input class='input_reg' type='text' name='email' size='40' maxlength='50' value='Email Address' onblur='if (this.value == \"\") {this.value = \"Email Address\";}' onfocus='if (this.value == \"Email Address\") {this.value = \"\";}'>

.

<input class='input_reg' type='password' name='password' size='40' maxlength='12' value='Password' onblur='if (this.value == \"\") {this.value = \"Password\";}' onfocus='if (this.value == \"Password\") {this.value = \"\";}'>

In your case heredoc or nowdoc syntax is prefered, when you output large blocks:

<?php
echo <<<HTM
    <div class='index_register'>
        Register to Play or <a href='login.php'>login</a>
        <p></p>
        <!-- Registration Form -->
        <form action='?do=register' method='post' autocomplete='off'>
            <input class='input_reg' type='text' name='username' size='40' maxlength='12' value='Username' onblur='if (this.value == "") {this.value = "Username";}' onfocus='if (this.value == "Username") {this.value = "";}'>
            <input class='input_reg' type='text' name='email' size='40' maxlength='50' value='Email Address' onblur='if (this.value == "") {this.value = "Email Address";}' onfocus='if (this.value == "Email Address") {this.value = "";}'>
            <input class='input_reg' type='password' name='password' size='40' maxlength='12' value='Password' onblur='if (this.value == "") {this.value = "Password";}' onfocus='if (this.value == "Password") {this.value = "";}'>
            <input class='submit' type='submit' value='Register' name='Register'>
        </form>
    </div>
HTM;
?>

If you use the Heredoc System you don´t have to escape the quotes. I used the Heredoc with JavaScript embedded as if I was using plain HTML and it all works as it should.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top