0

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

14
  • in form_handler.php before if try to print the post array like this: print_r($_POST); to see what you receive in POST in each case. Commented Mar 21, 2016 at 14:21
  • @RiggsFolly can you please emphasize? Im not understanding what you mean by your comment. Commented Mar 21, 2016 at 14:24
  • I dont see anything wrong with your code. It should work the way it is. Commented Mar 21, 2016 at 14:26
  • print_r($_POST); just shows the names and values of the <input type="text". The $_POST won't get send... Commented Mar 21, 2016 at 14:31
  • @Skar can you give us some feedback? What browser are you using? Commented Mar 21, 2016 at 14:32

2 Answers 2

1

Ok this took a while because you werent showing all your code. It seems that your form is actually being submitted through AJAX, not through PHP as you originally showed. You are disabling the form's way of submitting and using AJAX to submit, which is perfectly fine, however in your AJAX you are using .serialize() to obtain all your formdata, the problem is that .serialize() will not include the button with type="submit". Therefore your PHP ending up with that field not set.

To fix this, you need to modify your JS and manually insert that info to the content sent. We can do this with .serializeArray() in the following manner:

$('form#schuma input[type=submit]').click(function () { var contents = $(this).closest('form').serializeArray(); contents.push({name: "submit", value: this.value}); $.ajax({ url: 'form_handler.php', type: 'POST', data: contents, dataType: 'JSON', success: function (data) { console.log(data); // your other code } }); return false; }); 
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you very much. I wasn't really aware of that and did not get how everything is connected with each other. Much to learn, i still have ;P I am going to test it and get back to you after that :)
No problem. Let me know.
Sweet. Im glad I was able to help. Please accept my answer by clicking the checkmark next to my answer if you found this helped. :)
The differentiation works fine, but it seems the error/success message wont display anymore. It just displays the json message on a blank page. It was defintiely very helpful :)
since you are specifying dataType: 'JSON', you might also need to modify your PHP headers, so before the echo json.. do header('Content-Type: application/json');
|
1

I believe it is better to give the same name:

<input id="btnAdd" type="submit" name="submit" value="Add" /> <input id="btnDel" type="submit" name="submit" value="Delete" /> 

And in the PHP, you can check:

if (isset($_POST["submit"]) && $_POST["submit"] == "Add") { // Do add } elseif (isset($_POST["submit"]) && $_POST["submit"] == "Delete") { // Do delete } 

14 Comments

Thanks for the feedback. I would like to know why is this approach wrong. Thanks. If someone doesn't agree with the answer, kindly let me know why. I just checked this and it works fine. :)
Sorry, finger trouble, not intended to DV, just a woops
Thats the button I intended to hit
@RiggsFolly Thanks buddy! :P Same two button at the same place problem. LoL.
@CodeGodie I don't find a reason now why this doesn't work.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.