I am trying to make a simple incentive calculator.
PROBLEMS: 1. When I am submitting the form the values of the fields and field names are showing are showing up in the address bar. eg: "http://localhost/incentive%20tracker/?jQueryDatePicker1=09%2F05%2F2015&pid=n&qty=1&memo=&remarks=&add=Add"
-
When I try to submit the values in the first attempt just after the first load of the page, the values are not getting inserted in the database. After the first attempt, the values do get inserted.
-
On success in insertion of data in the database, jquery doesnt show me any message inspite of having the success function.
-
I am not being able to display an error message if the qty field is negative.
I am not sure what i am doing wrong. Expecting some help. Thanks in advance.
HTML
<form id="saleDetail">
<input type="text" id="jQueryDatePicker1" name="jQueryDatePicker1" value="06/05/2015">
<input type="text" name="pid" id="pid" required placeholder="Enter a product code">
<datalist id="products">
<?php
$sql="SELECT * FROM products";
$result = mysqli_query($db, $sql);
while($fetch_options=mysqli_fetch_array($result)){
?>
<option value="<?php echo $fetch_options['pid']; ?>"><?php echo $fetch_options['pid']; ?></option>
<?php
}
?>
</datalist>
<input type="number" id="qty" name="qty" value="" autocomplete="off" required placeholder="Enter the Quantity">
<input type="text" id="memo" name="memo" value="" autocomplete="off" placeholder="Enter the Cash memo number">
<textarea name="remarks" id="remarks" rows="2" cols="27" placeholder="Any remarks"></textarea>
<input type="submit" id="add" name="add" value="Add">
<div id="message"></div>
</form>
JQuery
<script>
$(document).ready(function()
{
var jQueryDatePicker1Opts =
{
dateFormat: 'dd/mm/yy',
changeMonth: false,
changeYear: false,
showButtonPanel: false,
showAnim: 'slideDown'
};
$("#jQueryDatePicker1").datepicker(jQueryDatePicker1Opts);
$("#jQueryDatePicker1").datepicker("setDate", "new Date()");
$("#PanelMenu1").panel({animate: true, animationDuration: 200, animationEasing: 'swing', dismissible: true, display: 'push', position: 'right'});
$('#add').click(function()
{
var date=$('#jQueryDatePicker1').val();
var pid=$('#pid').val();
var qty=$('#qty').val();
var remarks=$('#remarks').val();
var memo=$('#memo').val();
if(pid==="" || qty==="" || qty<0){
$("#message").html("<span style='color:#cc0000'>Please fill in the required fields!");
}
else{
var dataString='date='+date+'&pid='+pid+'&qty='+qty+'&remarks='+remarks+'&memo='+memo;
$.ajax({
type:"POST",
data: dataString,
url: "add.php",
cache:false,
success: function(html){
$("#message").html("Data Added successfully! ");
}
});
}
});
return false;
});
</script>
PHP
<?php
include 'connect.php';
$error_message = "";
$mysql_table = "sales";
//if ($_SERVER['REQUEST_METHOD'] == 'POST')
if (isset($_POST['pid']) && isset($_POST['qty']))
{
$newdate =$_POST['date'];
$newpid = $_POST['pid'];
$newqty = $_POST['qty'];
$newremarks = $_POST['remarks'];
$newmemo = $_POST['memo'];
$newpid = mysqli_real_escape_string($db, $newpid);
$newqty = mysqli_real_escape_string($db, $newqty);
$newremarks = mysqli_real_escape_string($db, $newremarks);
$newmemo = mysqli_real_escape_string($db, $newmemo);
$sql = "SELECT incentive FROM products WHERE pid = '$newpid'";
$result = mysqli_query($db, $sql);
$rate=mysqli_fetch_array($result);
$amt = $rate['incentive'] * $newqty;
$sql = "INSERT `".$mysql_table."` (`date`, `pid`, `qty`, `amt`, `memo`, `remarks`) VALUES ('$newdate', '$newpid', '$newqty', '$amt', '$newmemo', '$newremarks')";
$results = mysqli_query($db, $sql);
echo "Success";
mysqli_close($db);
}
?>
Aucun commentaire:
Enregistrer un commentaire