2

我知道我的问题有点令人困惑,但我的意思是我想在 PHP 'echo' 中显示一个 HTML 表单。所以我的整个 HTML 代码都在我的 php 打开和关闭标签中,然后在我的 HTML 脚本中,我想要一个 php 代码,但我收到一条错误消息:

解析错误:语法错误,意外的 'echo' (T_ECHO),期待 ',' 或 ';'

我的代码是这样的:

<?php
   echo '<form method="post" id="customForm" action="add_assessment.php">
    <table>
     // this line is where I get the error
    <input type="hidden" name="res_id" value='echo($_GET['res_id']);' />
?>
4

5 回答 5

5

您可以.在 PHP 中使用来连接字符串。所以你可以这样写:

<?php
   echo '<form method="post" id="customForm" action="add_assessment.php">
    <table>
     // this line is where I get the error
   <input type="hidden" name="res_id" value="'.$_GET['res_id'].'" />';
?>
于 2013-01-03T14:42:31.630 回答
2

.可用于连接字符串。您还可以使用,which 将它们作为单独的回声发送。

<?php
   echo '<form method="post" id="customForm" action="add_assessment.php">
    <table>
    <input type="hidden" name="res_id" value="' . intval($_GET['res_id']) . '" />';
?>

不要忘记 XSS 保护。将intval()用户输入从 $_GET 转换为整数,确保它不是恶意的。这似乎是您系统的重要 ID。您应该确保更改它不会破坏您的代码,如果会,请考虑改用Sessions

XSS 或跨站点脚本攻击是指攻击将 javascript 注入您的页面以试图使其以不同方式工作或重定向用户。在这种情况下,攻击者可以将此表单发送到不同的位置。如果此表格包含信用卡信息、其他个人信息或您的申请中的内部数据;攻击者只需将用户链接到包含错误数据的表单即可访问该信息。

如果设置正确,用户可能甚至都不知道他们的信息被盗了!

于 2013-01-03T14:42:50.057 回答
1
<?php
   echo '<form method="post" id="customForm" action="add_assessment.php">
    <table>
    <input type="hidden" name="res_id" value="' . $_GET['res_id'] . '" />';
?>
于 2013-01-03T14:40:54.633 回答
1

干得好:

<?php
   echo '<form method="post" id="customForm" action="add_assessment.php">
    <table>
    <input type="hidden" name="res_id" value="' . $_GET['res_id'] . '" />';
?>
于 2013-01-03T14:41:28.363 回答
1

在这里,您可以从官方 php 文档中找到如何使用 php-tag 的说明:http: //php.net/manual/en/language.basic-syntax.phpmode.php

于 2013-01-03T14:45:11.473 回答