I have been trying to create a web-based stopwatch in which the value of start time and stop time will be stored into mysql. The stopwatch is created in javascript, and I use jquery to store it to mysql. The problem is whenever I click 'Stop'on my stopwatch, the value is inserted twice in mysql while it should only be inserted once. Here's my code snippet :
function stopTimers() {
clearInterval(_myTimer_ms);
clearInterval(_myTimer_s);
clearInterval(_myTimer_m);
clearInterval(_myTimer_h);
$(document).ready(function(){
//Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...
$("#stop").click(function(){
//Get values of the input fields and store it into the variables.
var cell=$("#cell").val();
var machine=$("#machine").val();
var hour=$("#hour").val();
var tmin=$("#tmin").val();
var sec=$("#sec").val();
var mssec=$("#mssec").val();
//use the $.post() method to call insert.php file.. this is the ajax request
$.post('insert.php', {cell: cell,machine: machine,hour: hour,tmin : tmin,sec: sec,mssec: mssec},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(100); //Fade in the data given by the insert.php file
}
);
return false;
});
});
}
and here's my insert.php code :
<?php
//Configure and Connect to the Databse
$con = mysql_connect("localhost","diki","diki");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("diki", $con);
//Pull data from home.php front-end page
$my_date = date("Y-m-d H:i:s");
//$my_time =
$cell=$_POST['cell'];
$machine=$_POST['machine'];
$hour=$_POST['hour'];
$tmin=$_POST['tmin'];
$sec=$_POST['sec'];
$mssec=$_POST['mssec'];
//Insert Data into mysql
$query=mysql_query("INSERT INTO diki02(cell,machine,hour,tmin,sec,mssec,date,Stoptime) VALUES('$cell','$machine','$hour','$tmin','$sec','$mssec','$my_date',NOW())");
if($query){
echo "Data for $cell and $machine inserted successfully!";
}
else{ echo "An error occurred!"; }
?>
Still figuring out why it's inserted twice,, anybody ever encountered the same problem ?
THankss
Try this script block
'removed $(document).ready(function(){}); ' Block
//Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...
$("#stop").click(function () {
clearInterval(_myTimer_ms);
clearInterval(_myTimer_s);
clearInterval(_myTimer_m);
clearInterval(_myTimer_h);
//Get values of the input fields and store it into the variables.
var cell = $("#cell").val();
var machine = $("#machine").val();
var hour = $("#hour").val();
var tmin = $("#tmin").val();
var sec = $("#sec").val();
var mssec = $("#mssec").val();
//use the $.post() method to call insert.php file.. this is the ajax request
$.post('insert.php', { cell: cell, machine: machine, hour: hour, tmin: tmin, sec: sec, mssec: mssec },
function (data) {
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(100); //Fade in the data given by the insert.php file
});
return false;
});
Since your $.post is in $(document).ready() I presume it'll be called directly when the DOM is ready (without any other action from the user or javascript).
Being in the the stopTimer() function as well, it may be called a second time when you call this function.
Related
I'm trying when i submit a value to jqgrid box on multiple selected rows to Update the data of specific columns.My code is this but when i click OK in jqgrid nothing happens and function is not called :
jQuery(document).ready(function(){
jQuery('#list1').jqGrid('navButtonAdd', '#list1_pager',
{
'caption' : 'Resubmit',
'buttonicon' : 'ui-icon-pencil',
'onClickButton': function()
{
var str = prompt("Please enter data of Column")
var selr = jQuery('#list1').jqGrid('getGridParam','selarrrow');
$(selector).load('Updatestatus.php', {'string': str,'box[]' : selr })
},
'position': 'last'
});
});
The function that updates the column of the table:
function update_data($data)
{
// If bulk operation is requested, (default otherwise)
if ($data["params"]["bulk"] == "set-status")
{
$selected_ids = $data["cont_id"];
$str = $data["params"]["data"];
mysql_query("UPDATE out_$cmpname SET cont_status = '$str' WHERE cont_id IN ($selected_ids)");
die;
}
}
I'm new to jqgrid and Jquery.What can i do to call and execute this function when i click ok?
Thanks in advance!
You'll need a Ajax-call for this. I see you're using jQuery, have a look at http://api.jquery.com/load/
With this function, you can load PHP or HTML with jQuery to a certain element.
In the below code the $.POST method passes two variables to the php file empProfile.php which retrieves the employee profile from the database. Know I want to receive ID and Name from the php file to the jquery variables. Can someone help me.
data = {
personnelNo: $('#personnelNo').val(),
cnic: $('#cnic').val()
} //end of data
$.post(
"checkEmployee.php",
data,
function(data) {
var ID =; Name=;
} // end of function
); // end of post
//} //end of if statement
}); // end of blur
Try this
var ID = '<?php echo $yourId;?>';
var Name='<?php echo $yourname;?>';
How to post data to a php script (showitemid.php) using ajax and open the same script (showitemid.php) immediately in a thickbox on a hyperlink click and display that posted data. Below is my code:
postitemid.php
This file consists of multiple check-boxes. The user will tick the checkboxes and click a hyperlink. On clicking the hyperlink all the selected check-box values would be posted to showitemid.php and then immediately showitemid.php would open in a thickbox and display the received values. But it isn't receiving any values in my code ? Need help.
$('#showitem).click(function()
{
var data = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
$.ajax({type: 'POST',
url: 'showitemid.php',
data: data,success: success,dataType: dataType});
});
showitemid.php
$data = '';
if (isset($_POST['data']))
{
$data = $_POST['data'];
}
elseif (isset($_GET['data']))
{
$data = $_GET['data'];
}
echo 'd='.$data;
use something like this
$('#showthickbox').click(function()
{
var data = $('input:checkbox:checked').map(function() {
return this.value;
}).get();//your code
$.post("showitemid.php",{data:data},function(data){
$("#thickbox").html(data);
})
})
})
in your showitemid.php
echo "your data";
The main problem with the original code is the data being sent has no key named data to match $_POST['data']) so $_POST['data']) is empty. You have to send key/value pairs, you are only sending a value with no key. You are likely getting a bit confused since you use the same variable name constantly
var dataArray = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
var dataToServer= { data : dataArray} /* now have the key to match $_REQUEST in php */
Now can use AJAX shorthand method load() to populate content
$('#myContainer').load( "yourfile.php", dataToServer, function(){
/* new html exists run open thickbox code here*/
})
The issue:
I have a timesheet application. It has an SQlite database. I am trying to find a way to present a GUI so that if the user clicks a square(a ) i need to paas the data from the template to the model (paris) so i can save it in theb SQlite database.
There are three tables one for users one for the timesheet and one for the department.
It is a timesheet like application.
The setup:
Slim php
Idiorm/Paris
SQlite3
Does anyone know a good way to make the user click a so that the data is passed on to the model from the view?
Thank you in advance!
Imagine this is your view
<html>
<head>
$(document).ready(function() {
$("#add").on("click", function(e) {
var user_id = $('#user_id').val();
var time = date();
var department = $('#department').val();
var data = {uid:user_id, time:time, dept:department};
$.ajax({
url: "add_into_db.php",
type: 'POST',
data: data,
success: function(msg) {
if(msg=="true"){
alert("Your dats is inserted successfully");
document.location.reload(true);
}
else{
alert("Your data insertion failed");
return false;
}
}
});
e.preventDefault();
});
});
</head>
<body>
// Some html here
// input field for user
// input field for time
// input field for department
// whatever data you want os send on click, include it here
<input id ='add' type= 'submit' value 'Add'/> //Your submit button to add data via AJAX
</body>
Yout php function which will add data into database
function add_into_db(){
$user id = $_POST['user_id'];
$time = $_POST['time'];
$dept = $_POST['department'];
// connect to your db
// run your insert query
If (insertion is successfull) {
$msg = 'true';
echo $msg;
}
else{
$msg = 'false';
echo $msg
}
}
Hope it ill help you
I have a form which has a input textbox and submit button.
On submission of the form the textbox value should get pass to an php script and check the values whether it exists in the Mysql Database. If it exists then we need to show an alert box stating that "Entered Value is already exists, Try something new". If the value not exists the form can be submitted to the php script which is in the form action.
I tried with the jquery and the code is below:
$(document).ready(function() {
$("#form_add").submit(function () {
var pval = $("#name").val();
var datastring = 'pname'+pval;
$.post("unique_valid.php", {pname:pval }, function (data){
alert('duplicate');
});
return false;
});
});
Problem with this code is It shows alert box on every case but its not allowing to submit the form if the values is not exists in the database.
Php code :
$pname = $_POST['pname'];
if( $pname == $row['name']){
echo "success";
}else{
echo "failure";
}
Suggest the better solution for this problem.
That's because you're alerting 'duplicate' no matter what the PHP output is. Try checking the value of data before alerting, like this:
$(document).ready(function() {
$("#form_add").submit(function () {
var pval = $("#name").val();
var datastring = 'pname'+pval;
$.post("unique_valid.php", {pname:pval },
function (data){
if(data == 'failure'){
alert('duplicate');
}else{
alert('not a duplicate');
}
});
return false;
});
});
And I'm assuming your PHP code will actually be saving the record if it's not a duplicate (your code doesn't indicate as much, though)?