-1

这是我的 PHP 代码的一部分,其中有一个回显,它将打印一些 HTML 代码,带有onblurandonfocus条件。此代码在其外部echo但不在其内部时有效。
我已经尝试input在我的代码(Username字段)中的第一个添加双引号,但它仍然不起作用......有什么
想法吗?!

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>

"; }
4

4 回答 4

4

此代码在 echo 之外但不在其内部时有效。

然后不要使用回声

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>
<?
 }
于 2013-04-16T09:28:55.663 回答
0

这是你需要的。您需要正确地 escape quotes在 onblur/onfocus 内部:

在 php 代码中试试这个:

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

于 2013-04-16T09:27:09.933 回答
0

在您的情况heredocnowdoc语法是首选时,当您输出大块时:

<?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;
?>
于 2013-04-16T09:56:43.860 回答
0

如果您使用 Heredoc 系统,则不必转义引号。我使用了嵌入了 JavaScript 的 Heredoc,就好像我使用的是纯 HTML 一样,并且一切正常。

于 2016-01-28T21:17:01.087 回答