skip to Main Content

I am trying to submit a single form with multiple submit buttons. The form is submitted using Javascript. I am using the below api to create a table list with check box.

http://download1.parallels.com/Plesk/PP11/11.5/Doc/en-US/online/plesk-extensions-reference/classes/pm_View_List_Simple.html

My controller file for creating the list is

$data[] = array(
                'column-1' => '<form action="index.php/index/sign" method="post" id="form1">
                                <input type="checkbox" name="vehicle[]" value="Bike">I have a bike<br>
                                <input type="checkbox" name="vehicle[]" value="Car">I have a car<br>',
                'column-2' => '<img src="' . $iconPath . '" /> image #',
            );

$list = new pm_View_List_Simple($this->view, $this->_request);
        $list->setData($data);
        $list->setColumns(array(
            'column-1' => array(
                'title' => 'select',
                'noEscape' => true,
                'searchable' => true,
            ),
            'column-2' => array(
                'title' => 'Description',
                'noEscape' => true,
                'sortable' => false,
            ),
        ));

$tools = array (
         array (
             'title' => 'Add',
             'class' => 'sb-activate',
             'link' =>  'javascript: submitform()',
         ),
         array (
             'title' => 'Delete',
             'class' => 'sb-activate',
             'link' =>  'javascript: submitform1()',
         ),
     );

In the above example you can see that there is multiple submit buttons which does adding and deletion respectively. Both after submitting will goes to action sign means the view file will be sign.phtml. The main index view file list is pasted below.

<script type="text/javascript">
function submitform()
{
 document.forms["form1"].submit();
alert document.getElementById();
}

function submitform1()
{
        document.forms["form1"].submit();
}
</script>

<?php echo $this->renderList($this->list); ?>

My issue is both the form submission redirects to the same action sign with only checkbox values. But I need to do different things for different form submission. So I need to differentiate add and delete in the sign action. Will I be able to send any parameter from javascript function to PHP sign function so that I can differentiate the button in PHP side.

Kindly don’t suggest normal html submit buttons with different names because I cannot do this in this api.

4

Answers


  1. It would be possible to create a hidden element with the unique value on the fly. Try that.

    Login or Signup to reply.
  2. You can have multiple submit buttons per form, just check the value in the post data.

    HTML:

    <input type="submit" name="add" value="Add">
    <input type="submit" name="delete" value="Delete">
    

    PHP:

    if($_POST['add']) {
        // add
    } else if($_POST['delete']) {
        // delete
    }
    

    EDIT: I read your question too quickly. Here’s a solution in JavaScript that may work for you.

    HTML

    <form name="form1">
        <input id="formAdd" type="button" value="Add">
        <input id="formDelete" type="button" value="Delete">
    </form>
    

    JavaScript

    document.getElementById("formAdd").addEventListener('click', function() { 
        // do stuff for add
        // submit form
    });
    
    document.getElementById("formDelete").addEventListener('click', function() {
        // do stuff for delete
        // submit form
    }); 
    

    Demo: http://jsfiddle.net/MK7u9/

    Login or Signup to reply.
  3. you can create a input hidden element and on checkbox check/uncheck action you can assign value to that hidden element using Javascript and check the corresponding hidden element value on server side(php) to differentiate.

    Login or Signup to reply.
  4. $data[] = array('column-1' => '<form action="index.php/index/sign" method="post" id="form1">
    <input type="checkbox" name="vehicle[]" value="Bike">I have a bike<br>
    <input type="checkbox" name="vehicle[]" value="Car">I have a car<br>
    
    <--Start: Add this line to -->
    <input type="hidden" name="actionVal" id="actionVal" value="">
    <--End: Add this line to -->',
    'column-2' => '<img src="' . $iconPath . '" /> image #',
    );
    
    
    <script type="text/javascript">
    function submitform(){
       <--Start: Add this line to -->
        document.getElementById('actionVal').value = 'add';
        <--End: Add this line to -->
        document.forms["form1"].submit();
    }
    function submitform1()
    {
        <--Start: Add this line to -->
        document.getElementById('actionVal').value = 'delete'
         <--End: Add this line to -->
        document.forms["form1"].submit();
    }
    </script>
    
    
    //PHP 
    
    if($_POST['actionVal']=='add') {
    // add
    } else if($_POST['actionVal']=='delete') {
    // delete
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search