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
Related
I have a HTML table and Edit - Delete - Save images are associated with each row. Now I want to edit any cell and when I'll click on Save-image it will be displayed on the HTML table with updated values, as well as the associated table in the database will also be changed. I want to do this using Jquery Ajax and PHP but in MVC way. I have tried but somehow database changes are not happening, while HTML table is showing the updated values.
Ajax Code:
function Save()
{
var par = $(this).parent().parent();
var tdCid = par.children("td:nth-child(2)");
var tdCname = par.children("td:nth-child(3)");
var tdCtype = par.children("td:nth-child(4)");
var tdRegno = par.children("td:nth-child(5)");
var tdFare = par.children("td:nth-child(6)");
var tdNightfare = par.children("td:nth-child(7)");
var tdButtons = par.children("td:nth-child(8)");
var Cid = $("#tdCid").val();
var Cname = $("#tdCname").val();
var Ctype = $("#tdCtype").val();
var Regno = $("#tdRegno").val();
var Fare = $("#tdFare").val();
var Nightfare = $("#tdNightfare").val();
$.ajax({
url:"cardetails.php",
type:"POST",
data:{cid:Cid, name:Cname, cartype:Ctype, regno:Regno,fare:Fare, nfare:Nightfare},
success: function(response){
if(response==1){
alert('Data Inserted / Modified Successfully');
}
else
{
alert('Error !! Data Insertion / Modification Failed');
}
}
})
tdCid.html(tdCid.children("input[type=text]").val());
tdCname.html(tdCname.children("input[type=text]").val());
tdCtype.html(tdCtype.children("input[type=text]").val());
tdRegno.html(tdRegno.children("input[type=text]").val());
tdFare.html(tdFare.children("input[type=number]").val());
tdNightfare.html(tdNightfare.children("input[type=number]").val());
tdButtons.html("<img src='delete.png' class='btnDelete'/><img src='edit.png' class='btnEdit'/>");
$(".btnEdit").bind("click", Edit);
$(".btnDelete").bind("click", Delete);
};
cardetails.php script:
include('helper.php');
$car = new Admin;
$r=$car->update($_POST['cid'],$_POST['name'],$_POST['cartype'],$_POST['regno'],$_POST['fare'],$_POST['nfare']);
if($r==1)
return 1;
else
return 0;
helper.php script:
class Admin extends connection
{
var $update;
var $insert;
var $delete;
function update($id,$name,$typ,$rno,$fare,$ncharge)
{
$this->update=mysqli_query($this->con,"UPDATE cars SET cname='$name',ctype='$typ',regno='$rno',fare='$fare',nightcharge='$ncharge' WHERE cid='$id'") or die(mysqli_error($this->con));
if($this->update)
{
return 1;
}
}
}
the path and connection class and all the names and id included within the AJAX query, PHP script and database table has been cross-checked twice.
I've got a form that submits using ajax, however for some reason if you change the values and submit it again it then it inserts 2 duplicate rows, submit again and it inserts 3 duplicate rows the next time, and so on... Somewhere it's storing the number but being new to ajax i'm not 100% sure what I should or shouldn't be adding. Here's the code:
$(document).on('submit', '#add-badge-form', function(e)
{
e.preventDefault();
var data = $(this).serialize();
$.ajax({
type : 'POST',
url : 'submit.php',
data : data,
success : function(data)
{
$(".add-badge-result").fadeIn(500).show(function()
{
$(".add-badge-result").html(data);
});
$('#add-badge-form').trigger("reset");
}
});
return false;
});
EDIT: The PHP that handles the form:
if($_POST['form-name'] == 'add-badge') {
$name = $_POST['name'];
$description = $_POST['description'];
$level = $_POST['level'];
$result = mysqli_query($con,"INSERT INTO Badges (Name,Description,Level) VALUES ('$name','$description','$level')") or die(mysql_error());
mysqli_close($con);
if($result)
{
echo $name." added successfully.";
}
else
{
echo "Error adding ".$name.".";
}
UPDATE:
I've now found that this only occurs after you change pages (also using ajax). So if you insert a form, then insert again without leaving the page, it works fine. If you insert, then change the page, then go back and insert again, it inserts a duplicate. Here's how I change the page:
$(".button").click(function(){
$(".nav li ul a").removeClass("current");
if (this.id == "add-badge") {
$("#rightContainer").load("add-badge.php");
$(this).addClass("current");
} else if (this.id == "edit-badge") {
$("#rightContainer").load("edit-badges.php");
$(this).addClass("current");
} else if (this.id == "award-badge") {
$("#rightContainer").load("award-badge.php");
$(this).addClass("current");
}
});
I have fixed this now. Because of $(document).on('submit', '#add-badge-form', function(e) when loading the page, it was loading the instance of the form each time and thus if I loaded the page 5 times, it was having 5 instances of the form. I changed that to $( "#add-badge-form" ).submit(function(e) and it now only submits one version no matter how many times the page has been loaded. Simple solution!
I would recommand you to get only the fields you need from your form:
var data = { field1 : $("#field1").val(), ... };
This will prevent someone manually adding fields to your form.
I think the way you serialize data is the problem so it should fix it.
I'm trying to make a simple registration page that makes the user enter the username and email.
The problem is how can I check if the input values(email)already exists in the mysql when I press the submit button without going to the next page instantly ? if the input value doesn't exist in the mysql database I want to display a message like "email not registered". I'm trying to use ajax,jquery, and php but I cant find a decent solution.
//this is the script to check if the emails that the users entered matches.
//I'm trying to post the values to the'checkPage.php' to check if the email exists
//The problem is how can I on move to the next page after the result have been returned?
Sorry for my bad explanation.
<script>
$('input#submitbutton').on('click',function(){
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
});
}else{
}
});enter code here
</script>
//CheckPage.php
//I want the registration page to go to the next page only if the email
//haven't been found in the mysql database.
<?php
if(isset($_POST['mail'])&&isset($_POST['mail2'])){
$mail = $_POST['mail'];
$mail2 = $_POST['mail2'];
try{
$con = mysql_connect("localhost","root",""); echo "connected";
$db = mysql_select_db("db",$con);
$query = mysql_query("select email,id from user where email ='". mysql_real_escape_string(trim($mail))."'",$con);
echo (mysql_num_rows($query)!==0) ? mysql_result($query,0,'email'):'none';
} catch (Exception $ex) {
}
}
?>
*/
you can use this;
$('input#submitbutton').on('click',function(event){
event.preventDefault();
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
if (data !== "none") {
$('#your_form_id').submit();
}
});
}else{
}
});
First of all on submit button make an ajax call as follows
<script>
function myformsubmit()
{
var mail=$('#mail').val();
$.post( "check.php", { mail: mail})
.done(function( data ) {
if(msg == 'error')
{
$('#diverr').html('Match found');
return false;
}
else
{
//redirect to a valid page
}
});
}
In the check.php you will have to get your data by post make an sql select query loop thru the mail ids returned by the query and if there is a match found return 'error' or return blank
try to put your javascript code in $(document).ready();
$(document).ready(function() {
//your code here
$('input#submitbutton').on('click',function(){
var mail=$('input#mail').val();
var mail2=$('input#mail2').val();
if($.trim(mail)===$.trim(mail2)){
$.post('checkPage.php',{mail:mail},function(data){
$('#name-data').text(data); // displays the result if the email exists or not
});
}else{
}
});e
});
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.
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)?