1

I have a form in my code

<form class="validate form-horizontal" role="form" id="formLogin" action='actions.php' method="post" enctype="multipart/form-data">
    <input type="hidden" name="id" value="<?php echo @$selectedUser['id'];?>" />
    <input type="hidden" name="j" value="<?php echo "3"; ?>" />

    <div class="form-group clearfix">
        <div class="col-lg-offset-4 col-lg-9">
            <button type="submit" class="btn btn-success">&nbsp;&nbsp;&nbsp;Save&nbsp;&nbsp;&nbsp;</button>
            <a class="btn btn-default" href="index.php">Cancel</a>
        </div>
    </div>

    <div class="form-group clearfix">
        <div class="col-lg-offset-4 col-lg-9">
            <button type="submit" class="btn btn-success">&nbsp;&nbsp;&nbsp;Approve&nbsp;&nbsp;&nbsp;</button>
            <input style="margin-left: 10px;" type="checkbox" name="emailCheckBox" style="height: 25px">
            <label style="margin-left: 10px;" class="control-label">Email Tami</label>
        </div>
    </div>
</form>

I have two different buttons here , Save and Approve. There are atleast 7 more input fields in this form. Now i want it, so that when save button is clicked, it should send value of j = 3 and if Approve is pressed, it should send value of j to be 4

How can I override the form activity and maybe write some code in javascript to achive this, keeping in mind the data of other 7 etc input fields is being sent too, and its same for both buttons

2 Answers 2

1

You just have to add necessary attributes for that purpose:

<button type="submit" name="action" value="save" class="btn btn-success">&nbsp;&nbsp;&nbsp;Save&nbsp;&nbsp;&nbsp;</button>
<button type="submit" name="action" value="approve" class="btn btn-success">&nbsp;&nbsp;&nbsp;Approve&nbsp;&nbsp;&nbsp;</button>

In this example, add the name="action" and their respective values for each button:

value="save"
value="approve"

Then in PHP:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $action = $_POST['action']; // the value here will be either "save" or "approve"
    // so now your next course will depend on this value
    $j = null;
    if($action == 'save') {
        $j = 3;
    } elseif ($action == 'approve') {
        $j = 4;
    }
}

Or if you do not want to go this route, then you can just manipulate it client-side:

$('button[name="action"]').on('click', function(e){
    e.preventDefault();
    var action = $(this).attr('value');
    if(action == 'save') {
        $('input[name="j"]').attr('value', 3);
    } else if(action == 'approve') {
        $('input[name="j"]').attr('value', 4);
    }
    $('#formLogin').submit();
});

Then in PHP, just normally access $_POST['j'];

Sign up to request clarification or add additional context in comments.

Comments

1

Set the name for the form

<form class="validate form-horizontal" role="form" name="form" id="formLogin" action='actions.php' method="post" enctype="multipart/form-data">

.

A pure vanilla javascript would be

var button = document.querySelectorAll('.btn-success');
var input = document.forms['form'].j;

document.forms['form'].addEventListener('submit', function(e) {
    e.preventDefault();
}) 

button[1].addEventListener('mousedown', function() {
    input = parseInt(input.value) +1;
    console.log(input);
    document.forms['form'].submit();
})

button[0].addEventListener('mousedown', function() {
    document.forms['form'].submit();
})

.

JSFIDDLE: http://jsfiddle.net/TRNCFRMCN/2aa52wak/2/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.