8 Replies Latest reply on Aug 6, 2017 6:28 PM by gageler

    php error 500 when using $_POST

    gageler

      I am new to php and am having trouble getting the FileMaker php API to work when I try to set a search variable based on the value in a form, using $_POST.

       

      The search works fine if I "hardwire" the search using the line:

                $divSearch = 'Legal';

       

       

      But, I get an "Internal Server error 500" if I try and using the value submitted in the form, using

      if ($_POST['submit'] == 'Search') {

      $divSearch = $_POST['division'];

      else

      $divSearch = 'Legal';

      }

       

      Based on various examples I have seen, I have a tried a range of ways to apply the $_POST information, but every time I include it, I get an error.

       

      Can anyone advise why this might be happening?

       

      Below is the code that fails, with offending lines highlighted in read.

       

      Thanks

       

      Phillip Gageler

      ----------------------------------------------------

      <?php

       

      require_once('FileMaker.php');

       

      $fm = new FileMaker('PHPSampler', NULL, 'phptest', 'phptest');

       

      ?>

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

      <head>

      <meta http-equiv="content-type" content="text/html; charset=utf-8" />

      <title><?php echo $_SERVER['PHP_SELF'];?></title>

      <link rel="stylesheet" type="text/css" href="format.css" />

      </head>

      <body style="background-color:#EFEFEF;">

          <table width="600" cellpadding="0" cellspacing="10" border="0" align="center">

        

             <!-- header -->

              <tr style="background-color:#6C8EB5;border:1px solid #476898;">

                  <td colspan="2" id="main_table_head">

                

              <font size="3" face="Arial, Helvetica, sans-serif">

      <form action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm"  method="post">

      Division: <input type="text" name="division" id="division" size="40" value="Legal" />

      <br />

              <input type="submit" name="submit" id="submit" value="Search" />

            

      </form></font>

                  </td>

              </tr>

       

      <?php

       

      if (($_POST['submit'])) {

           $divSearch = $_POST['division'];

          else

           $divSearch = 'Legal';

      }

       

              $find = $fm->newFindCommand('phpForm');

            

              $find->addFindCriterion('Division', $divSearch);

       

      $find->addSortRule('Last_Name', 1, FILEMAKER_SORT_ASCEND);

      $find->addSortRule('Preferred_Name', 2, FILEMAKER_SORT_ASCEND);

            

              $result = $find->execute();

            

      $foundCount = $result->getFoundSetCount();

            

      $records = $result->getRecords();

       

      $division = $records[0]->getField('Division');

       

      ?>

              <tr id="main_table_body">

                  <td id="main_table_left">People</td>

                  <td id="main_table_right">

       

                  <?php

                  echo '<p class="title">'.$foundCount.' records found for Division = Human Resources';

                  ?>

                      <table cellpadding="0" cellspacing="0" border="0" width="100%" align="center">

                          <tr>

                              <td width="30%" class="division column_label">Division</td>

                              <td width="35%" class="last_name column_label">Last Name</td>

                              <td width="35%" class="preferred_name column_label">Preferred Name</td>

                          </tr>

       

      <?php       foreach($records as $record)

            

                  echo '

                          <tr>

                              <td class="division column_data">'.$record->getField('Division').'</td>

                              <td class="last_name column_data">'.$record->getField('Last_Name').'</td>

                              <td class="preferred_name column_data">'.$record->getField('Preferred_Name').'</td>

                          </tr>';

      ?>         

                      </table>

                  </td>

              </tr>

              <tr id="main_table_footer">

                  <td colspan="2" style="height:50px;color:#FFFFFF;font:22px arial bold;text-

      align:center;">footer</td>

              </tr>

          </table>

      </body>

      </html>

       

      ---------------------------------------------------

        • 1. Re: php error 500 when using $_POST
          user19752

          You missed braces aroud else.

          } else {

          • 2. Re: php error 500 when using $_POST
            gageler

            Thanks - I must have missed that on my final attempt.

             

            However, after adding the braces, I still get the error.

             

            The page works fine if I remove any attempts to compare $_POST before submitting the form.

             

            And if I am editing the php file while testing it, the presence of the of the conditional section if($_POST['submit']))...  does not cause a problem.

             

            But if I log out and come back to it later, I get the error 500. It is like the $_POST is only available while I have an open session. If a open a new session, have $_POST anywhere in the code before the form causes the error.

             

            I hope this make sense.

            • 3. Re: php error 500 when using $_POST
              user19752

              It may be

              if(isset($_POST['submit']))..

              • 4. Re: php error 500 when using $_POST
                beverly

                Yes, if used at all. Single line statements (in if or else) are not required to use the {...}.

                My preference is to ALWAYS use them:

                if ( ) {

                do this...

                } else {

                do that...

                }

                Beverly

                • 5. Re: php error 500 when using $_POST
                  beverly

                  I like to always test for a value with the isset().

                  if(isset($_POST['___']))

                  Beverly

                  • 6. Re: php error 500 when using $_POST
                    fmpdude

                    Maybe try an IDE for PHP that has debugging capabilities so you can step through, set breakpoints, and such?

                     

                    Eclipse for PHP. Amazing! FREE.

                     

                    PDT: Eclipse PHP Development Tools

                    • 7. Re: php error 500 when using $_POST
                      gageler

                      Thank you everyone for your help.

                      Adding the missing curly brackets alone did not work.

                      Using isset worked:

                          if (isset($_POST['submit'])) {

                                $divSearch = $_POST['division'];

                              } else {

                                $divSearch = 'Legal';

                          }

                       

                      However, if I wanted to echo the last submitted value for 'division' in the form (using value="<?php echo $_POST['division'];?>"), I had to move this section of code to be in front of the form, and set $_POST['division'] to some value:

                       

                      if (isset($_POST['division'])) {

                          $divSearch = $_POST['division'];

                          }else{

                      $_POST['division'] = 'Legal';

                      $divSearch = 'Legal';

                      }

                       

                      Attempting to echo $_POST['division'] in the form resulted in an error 500 if the value was not set.

                       

                      So in the end, the following code works:

                      <?php

                      require_once('FileMaker.php');

                      $fm = new FileMaker('PHPSampler', NULL, 'phptest', 'phptest');

                       

                      if (isset($_POST['division'])) {

                          $divSearch = $_POST['division'];

                          }else{

                      $_POST['division'] = 'Legal';

                      $divSearch = 'Legal';

                      }

                       

                      ?>

                      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

                              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

                      <head>

                      <meta http-equiv="content-type" content="text/html; charset=utf-8" />

                      <title><?php echo $_SERVER['PHP_SELF'];?></title>

                      <link rel="stylesheet" type="text/css" href="format.css" />

                      </head>

                      <body style="background-color:#EFEFEF;">

                          <table width="600" cellpadding="0" cellspacing="10" border="0" align="center">

                            <!-- header -->

                              <tr style="background-color:#6C8EB5;border:1px solid #476898;">

                                  <td colspan="2" id="main_table_head">  

                              <font size="3" face="Arial, Helvetica, sans-serif">

                      <form action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm"  method="post">

                      Division: <input type="text" name="division" id="division" size="40" value="<?php echo $_POST['division'];?>" />

                      <br />

                              <input type="submit" name="submit" id="submit" value="Search" />

                      </form></font>

                                  </td>

                              </tr>

                       

                      <?php

                              $find = $fm->newFindCommand('phpForm'); 

                              $find->addFindCriterion('Division', $divSearch);

                       

                      $find->addSortRule('Last_Name', 1, FILEMAKER_SORT_ASCEND);

                      $find->addSortRule('Preferred_Name', 2, FILEMAKER_SORT_ASCEND);

                       

                              $result = $find->execute();

                          

                      $foundCount = $result->getFoundSetCount();

                          

                      $records = $result->getRecords();

                       

                      $division = $records[0]->getField('Division');

                       

                      ?>

                              <tr id="main_table_body">

                                  <td id="main_table_left">People</td>

                                  <td id="main_table_right">

                       

                                  <?php

                                  echo '<p class="title">'.$foundCount.' records found for Division = '.$divSearch;

                                  ?>

                                      <table cellpadding="0" cellspacing="0" border="0" width="100%" align="center">

                                          <tr>

                                              <td width="30%" class="division column_label">Division</td>

                                              <td width="35%" class="last_name column_label">Last Name</td>

                                              <td width="35%" class="preferred_name column_label">Preferred Name</td>

                                          </tr>

                       

                      <?php      foreach($records as $record)

                          

                                  echo '

                                          <tr>

                                              <td class="division column_data">'.$record->getField('Division').'</td>

                                              <td class="last_name column_data">'.$record->getField('Last_Name').'</td>

                                              <td class="preferred_name column_data">'.$record->getField('Preferred_Name').'</td>

                                          </tr>';

                      ?>

                                      </table>

                                  </td>

                              </tr>

                              <tr id="main_table_footer">

                                  <td colspan="2" style="height:50px;color:#FFFFFF;font:22px arial bold;text-

                      align:center;">footer</td>

                              </tr>

                          </table>

                      </body>

                      </html>

                      • 8. Re: php error 500 when using $_POST
                        gageler

                        Thanks for that. I will definitely download that software.