【问题标题】:Echoing functions, but not enough quotes available回声功能,但没有足够的报价可用
【发布时间】:2017-08-14 02:55:04
【问题描述】:

我正在尝试回显注册表单,但收到错误消息:

"Uncaught SyntaxError: Unexpected token }"

我相信这是由于在调用带有变量的函数时使用了许多',但我不知道如何解决它。我尝试使用`,但出现错误:

“Uncaught SyntaxError: Unexpected token ILLEGAL”

这是表格。

onblur='checkField('username', '1')'

是我认为发生错误的地方

echo "<tr><td>Username:</td><td><input id='username' type='text' size='25'maxlength='25' onblur='checkField('username', '1')' /><div id=usernameHelp></div></td></tr>";
echo "<tr><td>Password:</td><td><input id='password' type='password' size='25' maxlength='25' onblur='checkField('password', '2')'/><div id=passwordHelp></div></td></tr>";
echo "<tr><td>Email: </td><td><input id='email' type='text' size='50' maxlength='50' onblur='checkField('email', '3')'/><div id=emailHelp></div></td></tr>";
echo "<tr><td>First Name:</td><td><input id='firstName' type='text' size='25' maxlength='25' onblur='checkField('name', '4')'/><div id=nameHelp></div></td></tr>";
echo "<tr><td>Address:</td><td><input id='address' type='text' size='50' maxlength='50' onblur='checkField('address', '5')'/><div id=addressHelp></div></td></tr>";
echo "<tr><td>Phone:</td><td><input id='phone' type='text' size='10' maxlength='10' onblur='checkField('phone', '6')'/><div id=phoneHelp></div></td><input id='formAttempted' type='hidden' value='true' /></tr>";
echo "<tr><td><br /><br /></td><td><input onclick='signuper()' type='submit' value='Signup!' /><br />";

echo "</td></tr></table>";

【问题讨论】:

  • 用反斜杠 (` \ `) 转义引号

标签: php html-table echo


【解决方案1】:

我将 HTML 和 PHP 分开。这样更简洁,如果代码分开,您将更容易更改和维护代码。

<?php

//code

?>

<tr><td>Username:</td><td><input id='username' type='text' size='25'maxlength='25' onblur='checkField('username', '1')' /><div id=usernameHelp></div></td></tr>
<tr><td>Password:</td><td><input id='password' type='password' size='25' maxlength='25' onblur='checkField('password', '2')'/><div id=passwordHelp></div></td></tr>
<tr><td>Email: </td><td><input id='email' type='text' size='50' maxlength='50' onblur='checkField('email', '3')'/><div id=emailHelp></div></td></tr>
<tr><td>First Name:</td><td><input id='firstName' type='text' size='25' maxlength='25' onblur='checkField('name', '4')'/><div id=nameHelp></div></td></tr>
<tr><td>Address:</td><td><input id='address' type='text' size='50' maxlength='50' onblur='checkField('address', '5')'/><div id=addressHelp></div></td></tr>
<tr><td>Phone:</td><td><input id='phone' type='text' size='10' maxlength='10' onblur='checkField('phone', '6')'/><div id=phoneHelp></div></td><input id='formAttempted' type='hidden' value='true' /></tr>
<tr><td><br /><br /></td><td><input onclick='signuper()' type='submit' value='Signup!' /><br />

</td></tr></table>

如果要在 HTML 中插入变量,可以使用以下语法:

<div class="foo">
    <p>Some text: <?php echo $text; ?> </p>
</div>

如果您想在 if 语句中执行此操作,可以使用以下语法:

<? if(some_condition){ ?>
     <!-- your HTML here -->
<? } ?>

希望这会有所帮助!

【讨论】:

  • 这部分代码在 if 语句中,其中有一个 else 语句用于检查他们是否已登录。如果我将它分开,我将如何解决这个问题?
  • @user2330621:更新了答案。
  • 天哪!有效!太感谢了!这真的让我很紧张。
【解决方案2】:

引号中有引号。将它们混合或逃脱。

onblur="checkField('username', '1')"
onblur='checkField("username", '1')'

另外,不需要将整数用引号括起来。

【讨论】:

【解决方案3】:

把这个改成

echo "<tr><td>Username:</td><td><input id='username' type='text' size='25'maxlength='25' onblur='checkField('username', '1')' /><div id=usernameHelp></div></td></tr>";

echo "<tr><td>Username:</td><td><input id='username' type='text' size='25'maxlength='25' onblur='checkField(\'username\', \'1\')' /><div id=usernameHelp></div></td></tr>";

适用于所有 checkField 函数

【讨论】:

  • 试过了,得到了 Uncaught SyntaxError: Unexpected token ILLEGAL
  • 所有 checkField() 函数的转义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 2018-09-29
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多