tags:

views:

115

answers:

6

i have a form which inserts book information into the database. however it is not reading the $_POST attribute.

book.php:

  <form action="books_manage.php" id="addbook_form" method="post">

     <div id="ab_wrapper">

         <div id="ab_leftcolumn">
                 <div id="bookinfo">
                 <fieldset>
                     <legend>Book Details</legend>
                         <div class="field">
                             <label>Book ID</label>
                             <input type="text" name="bid" id="bid"/>
                         </div>
                         <div class="field">
                             <label>Name</label>
                             <input type="text" name="bname" id="bname"/>
                         </div>
                          <div class="field">
                             <label>Author</label>
                             <input type="text" name="bauthor" id="bauthor"/>
                         </div>
                          <div class="field">
                             <label>Info</label>
                             <textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
                         </div>
                         <div class="field">
                             <label>Date Added</label>
                             <input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
                         </div>
                         <div class="field">
                             <label>Date Updated</label>
                             <input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
                         </div>
                        <div>
                         <input type="hidden" name="action" value="save">
                         <input type="submit" value="Save">
                         <input type="button" id="addcontent" value="Add Content">
                         <input type="reset" value="Reset">
                        </div>
                 </fieldset>
                 </div>
         </div>

         <div id="ab_rightcolumn">
                <div id="bookcontents">
                <fieldset>
                     <legend>Book Content</legend>
                         <div class="field">
                             <label>Chapter</label>
                             <input type="text" id="bchapter" name="bchapter"/>
                         </div>
                         <div class="field">
                             <label>Sub-Chapter</label>
                             <input type="text" id="bsubchapter" name="bsubchapter"/>
                         </div>
                          <div class="field">
                             <label>Content</label>
                             <textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
                         </div>
                      <br />
                         <div>
                         <input type="hidden" name="action" value="addnext">
                         <input type="submit" value="Save and Add Next Chapter">
                         <input type="submit" name="action" value="Done">
                      </div>

                 </fieldset>
             </div>

         </div>

     </div>



  </form>

books_manage.php:

<?php
  if (isset($_POST['action']) && $_POST['action'] == 'save')
    {
         echo "You clicked the save button";
    }
  else {
    echo "Hello. The date is " .  date("D d M Y") ;

  }

?>

the output:

Hello. The date is Thu 08 Jul 2010

it seems it isn't reading the value of the hidden button. it should display "You clicked the save button". Am I missing something?

+2  A: 

You need to name your submit button "action" and use the value of that button to determine the action. Your code basically has two action form values and the last one is what takes precedence.

  <form action="books_manage.php" id="addbook_form" method="post">

 <div id="ab_wrapper">

     <div id="ab_leftcolumn">
             <div id="bookinfo">
             <fieldset>
                 <legend>Book Details</legend>
                     <div class="field">
                         <label>Book ID</label>
                         <input type="text" name="bid" id="bid"/>
                     </div>
                     <div class="field">
                         <label>Name</label>
                         <input type="text" name="bname" id="bname"/>
                     </div>
                      <div class="field">
                         <label>Author</label>
                         <input type="text" name="bauthor" id="bauthor"/>
                     </div>
                      <div class="field">
                         <label>Info</label>
                         <textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
                     </div>
                     <div class="field">
                         <label>Date Added</label>
                         <input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
                     </div>
                     <div class="field">
                         <label>Date Updated</label>
                         <input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
                     </div>
                    <div>
                     <input type="submit" name="action" value="Save">
                     <input type="button" id="addcontent" value="Add Content">
                     <input type="reset" value="Reset">
                    </div>
             </fieldset>
             </div>
     </div>

     <div id="ab_rightcolumn">
            <div id="bookcontents">
            <fieldset>
                 <legend>Book Content</legend>
                     <div class="field">
                         <label>Chapter</label>
                         <input type="text" id="bchapter" name="bchapter"/>
                     </div>
                     <div class="field">
                         <label>Sub-Chapter</label>
                         <input type="text" id="bsubchapter" name="bsubchapter"/>
                     </div>
                      <div class="field">
                         <label>Content</label>
                         <textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
                     </div>
                  <br />
                     <div>
                     <input type="submit" name="action" value="Save and Add Next Chapter">
                     <input type="submit" name="action" value="Done">
                  </div>

             </fieldset>
         </div>

     </div>

 </div>


<?php
  if (isset($_POST['action']) && $_POST['action'] == 'Save')
{
     echo "You clicked the save button";
}
else if (isset($_POST['action']) && $_POST['action'] == 'Save and Add Next Chapter')
{
    echo 'You clicked the "Save and Add Next Chapter" button';
}
else if (isset($_POST['action']) && $_POST['action'] == 'Done')
{
    echo 'You clicked the done button';
}
else 
{
echo "Hello. The date is " .  date("D d M Y") ;
}
    ?>
John Conde
+3  A: 

Its becuase you have defined action 3 times

<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">

Do the followin on your books_manage.php

echo "<pre>";
print_r($_POST);
echo "</pre>";

You will see where you are going wrong.

Lizard
thank you for the pointer.
fuz3d
+4  A: 

You have two inputs with the name "action" in the same form. Make sure your form field names are unique.

Don't forget you can organise your names using this syntax -

<input name="form1['name']" value="".....
<input name="form2['name']" ..... etc

Then access these variables like this:

$_POST['form1']['name']...

Very useful!

danp
+6  A: 

First of all, multiple <input>s in the same <form> with the same name attribute isn't going to get you the behavior you're looking for.

Instead, you need to provide a name to the submit buttons, and then you can check which button was pressed:

<input type="submit" name="save" value="Add Content">
<input type="submit" name="done" value="No more content">

<?php
if(isset($_POST['save'])) {
    echo "saved";
} else if(isset($_POST['done'])) {
    echo "done";
}
?>

See comment below by Lèse majesté to learn how the HTML working group effed this one up.

Dolph
+1 for showing the best approach for handling "multi action" forms.
Jani Hartikainen
thanks guys for pointing out this silly mistake of mine.
fuz3d
It's a good approach, and would be the best if not for the poor design of submit buttons. With `select` options and checkboxes, the label for the option and the value submitted are separate attributes. But for submit buttons, the button "label" _is_ the submitted value. This was terrible design choice on the part of the HTML working group. This means that if you ever decide to make a presentational change (reword the submit label, or offer different localizations), you end up having to edit your server-side logic, not to mention check for values like "Save and Add Next Chapter".
Lèse majesté
+2  A: 

you have two inputs with name "action". the action you get is probably "Done", not "save"

kgb
+2  A: 

You have multiple inputs named action you will get

<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">

You need to remove the hidden variables and change the name of your first 'submit' to 'action'

<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Done">
Chris Diver