add ajax to lost form

This commit is contained in:
/jedi/ 2018-12-30 12:50:42 +01:00
parent f949d07115
commit df470b370f
4 changed files with 93 additions and 25 deletions

View file

@ -16,19 +16,63 @@ switch($_GET["action"]) {
case "add_featurerequest": case "add_featurerequest":
if (hasval($_POST["title"]) && hasval($_POST["desc"])) { if (hasval($_POST["title"]) && hasval($_POST["desc"])) {
/* Prepared statement, stage 1: prepare */ /* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("INSERT INTO feature_request(title, desc) VALUES (?, ?)"))) { if (!($stmt = $mysqli->prepare("INSERT INTO feature_request(title, `desc`) VALUES (?, ?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; $errormsg = "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}else
if (!$stmt->bind_param("ss", $_POST["title"], $_POST["desc"])) {
$errormsg = "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}else
if (!$stmt->execute()) {
$errormsg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
}else{
$errormsg = "all values have to be set";
}
break;
case "insert":
if (hasval($_POST["was"]) && hasval($_POST["wann"]) && hasval($_POST["wo"])) {
/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("INSERT INTO items(was, wann, wo) VALUES (?, ?, ?)"))) {
$errormsg = "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
} }
if (!$stmt->bind_param("sss", $_POST["was"], $_POST["wann"], $_POST["wo"])) { if (!$stmt->bind_param("sss", $_POST["was"], $_POST["wann"], $_POST["wo"])) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; $errormsg = "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
} }
if (!$stmt->execute()) { if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; $errormsg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
} }
}else{
$errormsg = "all values have to be set";
} }
break;
case "add_lost":
if (hasval($_POST["was"]) && hasval($_POST["wann"]) && hasval($_POST["wo"]) && hasval($_POST["contact"])) {
/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("INSERT INTO lost(was, wann, wo, contact) VALUES (?, ?, ?, ?)"))) {
$errormsg = "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("ssss", $_POST["was"], $_POST["wann"], $_POST["wo"], $_POST["contact"])) {
$errormsg = "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
$errormsg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
}else{
$errormsg = "all values have to be set";
}
break;
case "get_stats";
echo json_encode(array("status"=>"ok","stats"=>get_stats()));
break;
default:
$errormsg = "action unknown";
break; break;
} }
echo json_encode(array("get"=>$_GET,"post"=>$_POST)); if(empty($errormsg))
echo json_encode(array("get"=>$_GET,"post"=>$_POST,"status"=>"ok","message"=>"added one item"));
else
echo json_encode(array("get"=>$_GET,"post"=>$_POST,"status"=>"error","message"=>$errormsg));
?> ?>

View file

@ -19,6 +19,15 @@
<li class="breadcrumb-item active">Add</li> <li class="breadcrumb-item active">Add</li>
</ol> </ol>
<div id="alertmessage" class="collapse alert alert-danger" role="alert">
This is a danger alert—check it out!
</div>
<div id="successmessage" class="collapse alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<!-- Area Chart Example--> <!-- Area Chart Example-->
<div class="card mb-3"> <div class="card mb-3">
<div class="card-header"> <div class="card-header">
@ -26,7 +35,7 @@
Insert Item into DB Insert Item into DB
</div> </div>
<div class="card-body"> <div class="card-body">
<form method="POST" action="/action.php?action=lost"> <form method="POST" action="/action.php?action=lost" id="lost_form">
<p><input type="text" class="form-control" placeholder="WANN" name="wann" value="<?php echo date("Y-m-d H:i:s");?>"></p> <p><input type="text" class="form-control" placeholder="WANN" name="wann" value="<?php echo date("Y-m-d H:i:s");?>"></p>
<p><input type="text" class="form-control" placeholder="WO" name="wo"></p> <p><input type="text" class="form-control" placeholder="WO" name="wo"></p>
<p><input type="text" class="form-control" placeholder="WAS" name="was"></p> <p><input type="text" class="form-control" placeholder="WAS" name="was"></p>
@ -43,14 +52,21 @@
<script> <script>
function testajax(){ function testajax(){
$.post( "ajax.php?action=add_lost", $( "#lost_form" ).serialize() ).done(function( data ) { $.post( "/ajax.php?action=add_lost", $( "#lost_form" ).serialize() ).done(function( data ) {;
alert( "Data Loaded: " + data ); reply = JSON.parse(data);
if(reply.status == "ok"){
$( "#lost_form" ).trigger("reset");
$("#alertmessage").collapse('hide');
$("#successmessage").html(reply.message).collapse('show');
}else{
$("#successmessage").collapse('hide');
$("#alertmessage").html(reply.message).collapse('show');
}
}).fail(function() {
$("#successmessage").collapse('hide');
$("#alertmessage").html("error").collapse('show');
}); });
/*$.post( "ajax.php", { name: "John", time: "2pm" } ).done(function( data ) {
alert( "Data Loaded: " + data );
});*/
} }
</script> </script>

View file

@ -16,10 +16,15 @@
<li class="breadcrumb-item active">Feature Request</li> <li class="breadcrumb-item active">Feature Request</li>
</ol> </ol>
<div class="alert alert-danger" role="alert"> <div id="alertmessage" class="collapse alert alert-danger" role="alert">
This is a danger alert—check it out! This is a danger alert—check it out!
</div> </div>
<div id="successmessage" class="collapse alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<!-- Area Chart Example--> <!-- Area Chart Example-->
<div class="card mb-3"> <div class="card mb-3">
<div class="card-header"> <div class="card-header">
@ -27,7 +32,7 @@
Add Feature Request Add Feature Request
</div> </div>
<div class="card-body"> <div class="card-body">
<form id="lost_form" method="POST" action="action.php?action=lost"> <form id="request_form" method="POST" action="/action.php?action=lost">
<div class="form-group"> <div class="form-group">
<label for="title_field">Title</label> <label for="title_field">Title</label>
<input type="text" class="form-control" placeholder="title" name="title" id="title_field"> <input type="text" class="form-control" placeholder="title" name="title" id="title_field">
@ -50,17 +55,20 @@
<script> <script>
function testajax(){ function testajax(){
$.post( "ajax.php?action=add_lost", $( "#lost_form" ).serialize() ).done(function( data ) { $.post( "/ajax.php?action=add_featurerequest", $( "#request_form" ).serialize() ).done(function( data ) {;
alert( "Data Loaded: " + data ); reply = JSON.parse(data);
if(reply.status == "ok"){
$( "#lost_form" ).trigger("reset");
$("#alertmessage").collapse('hide');
$("#successmessage").html(reply.message).collapse('show');
}else{
$("#successmessage").collapse('hide');
$("#alertmessage").html(reply.message).collapse('show');
}
}).fail(function() { }).fail(function() {
alert( "error" ); $("#successmessage").collapse('hide');
}).always(function() { $("#alertmessage").html("error").collapse('show');
alert( "finished" ); });
});;
/*$.post( "ajax.php", { name: "John", time: "2pm" } ).done(function( data ) {
alert( "Data Loaded: " + data );
});*/
} }

View file

@ -83,7 +83,7 @@
<span>Tables</span></a> <span>Tables</span></a>
</li--> </li-->
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/?page=request"> <a class="nav-link" href="/request/">
<i class="fas fa-fw fa-comments"></i> <i class="fas fa-fw fa-comments"></i>
<span>Feature Requests</span> <span>Feature Requests</span>
</a> </a>