PHP Parse错误:语法错误,意外'发布'

时间:2016-04-28 18:00:30

标签: php forms

我有一个由下拉列表组成的表单,当用户提交信息时,它会被提交到PHP页面。现在根据用户在下拉列表中选择的响应,我创建了另一个表单,我正在尝试提交信息。我的问题是,是否可以在php文件中使用post方法?

HTML

<form method="post" name="project_form" action="php/harvest_clientproject.php">
Client Name:
                        <select id="clt" name="client_name">
<option value="A">Client A</option>
  <option value="B">Client B</option>

<input  type="submit" value="Submit">

                        </form>

PHP文件1-Clientproject.php

    $client_id_dropdown=$_POST['client_name'];
    echo $client_id_dropdown;

    /*Convert String to Integer */

    $client_id_dropdown_int=(int) $client_id_dropdown;


       /*Populating a dropdown with the list of projects from an array $data*/ 

      echo " <form method= "post" name="project_form_2" action="harvest_clientproject2.php">
        <select id="animal" name="project_name">";

           // Iterating through the product array
            foreach($data as $ $key=>$fruit){

          echo "<option value= "; echo  $fruit->{'id'}; echo ">" ;echo $fruit->{'name'};echo"</option>";

            }
            ?> echo "
        </select>
        <input type="submit" value="Submit">

</form>";

PHP文件2-harvest_clientproject2.php

*Getting Project ID */

$projectid=$_POST['project_name'];

/*Convert String to Integer */

$projectid_int=(int) $projectid;


/*Generating Project Details and putting it in the form of a table */

$result = $api->getProject($projectid);
$data = $result->get( "data" );

echo "<table border='1'>
    <tr><td>Project Name</td>
    <td>Project Creation Date</td>
    <td>Billable</td>
    </tr>";
        foreach($data as $key=>$fruit) {
                        ?>
                     <tr><td><?php echo $fruit->name;?></td>
                     <td><?php echo $fruit->{'created-at'};?></td>
                      <td><?php echo $fruit->{'billable'};?></td></tr>
        <?php ;}
    echo "</table>";
?>

但是一旦我尝试这样做,它会在第一个PHP文件中出现错误,说“语法错误意外发布”。请建议我如何克服这个问题

2 个答案:

答案 0 :(得分:1)

语法高亮显示问题:

echo " <form method= "post" name="project_form_2" action="harvest_clientproject2.php">
    <select id="animal" name="project_name">";

为了在字符串中包含引号,您需要“转义”这些引号。否则,解析器无法知道您是否要包含引号字符,或者是否要结束字符串。 (当然,从语法上来说,它假设后者。)

由于PHP也可以对字符串使用单引号,因此在这种特定情况下最简单的方法是将它们用于服务器端代码:

echo '<form method="post" name="project_form_2" action="harvest_clientproject2.php">';
echo '<select id="animal" name="project_name">';

请注意PHP can behave differently中的单引号和双引号字符串取决于这些字符串是什么。 (这就是为什么在这种情况下我选择在回车时把它分成两个echo语句。)

答案 1 :(得分:0)

是的,这是可能的,但首先修复语法错误首先更改此

echo " <form method= "post" name="project_form_2" action="harvest_clientproject2.php">
    <select id="animal" name="project_name">";

echo '<form method= "post" name="project_form_2" action="harvest_clientproject2.php">';