这是我的PHP代码的一部分,其中有一个echo,它将打印一些具有onblur
和onfocus
条件的HTML代码。此代码在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>
"; }
此代码在echo之外但不在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>
<?
}
以下是您需要的。您需要正确onblr/onfocus:内的escape quotes
在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 = "";}'>
在您的情况下,当您输出大块时,首选heredoc
或nowdoc
语法:
<?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;
?>
如果使用Heredoc系统,则不必转义引号。我使用嵌入了JavaScript的Heredoc,就好像我使用的是纯HTML一样,一切都正常工作。