如何将PHP脚本放在HTML脚本中的PHP脚本


how to put php scripts in html scripts in a php script

我知道我的问题有点令人困惑,但我的意思是我想在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']);' />
?>

在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'].'" />';
?>

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

<?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注入您的页面,试图使其工作方式不同或重定向用户。在这种情况下,攻击者可以将此表单发送到不同的位置。如果此表格包含信用卡信息、其他个人信息或您申请的内部数据;攻击者可以简单地通过将用户链接到包含错误数据的表单来访问这些信息。

如果设置正确,用户可能永远不会知道他们的信息被盗!

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

给你:

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

您可以从php官方文档中找到如何使用php-tag的解释:http://php.net/manual/en/language.basic-syntax.phpmode.php