10 Replies Latest reply on Dec 5, 2012 6:23 AM by Mike_Mitchell

    Passing Blank Values in a Form - API

    artdezign

      Hi There,

       

      I have a form that is meant to create a new entry for a blog post. But once it is executed it creates a new record with only blank values. Can anyone tell what is missing here?

       

      Form Code:

       

       

      <form action="newEntryResponse.php" method="POST" name="newEntry">

       

       

      <h2>Title</h2>

      <input name="title" type="text" id="title"><br />

      <h2>Content</h2>

      <textarea name="body" cols="50" rows="8"></textarea><br />

      <input type="submit" name="submit" value="Submit Post"><br />

      </form>

       

      Respose PHP code:

       

      <?php

      require_once ('../config.php');

      require_once ('functions.inc.php');

      ?>

       

       

      <?php

      $newPostCmd = $fm->newAddCommand('BlogPosts'); // initialize the database for Add Command

      $newPostCmd->setField('title',POST('title')); // Create the new Title Field

      $newPostCmd->setField('body',POST('body')); // Create the new Body Field ($fmCommance->setField('fieldName', $value);

      $newPost=$newPostCmd->execute(); //execute the command

       

       

      if(FileMaker::isError($newPost)) {

      die('Database Error: '.$newPost->getMessage());

      }

       

       

      ?>

      <?php include('BlogHeader.php'); ?>

       

       

      <main>

      <paper>

      <h1>Success</h1>

      <p>Your Post was made Successfully, please go back to the <a href="index.php">Home Page</a></p>

      </paper>

      </main>

       

       

      <?php include('BlogFooter.php'); ?>

       

      I greatly appreciate any suggestions you have.

       

      Thanks,

        • 1. Re: Passing Blank Values in a Form - API
          mbraendle

          I think it is $_POST, not POST.

           

          Edit: $_POST is an associative array, so you need to write, e.g. $_POST['title']

           

          Best regards,

           

          Martin

           

          Message was edited by: MartinBraendle

          • 2. Re: Passing Blank Values in a Form - API
            artdezign

            Hey Martin,

             

            I tried that, but it made the function die.  $_POST would be the way to go with pure PHP, but this is the Filemaker API and doesn't use that format in the line commands.

             

            Thanks though,  let me know if you see something else.

            • 3. Re: Passing Blank Values in a Form - API
              beverly

              Yes, but it's $_POST['body'], etc.

               

              You have an array of POSTed values after submit. You access with the key to get the value.

               

              If your method had been "get" instead of "post" on the form (or you are reading an URL, use $_GET['body'], for example.

               

              To access either post or get, you may be able to use $_REQUEST['body']

               

              php.net is going to be helpful for learning PHP! There are many examples.

               

              -- sent from my iPhone4 --

              Beverly Voth

              --

              • 4. Re: Passing Blank Values in a Form - API
                mbraendle

                Bev,

                 

                thanks for your explanation in other words - I was a little bit short on that.

                 

                php.net is indeed a great resource!

                • 5. Re: Passing Blank Values in a Form - API
                  beverly

                  I tried that, but it made the function die.  $_POST would be the way to go with pure PHP, but this is the Filemaker API and doesn't use that format in the line commands.

                   

                   

                  The API is just a "layer" on the PHP. You should still use standard PHP! If you are setting a database with the POSTed values, then use $_POST['formfield']

                   

                  NOTE that the "formfield" is the name of the INPUT from the FORM, not necessarily the name of the field, as they can be different.

                   

                   

                  $newPostCmd = $fm->newAddCommand('BlogPosts'); // initialize the database for Add Command

                  $newPostCmd->setField('title',POST('title')); // Create the new Title Field

                  $newPostCmd->setField('body',POST('body')); // Create the new Body Field ($fmCommance->setField('fieldName', $value);

                   

                   

                  should be:

                  $newPostCmd = $fm->newAddCommand('BlogPosts'); // initialize the database for Add Command

                  $newPostCmd->setField( 'title', $_POST['title'] ); // Create the new Title Field

                  $newPostCmd->setField( 'body', $_POST['body'] ); // Create the new Body Field ($fmCommance->setField('fieldName', $value);

                   

                  Beverly

                  • 6. Re: Passing Blank Values in a Form - API
                    artdezign

                    This did it!  Thanks!  The code I had up was directly from a book that I now wonder if I should throw away...  Thanks for being apart of this forum.  I've know about php.net for quite a while now, and my new Filemaker APHI for PHP book references it directly so I feel I can trust it more.

                     

                    Thank you, thank you.

                    • 7. Re: Passing Blank Values in a Form - API
                      LyndsayHowarth

                      I wouldn't have survived without php.net

                       

                      Another place I use because it is succinct is http://www.w3schools.com/

                       

                      - Lyndsay

                      • 8. Re: Passing Blank Values in a Form - API
                        Mike_Mitchell

                        The reason the book uses POST instead of $_POST is because they have a function in the book called POST that does some processing. It's in the default include file. If you don't include that function, the PHP parser doesn't know what you're talking about.

                         

                        Here's a copy of the function:

                         

                        function POST($var=null)   {

                            if($var === null)   {

                                $ret = array();

                                foreach($_POST as $key=>$value)   {

                                    $ret[$key] = POST($key);

                                }

                                return $ret;

                            }

                            if(!isset($_POST[$var]) )  return false;

                            if(get_magic_quotes_gpc() )   {

                                return stripslashes($_POST[$var]);

                            }else{

                                return $_POST[$var];

                            }

                        }

                         

                        I don't have the book handy to give you a page reference, but they do explain what the function does in the book. It's not strictly required.

                         

                        HTH

                         

                        Mike

                        • 9. Re: Passing Blank Values in a Form - API
                          datastride

                          Just FYI:

                           

                           

                           

                          The “magic quotes” feature was deprecated in PHP 5.3 and removed entirely in PHP 5.4.

                           

                           

                           

                          I’d recommend just addressing the $_POST array directly …

                           

                           

                           

                          Peace, love & brown rice,

                           

                          Morgan Jones

                           

                           

                           

                          FileMaker + Web:  Design, Develop & Deploy

                           

                          Certifications: FileMaker 9, 10, 11 & 12

                           

                          Member: FileMaker Business Alliance

                           

                          One Part Harmony <http://www.onepartharmony.com/>  

                           

                          Austin, Texas • USA

                           

                          512-422-0611

                          • 10. Re: Passing Blank Values in a Form - API
                            Mike_Mitchell

                            Good info, Morgan. Thanks. The book was published several years ago; it's always a good idea to double-check such things.

                             

                            Mike