简体   繁体   中英

Submit a form with multi submit buttons - PHP

I'm trying to submit an HTML form that has 3 submit buttons with different names but I can't identify which one was pressed to submit the form.

HTML:

<div class="col-md-12">
    <form id="validation-form" class="form-horizontal form-label-left"
    data-parsley-priority-enabled="false"
    novalidate="novalidate"
    action="./controller/productlineNewController.php" method="post">
        <div class="col-md-6">
            <section class="widget">
                <div class="body">
                    <fieldset>
                        <div class="form-group">
                            <label class="control-label col-md-4" for="pl_name">Product Line Name *</label>
                            <div class="col-md-6">
                                <input type="text" id="pl_name" name="pl_name" onBlur="checkAvailability()"
                                class="form-control input-transparent"
                                placeholder="Enter Product Line Name" required="required">
                                <span class="help-block" id="availability-status"></span>
                            </div>
                            <p><img src="img/small_loading.gif" id="loaderIcon" style="display:none" /></p>
                        </div>
                    </fieldset>
                </div>
            </section>
        </div>
        <div class="col-md-12">
            <section class="widget">
                <div class="form-actions">
                    <div class="row">
                        <div class="col-md-10 col-md-offset-2">
                            <button type="submit" name="save" class="btn btn-lg btn-success"
                            onclick="needToConfirm = false;">Save</button>&nbsp;
                            <button type="submit" name="save_new" class="btn btn-lg btn-success"
                            onclick="needToConfirm = false;">Save &amp; New Product Line</button>&nbsp;
                            <button type="submit" name="save_project" class="btn btn-lg btn-primary"
                            onclick="needToConfirm = false;">Save &amp; Add Project</button>&nbsp;
                            <button type="button" onclick="resetForm()"
                            class="btn btn-lg btn-danger">Reset Form</button>&nbsp;
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </form>
</div>

PHP - productlineNewController.php

print_r($_POST);
if (array_key_exists("save", $_POST)) {
    $action = TRUE;
    $new    = FALSE;
    $task   = FALSE;
    echo 'save';
    } elseif (array_key_exists('save_new', $_POST)) {
    $action = TRUE;
    $new    = TRUE;
    $task   = FALSE;
    echo 'save_new';
    } elseif (array_key_exists('save_project', $_POST)) {
    $action = TRUE;
    $new    = FALSE;
    $task   = TRUE;
    echo 'save_project';
}

Result:

Array ( [pl_name] => aaa )

I have tried isset function to check if any submit button was clicked but I got nothing.

I also tried to use input insted of buttons

<input type="submit" name="save2" id="save" class="btn btn-lg btn-success" onclick="needToConfirm = false;" value="Save">&nbsp;                                                

but same problem.

Please help.

edit

when I dropped this JS code from the HTML page it worked fine but I need this JS code to validate the form. can anyone fix this please.

        <script src="lib/parsleyjs/dist/parsley.min.js"></script>

This method used in Core PHP Try this in your way

<?php
    if (isset($_POST['save'])) {
        # save was clicked
    }
    else if (isset($_POST['save_new'])) {
        # save_new was clicked
    }
    else if (isset($_POST['save_project'])) {
        # save_project was clicked
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM