-1

I have a form which consists of a dropdown and when the user submits the Information, it gets submitted to a PHP Page. Now based on the Response the user has selected on the dropdown, I created another form and I am trying to submit the Information. My Question is, Is it possible to have a post method within a php file?

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 File 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 File 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>";
?>

But once I try to do this it gives an error in the First PHP File saying "Syntax Error Unexpected Post". Kindly suggest on how could I overcome this Issue

user3402248
  • 439
  • 6
  • 25

2 Answers2

1

The syntax highlighting shows the problem:

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

In order to have quotes within a string, you need to "escape" those quotes. Otherwise the parser has no way to know whether you meant to include a quote character or if you meant to end the string. (Syntactically it assumes the latter, of course.)

Since PHP can also use single-quotes for strings, the simplest approach in this specific case is to use those for the server-side code:

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

Note that single-quoted and double-quotes strings in PHP can behave differently depending on what those strings are. (Which is why in this case I opted to break it into two echo statements at the carriage return.)

David
  • 208,112
  • 36
  • 198
  • 279
0

Yes, it is possible, but first fix your syntax error first change this

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

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

Babajide Apata
  • 671
  • 6
  • 18