I am trying to use two submit buttons in one form. Doesn't sound hard... that's what I thought, too. It seems the variable of the submit buttons won't get send. It works fine with just one button or no differentiation for the buttons.
The message.js is just to show a toastr error/success message.
form_handler.php :
<?php require_once ('dbconnect.php'); $nr = $_POST["nr"]; $description = $_POST["description"]; $price = $_POST["price"]; $size = $_POST["size"]; if (isset($_POST["submit"]) && $_POST["submit"] == "Add") { if(isset($_POST["nr"], $_POST["description"], $_POST["price"])){ $sql =" INSERT INTO artikelstamm (nr, description, size, price) VALUES ('$nr', '$description', '$size', '$price')"; $ergebnis = mysqli_query($db_link, $sql); //or die("Error: ".mysqli_error($db_link)); if(!$ergebnis){ header('Content-type: text/javascript'); $arr = array( 'message' => '...', 'title' => 'Error' ); echo json_encode($arr); }else{ header('Content-type: text/javascript'); $arr = array( 'message' => '...', 'title' => 'Success' ); echo json_encode($arr); } } } elseif (isset($_POST["submit"]) && $_POST["submit"] == "Delete") { print_r($_POST); } HTML:
<!-- Content --> <article class="first"> <h2>Article</h2> </article> <ul class="nav nav-tabs"> <li class="active"><a data-toggle="tab" href="#tab1">Tab1</a></li> <li><a data-toggle="tab" href="#tab2">Tab2</a></li> <li><a data-toggle="tab" href="#tab3">Tab3</a></li> <li><a data-toggle="tab" href="#tab4">Tab4</a></li> </ul> <div class="tab-content"> <div id="tab1" class="tab-pane fade in active"> <div class="container"> <div class="row"> <div class="9u skel-cell-important"> <?php require_once('article/showArticles.php'); ?> </div> <div class="3u"> <section> Form here<br /><br /> <form action="form_handler.php" method="post" id="schuma"> <input type="text" name="nr" placeholder="Nr" /><br /> <input type="text" name="description" placeholder="Description" /><br /> <input type="text" name="size" placeholder="Size" /><br /> <input type="text" name="price" placeholder="Price" /><br /> <input id="btnAdd" type="submit" name="submit" value="Add" /> <input id="btnDel" type="submit" name="submit" value="Delete" /> </form> <script src="article/message.js"></script> </section> </div> </div> </div> </div> message.js :
$('#schuma').on('submit', function(){ var that = $(this), contents = that.serialize(); $.ajax({ url: 'form_handler.php', type: 'POST', data: contents, dataType: 'JSON', success: function(data) { console.log(data) toastr.options = { "closeButton": true, "debug": false, "positionClass": "toast-top-full-width", "onclick": null, "showDuration": "20000", "hideDuration": "20000", "timeOut": "20000", "extendedTimeOut": "20000", "showEasing": "swing", "hideEasing": "linear", "showMethod": "fadeIn", "hideMethod": "fadeOut" } if(data.title == "Success"){ toastr.success(data.message, data.title); } else{ toastr.error(data.message, data.title); } } }); return false; }); EDIT: New Code
print_r($_POST);to see what you receive in POST in each case.print_r($_POST);just shows the names and values of the<input type="text". The$_POSTwon't get send...