duanmao7553 2014-05-21 11:32
浏览 41
已采纳

PHP HTML Heredoc Parse错误:语法错误,意外'''

So I'm writing HTML code for form inside my PHP block like below: Add New Record in PostgreSQL Database .error {color: #FF0000;}

$con= pg_connect("host=$host port= $port dbname=$db user=$user password=$pass") 
    or die ("Could not connect to server
");
if(empty($_POST['title']))
{
    $titleErr= "Title is required";
    displayForm();
}
else
{
    processForm();

}
}

else
{
displayForm();
}

function processForm(){
$title = $_POST['title'];
$author = $_POST['author'];
$status = $_POST['status'];
$remark = $_POST['remark'];
$story = $_POST['story'];
$art = $_POST['art'];
if($story=='')
{
$story=0;
}
if($art=='')
{
$art=0;
}

$query = "INSERT INTO rating ".
   "(title,author,story,art, status, remarks) ".
   "VALUES('$title','$author',$story, $art,'$status','$remark')";
$rs= pg_query($con, $query) or die ("Cannot execute query:$query because".pg_last_error()."
");

/************************************************
Save uploaded image in server
************************************************/
echo "Entered data successfully
";
pg_close($con);
}

function displayForm()
{
print<<<END
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Title</td>
<td><input name="title" type="text" id="title"><span class="error">* <?php echo $titleErr;?></span></td>
</tr>
<tr>
<td width="100">Author</td>
<td><input name="author" type="text" id="author"></td>
</tr>
<tr>
<td width="100">Story Rating</td>
<td><input name="story" type="text" id="story"></td>
</tr>
<tr>
<td width="100">Art Rating</td>
<td><input name="art" type="text" id="art"></td>
</tr>
<tr>
<td width="100">Remarks</td>
<td><input name="remark" type="text" id="remark"></td>
</tr>
<tr>
<td width="100">Status</td>
<td><input name="status" type="text" id="status"></td>
</tr>
<tr>
<td width="100">Image</td>
<td><input name="file" type="file" id="file"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Entry">
</td>
</tr>
</table>
</form>
END;
}
?>
</body>
</html>

In the function displayForm(), I keep getting this error: Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING), specifically in this line: " enctype="multipart/form-data">

I made sure already there's no leading space at the closing of the Heredoc and the beginning of the Heredoc, and I can't find any solution on the net. What I'm trying to achieve is displaying the error message besides the required fields after the user click submit (display the form once again, now with error message). Please suggest me a solution on this as well in case the structure of my code is wrong.

  • 写回答

3条回答 默认 最新

  • duanba5777 2014-05-21 11:38
    关注

    You should encapsulate your indexed variable like this inside your quoted string or heredoc: {$_SERVER["PHP_SELF"]}

    But that is not your only problem. Your next problem is that you are trying to open a <?php tag inside the heredoc. Though this should not give you an error, it will also not work. You are already inside PHP! So this will just literally output that code.

    You can use variables within heredoc as I already stated. If it's an indexed variable (meaning it uses []), use curly braces {} around the variable. But you cannot call functions (such as htmlspecialchars()) from within the heredoc. Call the function before you start your heredoc. (edit: As Marcin Nabialek shows us in his answer, there is a work-around to call functions from within the heredoc. But you then have to first put the function name in another variable again.)

    So what you need to do is change :

    print<<<END
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">
    

    into:

    $phpself = htmlspecialchars($_SERVER["PHP_SELF"]);
    print<<<END
    <form method="post" action="$phpself" enctype="multipart/form-data">
    

    And change:

    <td><input name="title" type="text" id="title"><span class="error">* <?php echo $titleErr;?></span></td>
    

    into:

    <td><input name="title" type="text" id="title"><span class="error">* $titleErr</span></td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度