我想在一種形式中使用兩個提交按鈕。聽起來不難......這也是我的想法。看起來提交按鈕的變量不會發送。 它只用一個按鈕就可以正常工作,或者對按鈕沒有區別。一種形式的兩個按鈕
message.js只是顯示toastr錯誤/成功消息。
form_handler.php:
<?php
require_once ('dbconnect.php');
\t $nr = $_POST["nr"];
$description = $_POST["description"];
\t $price = $_POST["price"];
\t $size = $_POST["size"];
if (isset($_POST["submit"]) && $_POST["submit"] == "Add") {
\t if(isset($_POST["nr"], $_POST["description"], $_POST["price"])){
\t \t $sql =" INSERT INTO artikelstamm (nr, description, size, price) VALUES ('$nr', '$description', '$size', '$price')";
\t \t $ergebnis = mysqli_query($db_link, $sql);
\t \t \t //or die("Error: ".mysqli_error($db_link));
\t \t if(!$ergebnis){
\t \t \t header('Content-type: text/javascript');
\t \t $arr = array(
\t \t 'message' => '...',
\t \t 'title' => 'Error'
\t \t);
\t \t echo json_encode($arr);
\t \t }else{
\t \t \t header('Content-type: text/javascript');
\t \t \t $arr = array(
\t \t \t 'message' => '...',
\t \t \t 'title' => 'Success'
\t \t \t);
\t \t \t echo json_encode($arr);
\t \t }
\t }
} elseif (isset($_POST["submit"]) && $_POST["submit"] == "Delete") {
\t 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 \t \t \t \t \t \t \t \t \t \t \t \t require_once('article/showArticles.php'); \t \t \t
?>
\t \t </div>
\t \t <div class="3u">
\t <section>
\t \t Form here<br /><br />
\t \t \t <form action="form_handler.php" method="post" id="schuma">
\t \t \t \t <input type="text" name="nr" placeholder="Nr" /><br />
\t \t \t \t <input type="text" name="description" placeholder="Description" /><br />
\t \t \t <input type="text" name="size" placeholder="Size" /><br />
\t \t \t \t <input type="text" name="price" placeholder="Price" /><br />
\t \t \t \t <input id="btnAdd" type="submit" name="submit" value="Add" />
\t \t \t \t <input id="btnDel" type="submit" name="submit" value="Delete" />
\t \t \t </form>
\t \t \t <script src="article/message.js"></script> \t \t \t \t \t \t \t \t \t \t \t \t
\t \t </section>
\t \t </div>
\t </div>
\t </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) {
\t 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;
});
編輯:新代碼
在form_handler.php之前,如果嘗試打印接線柱陣列是這樣的:'的print_r($ _ POST);'看你在POST收到在每種情況下什麼。 –
@RiggsFolly你能強調一下嗎?我不明白你的評論是什麼意思。 – CodeGodie
我沒有看到你的代碼有什麼問題。它應該按照它的方式工作。 – CodeGodie