Last active
August 29, 2015 14:10
-
-
Save dennisdegryse/0553c2063ae14a927699 to your computer and use it in GitHub Desktop.
AJAX Problem (Sushma Reddy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Establishing Connection with Server.. | |
$connection = mysqli_connect("localhost", "root", "") | |
or die("Could not connect to database at 'localhost'"); | |
// Selecting Database | |
mysql_select_db($connection, "ajax") | |
or die("Could not open database 'ajax'"); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('ajaxsubmit.php'); | |
/*if(isset($_POST['nameid'])) | |
{ | |
$nameid=$_POST['nameid']; | |
$sel=mysql_query("select * from ajax_form where name='".$nameid."'"); | |
$res=mysql_fetch_array($sel); | |
$num=mysql_num_rows($sel); | |
if($num==0) | |
{ | |
$data[]='Name Doesnot Exist'; | |
} | |
else | |
{ | |
$data=$res; | |
} | |
print json_encode($data); | |
}*/ | |
if(isset($_GET['aid'])) | |
{ | |
$aid=$_POST['aid']; | |
$update=mysql_query("update ajax_form set name='".$_POST['name']."',age='".$_POST['age']."',salary='".$_POST['salary']."',email='".$_POST['email']."' where id='".$_GET['aid']."'"); | |
} | |
if(isset($_GET['id'])) | |
{ | |
$aid=$_POST['id']; | |
$delete=mysql_query("delete from ajax_form where id='".$_GET['id']."'"); | |
} | |
//editing: | |
/*if(isset($_GET['id'])) | |
{ | |
$sel_qry=mysql_query("select * from ajax_form where id='".$_GET['id']."'"); | |
$res_qry=mysql_fetch_array($sel_qry); | |
} | |
<input type="hidden" name="hideid" value="<?php if(isset($_GET['id'])) echo $_GET['id']; ?>" /> | |
*/ | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//session_start(); | |
include('ajaxsubmit.php'); | |
function printDataRow($row) { | |
?> | |
<tr> | |
<td><?php echo $i; ?></td> | |
<td><?php echo $res['name']; ?></td> | |
<td><?php echo $res['age']; ?></td> | |
<td><?php echo $res['salary']; ?></td> | |
<td><?php echo $res['email']; ?></td> | |
<td><a href="<?php echo $res['id'];?>" class="edit">EDIT</a> / <a href="?id=<?php echo $res['id']; ?>">DELETE</a></td> | |
</tr> | |
<?php | |
} | |
function printMessageRow($message) { | |
?> | |
<tr> | |
<td colspan="6"><?php echo $message ?></td> | |
</tr> | |
<?php | |
} | |
if(isset($_POST['name'])) | |
{ | |
$res=mysql_query("INSERT INTO ajax_form SET name='".$_POST['name']."',age='".$_POST['age']."',salary='".$_POST['salary']."',email='".$_POST['email']."'"); | |
if($res) | |
{ | |
echo"Values Inserted"; | |
} | |
} | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Untitled Document</title> | |
<script src="jquery-1.7.min.js"></script> | |
<script src="script.js"></script> | |
<script> | |
$(document).ready(function(){ | |
$('.edit').click(function(e) | |
{ | |
e.preventDefault(); | |
var aid = $('#hideid').val(); | |
$.ajax({ | |
//url:"edit.php", | |
type:"post", | |
data: {id:aid}, | |
dataType:'json', | |
success: function(response){ | |
$("#name").val(response[1]); | |
$("#age").val(response[2]); | |
$("#salary").val(response[3]); | |
$("#email").val(response[4]); | |
}, | |
error:function(){ | |
alert("failure"); | |
} | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" > | |
<input type="hidden" name="hideid" id="hideid" value="<?php if(isset($_GET['id'])) echo $_GET['id']; ?>" /> | |
Name: <input type="text" name="name" id="name" value="" /><br /><br /> | |
Age: <input type="text" name="age" id="age" value="" /><br /><br /> | |
Salary: <input type="text" name="salary" id="salary" value=""/><br /><br /> | |
Email: <input type="email" name="email" id="email" value="" /><br /><br /> | |
<input type="button" value="send" id="send" /><br /><br /> | |
<!--<input type="text" name="nameid" id="nameid" value="" /> | |
<input type="button" value="EDIT" id="edit" name="edit" /> | |
--> | |
</form> | |
<table border="1"> | |
<tr> | |
<th>S.No</th> | |
<th>Name</th> | |
<th>Age</th> | |
<th>Salary</th> | |
<th>Email</th> | |
<th>Action</th> | |
</tr> | |
<?php | |
$res = mysqli_query("select * from ajax_form "); | |
$num = mysqli_num_rows($res); | |
if ($num > 0) { | |
for ($i = 0; $i < $num; $i++) | |
{ | |
$row = mysqli_fetch_array($res); | |
printDataRow($row); | |
} | |
} else { | |
printMessageRow("No data"); | |
} | |
?> | |
<?php } ?> | |
</table> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Untitled Document</title> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- phpMyAdmin SQL Dump | |
-- version 4.2.7.1 | |
-- http://www.phpmyadmin.net | |
-- | |
-- Host: 127.0.0.1 | |
-- Generation Time: Dec 02, 2014 at 11:06 AM | |
-- Server version: 5.5.39 | |
-- PHP Version: 5.4.31 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8 */; | |
-- | |
-- Database: `mydba` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `form_element` | |
-- | |
CREATE TABLE IF NOT EXISTS `form_element` ( | |
`id` int(25) NOT NULL, | |
`name` varchar(255) NOT NULL, | |
`age` varchar(255) NOT NULL, | |
`salary` varchar(255) NOT NULL, | |
`email` varchar(25) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ; | |
-- | |
-- Dumping data for table `form_element` | |
-- | |
INSERT INTO `form_element` (`id`, `name`, `age`, `salary`, `email`) VALUES | |
(8, '', '', '', ''), | |
(9, '', '', '', ''), | |
(10, 'sushma', '23', '23444', 'asds@gmail.com'); | |
-- | |
-- Indexes for dumped tables | |
-- | |
-- | |
-- Indexes for table `form_element` | |
-- | |
ALTER TABLE `form_element` | |
ADD PRIMARY KEY (`id`); | |
-- | |
-- AUTO_INCREMENT for dumped tables | |
-- | |
-- | |
-- AUTO_INCREMENT for table `form_element` | |
-- | |
ALTER TABLE `form_element` | |
MODIFY `id` int(25) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript Document | |
$(document).ready(function(){ | |
var name = $("#name").val(); | |
/*if(name=='') | |
{ | |
alert('Enter Name'); | |
$('#nameid').focus(); | |
return false; | |
}*/ | |
var age = $("#age").val(); | |
/*if(age=='') | |
{ | |
alert('Enter Age'); | |
$('#ageid').focus(); | |
return false; | |
}*/ | |
var salary = $("#salary").val(); | |
/*if(salary=='') | |
{ | |
alert('Enter Salary'); | |
$('#salaryid').focus(); | |
return false; | |
}*/ | |
var email = $("#email").val(); | |
/*if(email=='') | |
{ | |
alert('Enter Email'); | |
$('#emailid').focus(); | |
return false; | |
}*/ | |
function submitForm(event) | |
// Prevent the form from submitting | |
event.preventDefault(); | |
var name = $("#name").val(); | |
/*if(name=='') | |
{ | |
alert('Enter Name'); | |
$('#nameid').focus(); | |
return false; | |
}*/ | |
var age = $("#age").val(); | |
/*if(age=='') | |
{ | |
alert('Enter Age'); | |
$('#ageid').focus(); | |
return false; | |
}*/ | |
var salary = $("#salary").val(); | |
/*if(salary=='') | |
{ | |
alert('Enter Salary'); | |
$('#salaryid').focus(); | |
return false; | |
}*/ | |
var email = $("#email").val(); | |
/*if(email=='') | |
{ | |
alert('Enter Email'); | |
$('#emailid').focus(); | |
return false; | |
}*/ | |
// Returns successful data submission message when the entered information is stored in database. | |
var dataString = 'name='+ name + '&age='+ age + '&salary='+ salary + '&email='+ email; | |
if(name==''||age==''||salary==''||email=='') { | |
alert("Please Fill All Fields"); | |
} else { | |
// AJAX Code To Submit Form. | |
$.ajax({ | |
type: "POST", | |
url: "ajaxsubmit.php", | |
data: dataString, | |
cache: false, | |
success: function(result) { | |
alert(result); | |
} | |
}); | |
} | |
return false; | |
} | |
$("#send").click(submitForm); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment