8 Replies Latest reply on Jun 27, 2012 11:36 AM by fredrogers

    List of Records using PHP button to select just one

    fredrogers

      Hello! First, thank you for reading this.

       

       

      I am just starting php and I am tying to make a site using FileMaker to display and enter information.

      I have the php connecting to my database, then a search page using a form, then it displays a list of records. I would like to make a "button" that will select one record then display related records.

      This is where my trouble is. I do not know how to make a form that will save either the record_Id or key field to then display the next page.

      I am using a foreach loop to display the list in a table:

       

       

      $records = $result->getRecords();

      echo '<table border="1">';

      echo '<tr>';

      echo '<th>Company</th>';

      echo '<th>Id Num</th>';

      echo '<th>Choose</th>';

      echo '</tr>';

      foreach ($records as $record) {

        echo '<tr>';

        echo '<td>'.$record->getField('Company').'</td>';

        echo '<td>'.$record->getField('K_Medical').'</td>';

        echo '<td>

      <form action="welcome.php" method="post">

      #This is where I think I need the button, but instead it just breaks

      <input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';

      <input type="submit" />

      </form>';

      echo '</form></td>';

        echo '</tr>';

      }

      echo '</table>';

       

       

      As you can see I have tried to use a hidden form field to get the key field of the record, but the page dose not work. I get an error 500 when I try to view it in a browser.

       

      Any help would be greatly appreciated! If I have not provided enough information please let me know.

       

      -=fred=-

        • 1. Re: List of Records using PHP button to select just one
          Mike_Mitchell

          Fred:

           

          I think you just have a syntax problem. I see where you're using a single quote to open and close your text strings for PHP to echo out. That's fine, but there's something missing. Note this line:

           

          <input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';

           

          Try this instead:

           

          <input type="hidden" name="med_id[]" value='.$record->getField('K_Medical').'/>';

           

          See the difference? Look at it typed like this:

           

          echo '<input type="hidden" name="med_id[]" value='.$record->getField('K_Medical').'/>';

           

          If you don't use the "." as a string concatenator, PHP gets confused and doesn't know what you mean.

           

          HTH

           

          Mike

          • 2. Re: List of Records using PHP button to select just one
            fredrogers

            Thank you so much for the help!

             

            I am very new to php, so feel free to point out any errors.

             

            Ok I switched out the ' for ". And I added the periods around the value.

             

            I still get error 500, but I think I have a larger issue when I comment out all of the form row of the table I still get an error 500.

             

            It now looks like:

             

              $records = $result->getRecords();

              echo "<table border="1">";

                      echo "<tr>";

                      echo "<th>Company</th>";

                      echo "<th>Id Num</th>";

                      echo "<th>Choose</th>";

                      echo "</tr>;"

                      foreach ($records as $record) {

                          echo "<tr>";

                          echo "<td>".$record->getField('Company')."</td>";

                          echo "<td>".$record->getField('K_Medical')."</td>";

                          echo "<td> <form action='welcome.php' method='post'>"

                    echo "<input type='hidden' name='med_id[]' value=".$record->getField('K_Medical')." />";

                    echo "<input type='submit' />"

                    echo "</form>";

                    echo "</form></td>";

                          echo "</tr>";

                      }

                      echo "</table>";

             

            But If I make it look like:

             

              $records = $result->getRecords();

              echo "<table border="1">";

                 echo "<tr>";

                 echo "<th>Company</th>";

                 echo "<th>Id Num</th>";

                 echo "<th>Choose</th>";

                 echo "</tr>;"

                 foreach ($records as $record) {

                    echo "<tr>";

                    echo "<td>".$record->getField('Company')."</td>";

                    echo "<td>".$record->getField('K_Medical')."</td>";

                   # echo "<td> <form action='welcome.php' method='post'>"

                   # echo "<input type='hidden' name='med_id[]' value=".$record->getField('K_Medical')." />";

                   # echo "<input type='submit' />"

                   # echo "</form>";

                   # echo "</form></td>";

                    echo "</tr>";

                 }

                 echo "</table>";

             

            I still get the error 500.

             

            Thanks!

             

            -=fred=-

            • 3. Re: List of Records using PHP button to select just one
              fredrogers

              Aha I found why the error 500!

               

              On line 7 of what I posted I had : echo "</tr>;" It should have been: echo "</tr>"; I was "echoing out" then end of line semi colon.

               

              Now to add back in the form data and see what I get.

               

              -=fred=-

              • 4. Re: List of Records using PHP button to select just one
                Mike_Mitchell

                Okay, we still have an issue with punctuation. My bad - I missed this the first time; sorry.

                 

                First, comments in PHP are done two ways. A single-line comment looks like this:

                 

                     //  I am a single-line comment

                 

                A block of commented code looks like this:

                 

                     /*  I'm a block of commented code.

                     This is line 2 of the block   */

                 

                The # symbol doesn't comment code out. Not in PHP. So, you're really not disabling your form. Which leads us back to the problem line:

                 

                     echo "<input type='hidden' name='med_id[]' value=".$record->getField('K_Medical')." />";

                 

                Let's see how that would actually echo out in HTML:

                 

                     <input type='hidden' name='med_id[]' value={keyvalue} />

                 

                See anything missing? How about this:

                 

                     echo "<input type='hidden' name='med_id[]' value='".$record->getField('K_Medical')."' />";

                 

                which echoes out as:

                 

                     <input type='hidden' name='med_id[]' value='{keyvalue]' />

                 

                Ah. The quotes around the key value are important. So they need to be in your string.

                 

                BTW - While this will parse correctly, strictly speaking, it's not "per spec". In order for it to be "well-formed" HTML, your parameters should be in double-quotes, not single. So we might want to change it to one of these:

                 

                     echo '<input type="hidden" name="med_id[]" value="'.$record->getField('K_Medical')."' />';

                 

                or

                 

                     echo "<input type=\"hidden\" name=\"med_id[]\" value=\"".$record->getField('K_Medical')."\" />";

                 

                (I think I sprained something typing all those quotes.)         

                 

                Let me know how that works out.

                 

                Mike

                • 5. Re: List of Records using PHP button to select just one
                  fredrogers

                  Ok now when i comment out the form part, the page displays the results.

                   

                  But if I try and have it run the part:

                  echo "<td> <form action='welcome.php' method='post'>" ;

                  echo "<input type='hidden' name='med_id' value="10" />";

                  echo "<input type='submit' />" ;

                  echo "</form>";

                  echo "</td>";

                   

                   

                  It breaks.

                   

                  -=fred=-

                  • 6. Re: List of Records using PHP button to select just one
                    Mike_Mitchell

                    The code is breaking because you have blended single and double quotes:

                     

                         echo "<input type='hidden' name='med_id' value="10" />";

                     

                    What PHP sees is:

                     

                         echo "<input type='hidden' name='med_id' value="

                         10

                         " />"

                     

                    with no closing semicolon on the first two lines. Change it to:

                     

                         echo "<input type='hidden' name='med_id' value='10' />";

                     

                    and you should be good.

                     

                    Mike

                    • 7. Re: List of Records using PHP button to select just one
                      fredrogers

                      Thank you that worked like a champ!

                       

                      Now to try and put back in the .$record and see if I can get it to work.

                       

                      -=fred=-

                      • 8. Re: List of Records using PHP button to select just one
                        fredrogers

                        Thank you so much!

                         

                        Your thoughtfull answers help me see what I was doing wrong.

                         

                        Now for the next fun part. Seeing If can really use the data from the form.

                         

                        -=fred=-