开发者

one form multiply tasks with php

I am making an CMS, and the pag开发者_JS百科es that are going to be made by the clients, they are going to use the same form to edit the page, add new page, add new subpage, edit the subpage.. I dont want to copy that form multiply times for each action. I would like to know if anyone of you has an idea how to use that form for all of the actions in the best way.

    <div class="containerholder">
<div class="container">
<div id="contain">
        <form method="post" id="customForm" action="edited.php">
            <div>
                <label for="name">Navigation</label>
                <input id="name" name="navigation" type="text" value="<?php echo $navigation ?>" />
            </div>
            <div>
                <label for="message">Content</label>
                <textarea id="content" name="content" cols="" rows="" ><?php echo $content ?></textarea>
            </div>
            <div>
                <input type="hidden" name="id" id="id" value="<?php echo $id ?>">
                <input id="send" name="send" type="submit" value="Save and update" />
            </div>
        </form>
    </div>
</div></div>

this is the form if you want to see it, I have an idea how to make it but it doesnt look to me like it is the best way (professional).

Thank you for your time!


Use multiple submit-buttons for every task and identify the requested task by the name of the submit-button(usually only a clicked submit-button is submitted)

Professional: it's not "professional" in my eyes to use a single form for all tasks.


You could use multiple submit buttons, as google does with its search/feeling lucky choice:

  <input id="send" name="send" type="submit" value="edit" />
  <input id="send" name="send" type="submit" value="new page" />
  <input id="send" name="send" type="submit" value="edit subpage" />
  <input id="send" name="send" type="submit" value="new subpage" />

The name=value pair for the submit button is transmitted as it is for the rest of the form data, so at the server it is collected using the same functionality ($_GET or $_POST etc.)

Alternatively, you could use form controls and javascript events to control the communication to the server via AJAX (it's more flexible, you can control exactly what data is transmitted via javascript, and it's seamless for the end-user who isn't waiting for page reloads). See http://www.w3schools.com/ajax for simple source code.


add two or more radio button to select you function, for example :

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

<input type="radio" name="function" value="Add" />
<input type="radio" name="function" value="Delete" />
<input type="radio" name="function" value="Edit" />

..
</form>

and in php :

<?PHP
...

if( $_POST['function'] == 'Add' )
{
//Add Function
} else
if( $_POST['function'] == 'Delete' )
{
//DeleteFunction
} else
if( $_POST['function'] == 'Edit' )
{
//EditFunction
}

...
?>


I would suggest making a single form, and then using scripting to switch it between modes and datafill the fields as required.

For links directing to the form.

/* To create a New Record */
<a href="thisfile.php?action=new">New Record</a>
/* To delete an Existing Record */
<a href="thisfile.php?action=delete&record=123">Delete #123</a>
/* To edit an Existing Record */
<a href="thisfile.php?action=edit&record=123">Delete #123</a>

For the form file (in my example thisfile.php itself).

<?php
if( isset( $_GET['action'] ) ){
  switch( strtolower( $_GET['action'] ) ){
    case 'delete' :
      /* Perform Deletion Action Here */
      break;
    case 'edit' :
      if( isset( $_POST['save'] ) ){
        /* Perform Modification of Existing Values Here */
      }else{
        /* Perform Query for Existing Values Here */
      }
      break;
    case 'new' :
      if( isset( $_POST['save'] ) ){
        /* Perform Creation of New Record Here */
      }else{
        /* Set Default Values */
        $navigation = '';
        $content    = '';
      }
      break;
  }
}
?>
<div class="containerholder">
  <div class="container">
    <div id="contain">
      <form method="post" id="customForm">
        <?php if( isset( $_GET['action'] ) ){ ?>
          <input type="hidden" name="action" value="<?php echo $_GET['action']; ?>" />
        <?php } ?>
        <?php if( isset( $_GET['record'] ) ){ ?>
          <input type="hidden" name="record" value="<?php echo $_GET['record']; ?>" />
        <?php } ?>
        <div>
          <label for="name">Navigation</label>
          <input id="name" name="navigation" type="text" value="<?php echo $navigation ?>" />
        </div>
        <div>
          <label for="message">Content</label>
          <textarea id="content" name="content" cols="" rows="" ><?php echo $content ?></textarea>
        </div>
        <div>
          <input id="send" name="send" type="submit" value="Save and update" />
        </div>
      </form>
    </div>
  </div>
</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜