-1

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>

"; }
zppinto
  • 287
  • 7
  • 20
  • 1
    You really need to study what's client side and what's server side programming – Mr. Alien Apr 16 '13 at 09:26
  • I know that! But it's possible to get this to work correctly? – zppinto Apr 16 '13 at 09:27
  • 1
    If you are expecting to echo something on change than answer is no, if you are expecting that onchange text is already present and it is causing issues than it's probably quotes, and the main thing is **WHY YOU ARE ECHOING SO MUCH OF HTML USING PHP?** – Mr. Alien Apr 16 '13 at 09:29
  • I need to echo! Otherwise it won't work because of my previous php code and conditions... I know I don't know how to code, but it works :P – zppinto Apr 16 '13 at 09:38
  • Never echo so much HTML using PHP :) – Mr. Alien Apr 16 '13 at 09:39
  • 1
    Cramming HTML into a string for no reason is a really bad practice, and will only give headaches to any developer succeeding you. – EJTH Apr 16 '13 at 09:59
  • 1
    I`m working for years with THAT ^ **REALLY BAD** practice and have no problems? &? – BlitZ Apr 16 '13 at 10:02

4 Answers4

4

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>
<?
 }
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Because of my previous PHP code I can't use it this way! That's why I'm asking for help to get it to work inside an `echo` tag. Thank you anyway – zppinto Apr 16 '13 at 09:45
  • You're right! I can get it to work like this, but it's not my intention. I accepted your answer as useful ;) – zppinto Apr 16 '13 at 10:06
0

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 = \"\";}'>

Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70
0

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;
?>
BlitZ
  • 12,038
  • 3
  • 49
  • 68
0

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.