Why isnt this ajax live search working - php

**Updated code as of 3-28
Index.php
<!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>
<title>jQuery Search Demonstration</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".keywords").keyup(function(){
$('#key').html($(this).val());
$('#table').html($('.table:checked').val());
// Do the ajax call
// Get the textbox value: $(this).val()
// Get the radio value: $('.table:checked').val()
/*$.post("search.php", { keywords: $(".keywords").val() }, function(data){
$("div#content").empty()
$.each(data, function(){
$("div#content").append("- <a href='prof.php?pID=" + this.pID + "'>" + this.last + "</a>");
});
}, "json");*/
});
});
</script>
</head>
<body>
Search by:
<input type="radio" name="table" class="table" value="Table 1" />Professor<br />
<input type="radio" name="table" class="table" value="Table 2" />Department<br />
<input type="radio" name="table" class="table" value="Table 3" />Course<br />
<input type="text" name="search" class="keywords">
<input type="submit" name="submit" class="search">
<div id="content">
</div>
</body>
</html>
Search.php
<?php
$link = mysql_connect('##',"##","##");
mysql_select_db("###", $link);
$keywords = mysql_real_escape_string( $_POST["keywords"] );
$query = mysql_query("SELECT pID, lname, fname
FROM Professor
WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'");
$arr = array();
while( $row = mysql_fetch_array ( $query ) )
{
$arr[] = array( "pID" => $row["pID"], "last" => $row["lname"], "first" => $row["fname "] );
}
echo json_encode( $arr );
?>
Sql for each selection:
[Professor]
SELECT pID, lname, fname
FROM Professor
WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'";
[Department]
SELECT prefix, code
FROM Department
WHERE name LIKE '%". $keywords . "%'";
[course]
SELECT prefix, code
FROM Course
WHERE CONCAT(prefix,course) LIKE '%". $keywords . "%'";

You should change your query like this:
$query = mysql_query("SELECT pID, lname, fname
FROM Professor
WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'");
Please make sure this these fields in json and javascript are the same:
$arr[] = array( "id" => $row["pID"], "last" => $row["lname"], "first" => $row["fname"] );
$("div#content").append("- <a href='post.php?id=" + this.id + "'>" + this.first + " " + this.last + "</a>");
HTML code:
<!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>
<title>jQuery Search Demonstration</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".keywords").keyup(function(){
getData();
});
$(".table").click(function(){
getData();
});
});
function getData(){
$.post("search.php",
{
keywords: $(".keywords").val(),
table: $('.table:checked').val()
},
function(data){
$("div#content").empty();
var phppage;
switch($('.table:checked').val())
{
case 'professor':
phppage = 'prof';
break;
case 'department':
phppage = 'department';
break;
case 'course':
phppage = 'course';
break;
}
$.each(data, function(){
$("div#content").append("- <a href='" + phppage + ".php?pID=" + this.id + "'>" + this.name + "</a>");
});
},
"json");
}
</script>
</head>
<body>
Search by:
<input type="text" name="search" class="keywords" /><br />
<input type="radio" name="table" class="table" value="professor" checked="checked" /> Professor<br />
<input type="radio" name="table" class="table" value="department" /> Department<br />
<input type="radio" name="table" class="table" value="course" /> Course<br />
<div id="content"></div>
</body>
</html>
PHP code:
<?php
$link = mysql_connect('localhost',"#","#");
mysql_select_db("#", $link);
$arr = array();
$keywords = mysql_real_escape_string( $_POST["keywords"] );
switch ($_POST["table"])
{
case 'professor';
$arr = getProfessor($keywords);
break;
case 'department';
$arr = getDepartment($keywords);
break;
case 'course';
$arr = getCourse($keywords);
break;
}
echo json_encode( $arr );
function getProfessor($keywords){
$arr = array();
$query = mysql_query("SELECT pID, lname, fname
FROM Professor
WHERE CONCAT(lname,fname) LIKE '%". $keywords . "%'");
while( $row = mysql_fetch_array ( $query ) )
{
$arr[] = array( "id" => $row["pID"], "name" => $row["fname"] . ' ' . $row["lname"]);
}
return $arr;
}
function getDepartment($keywords){
$arr = array();
$query = mysql_query("SELECT prefix, code
FROM Department
WHERE name LIKE '%". $keywords . "%'");
while( $row = mysql_fetch_array ( $query ) )
{
$arr[] = array( "id" => $row["code"], "name" => $row["code"]);
}
return $arr;
}
function getCourse($keywords){
$arr = array();
$query = mysql_query("SELECT prefix, code
FROM Course
WHERE CONCAT(prefix,course) LIKE '%". $keywords . "%'");
while( $row = mysql_fetch_array ( $query ) )
{
$arr[] = array( "id" => $row["code"], "name" => $row["code"]);
}
return $arr;
}
?>

It looks like in your javascript you're trying to access the .id and .title properties of the returned json objects but they don't exist from the array you create in php
$arr[] = array( "id" => $row["pID"], "title" => $row["lname"]." ".$row["fname"] );
Does that work?
EDIT:
$(document).ready(function(){
$(".search").click(function(){
$.post("search.php", { keywords: $(".keywords").val() }, function(data){
$("div#content").empty()
$.each(data, function(){
$("div#content").append("- <a href='post.php?id=" + this.id + "'>" + this.first + " " + this.last + "</a>");
});
});
Looks like that is missing a closing brace to enclose the $.each
$(document).ready(function(){
$(".search").click(function(){
$.post("search.php", { keywords: $(".keywords").val() }, function(data){
$("div#content").empty()
$.each(data, function(){
$("div#content").append("- <a href='post.php?id=" + this.id + "'>" + this.first + " " + this.last + "</a>");
});
});
});

Related

Dynamically Add Input Fields And Submit To Database With jQuery and PHP

I want to Post Multiple values for foreach function because i have multiple dynamic textbox then how should i send the values in database??
how to write foreach function for dat..
the code below display 2 multiple textbox but didnt submit values in databse
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
var counter = 0;
$(function(){
$('p#add_field').click(function(){
counter += 1;
$('#container').append(
'<strong>Hobby No. ' + counter + '</strong><br />'
+ '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />'
+'<strong>HolidayReason ' + counter + '</strong> '
+ '<input id="holidayreason_' + counter + '" name="holireason[]' + '" type="text" />'
);
});
});
</script>
<body>
<?php
if (isset($_POST['submit_val'])) {
if (($_POST['dynfields'])&& ($_POST['holireason'])) {
//$aaa=array($_POST['dynfields']);
foreach ($_POST['dynfields'] as $key=>$value)
{
$values = mysql_real_escape_string($value);
//$holireasons = mysql_real_escape_string($holireason);
$query = mysql_query("INSERT INTO my_hobbies (hobbies) VALUES ('$values')" );
}
}
echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>";
mysql_close();
}
?>
<?php if (!isset($_POST['submit_val'])) { ?>
<h1>Add your Hobbies</h1>
<form method="post" action="">
<div id="container">
<p id="add_field"><span>Click To Add Hobbies</span></p>
</div>
<input type="submit" name="submit_val" value="Submit" />
</form>
<?php } ?>
</body>
</html>
try this
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
var counter = 0;
$(function(){
$('p#add_field').click(function(){
counter += 1;
$('#container').append(
'<strong>Hobby No. ' + counter + '</strong><br />'
+ '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />'
+'<strong>HolidayReason ' + counter + '</strong> '
+ '<input id="holidayreason_' + counter + '" name="holireason[]' + '" type="text" />'
);
});
});
</script>
<body>
<?php
if (isset($_POST['submit_val'])) {
if (($_POST['dynfields'])&& ($_POST['holireason'])) {
$no = count($_POST['dynfields']);
for ($i=0; $i <$no ; $i++) {
echo $_POST['dynfields'][$i]."<br>";
echo $_POST['holireason'][$i]."<br>";
$abc = mysql_real_escape_string($_POST['dynfields'][$i]);
$xyz = mysql_real_escape_string($_POST['holireason'][$i]);
$sql = "INSERT INTO my_hobbies (hobbies,Holidayreason) VALUES ('$abc','$xyz')";
mysql_query($sql);
}
}
echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>";
mysql_close();
}
?>
<?php if (!isset($_POST['submit_val'])) { ?>
<h1>Add your Hobbies</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div id="container">
<p id="add_field"><span>Click To Add Hobbies</span></p>
</div>
<input type="submit" name="submit_val" value="Submit" />
</form>
<?php } ?>
</body>
</html>
Use array_combine as follows.
foreach(array_combine($_POST['dynfields'] , $_POST['holireason']) as $dyn => $holi) {
$abc = mysql_real_escape_string($dyn);
$xyz = mysql_real_escape_string($holi);
$sql = mysql_query ("INSERT INTO my_hobbies (hobbies,Holidayreason) VALUES ('".$abc."','".$xyz."')");
}
100% works fine.

Reset second jquery drop down select box when first is changed

I am using the following script to fetch records from my database and put them into select boxes using jquery, ajax and php. The select boxes are also styled and added features using Select 2
http://ivaynberg.github.io/select2/select-2.1.html#basics
If I select a customer from the first select box and then select a vehicle from the second box this works fine........if I then change my mind and select a different company, the vehicle box stays on the last reg and doesn't revert back to :
<option>Select A Customers Vehicle</option>
If I then click on the vehicle select box I can select the vehicles from the company and the 'ghost vehicle' from the last query vanishes, so it does work, its just when I change the company again I would like it just to reset the vehicle box back to its default again until I select a vehicle.
This is the Main Page :
<script src="js/jquery/jquery.js"></script>
<script src="js/jqueryui/js/jquery-ui.js"></script>
<link href="js/select2/select2.css" rel="stylesheet"/>
<script src="js/select2/select2.js"></script>
<script>
$(document).ready(function() { $("select").select2(); });
</script>
<?php
if (session_status() !== PHP_SESSION_ACTIVE) {session_start();}
if (isset($_SESSION['key'])) {$sessionkey = $_SESSION['key'];}else {$sessionkey = '';}
if ($sessionkey == 'sbhjbKA2bsbhjbKA209bhjbKA2bsbhjbKA209KaXff19u0bsbhjbKA209KaXff19u9Ka'){
include 'connectmysqli.php';
echo '<link rel="stylesheet" href="css/template/template.css" />';
echo '<strong class="pagetitle">Add New Sale</strong>
';
$saleID = rand().rand();
$today = date("Y-m-d");
echo '<form method="post" action="addsalesubmit.php">';
echo '<input type="hidden" value="'.$saleID.'" name="saleID" id="saleID">';
echo '<input type="hidden" value="'.$today.'" name="date" id="date">';
?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Select test</title>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#customer').on('change', function (){
$.getJSON('select.php', {customerId: $(this).val()}, function(data){
var options = '';
for (var x = 0; x < data.length; x++) {
options += '<option value="' + data[x]['id'] + '">' + data[x]['reg'] + ' - ' + data[x]['make'] + ' - ' + data[x]['model'] + '</option>';
}
$('#vehicle').html(options);
});
});
});
</script>
</head>
<body>
<br>
<select id="customer">
<option>Please Select / Search For A Customer</option>
<?php
$sql = <<<SQL
SELECT *
FROM `customers`
SQL;
if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']');}
while($row = $result->fetch_assoc()){
if ($row['bussinessname'] == ''){$name = $row['title'].' '.$name = $row['firstname'].' '.$name = $row['surname'];}else
{$name = $row['bussinessname'];}
echo '<option value="'.$row['customerID'].'">'.$name.'</option>';
}
echo '</select></p>';
?>
</select>
<br>
<br>
<select id="vehicle">
<option>Select A Customers Vehicle</option>
</select>
</body>
</html>
<?php
}
else
{echo '<h1 style="font-family:Verdana, Geneva, sans-serif; color:red;">Access Denied !</h1>';}
?>
This is the php script that does all the fetching :
<?php include 'connectmysqli.php'; ?>
<?php
$id = $_GET['customerId'];
$sql = 'SELECT * FROM vehicles WHERE customerID = ' . (int)$id;
$result = $db->query($sql);
$json = array();
while ($row = $result->fetch_assoc()) {
$json[] = array(
'id' => $row['vehicleID'],
'reg' => $row['reg'],
'make' => $row['make'],
'model' => $row['model']
);
}
echo json_encode($json);
?>
On every call of the onchange first empty the second dropdown
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#customer').on('change', function (){
$('#vehicle').html("<option value=''>Select</option>");// add this on each call then add the options when data receives from the request
$.getJSON('select.php', {customerId: $(this).val()}, function(data){
var options = '';
for (var x = 0; x < data.length; x++) {
options += '<option value="' + data[x]['id'] + '">' + data[x]['reg'] + ' - ' + data[x]['make'] + ' - ' + data[x]['model'] + '</option>';
}
$('#vehicle').html(options);
$("select").select2();
});
});
});
</script>
the following is not asked but i have to advice you that there are some additional errors in your code:
echo '</select></p>';
?>
</select>
there are two </select> and one </p> without a starting <p> at the end of your customer select box

Five Buttons One Dropdown List

I'm trying to populate a dropdown listbox with a set of five buttons. The first one works, however the other four do not, as of yet. If been looking around but due to inexperience I can't seem to put it together. Any help is appreciated. Thank you. Here is the code I have so far...incomplete.
mysql_select_db('Mydb');
$place = mysql_query("select * from tblRestaurants order by RestName ASC");
$cuisine = mysql_query("select * from tblCuisine order by CuisineName ASC");
$city = mysql_query("select * from tblCities order by CityName ASC");
$state = mysql_query("select * from tblStates order by StateName ASC");
$zipcode = mysql_query("select * from tblLocations order by ZipCode ASC");
while ($nt= mysql_fetch_assoc($place))
$arrData[] = $nt;
if(isset($_GET["ajax"]))
{
echo json_encode($arrData);
die();
}
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function displayPlace()
{
$.getJSON("Four.php?ajax=true", function(data) {
$.each(data, function(index, objRecord) {
var option=document.createElement("option");
option.value=objRecord.RestID;
option.text=objRecord.RestName;
$("#Doggie").append('<option value="' + objRecord.RestID + '">' + objRecord.RestName + '</option>');
});
});
}
function displayCuisine()
{
$.getJSON("Four.php?ajax=true", function(data) {
$.each(data, function(index, objRecord) {
var option=document.createElement("option");
option.value=objRecord.CuisineID;
option.text=objRecord.CuisineName;
$("#Doggie").append('<option value="' + objRecord.CuisineID + '">' + objRecord.CuisineName + '</option>');
});
});
}
</script>
<title>SEARCH</title>
</head>
<body>
<form>
<button type="button" onclick="javascript:displayPlace();">Place</button>
<button type="button" onclick="javascript:displayCuisine();">Cuisine</button>
<button type="button" onclick="javascript:displayCity();" >City</button>
<button type="button" onclick="javascript:displayState();">State</button>
<button type="button" onclick="javascript:displayZipCode();">Area</button>
<br />
<select name="Doggie" id="Doggie"></select>
<br />
</form>
</body>
</html>
Please modify your php code i have tried to explain this using some sample code
and pass one additional parameter of case in the ajax request and then it will work for you
$list['place'] = mysql_query("select * from tblRestaurants order by RestName ASC");
$list['cuisine'] = mysql_query("select * from tblCuisine order by CuisineName ASC");
foreach($list as $key=>$value){
while ($nt = mysql_fetch_assoc($list[$key]))
$list_array[$key] = $nt;
}
if(isset($_GET["ajax"]))
{
switch($_GET['case']){
case 'place':
echo json_encode($list_array['place']);
break;
case 'cuisine':
echo json_encode($list_array['cuisine']);
break;
}
die();
}

Loading JSON data with jQuery, PHP and MySQL for radio buttons

I'm trying to populate a third set of radiobuttons as an addition to the following script: http://www.electrictoolbox.com/json-data-jquery-php-radio-buttons/
For some reason I cannot seem to fill the third set with the corresponding data. It just stays blank :(
Calling the populateFruittype() function only gives back [ ], while populateFruitVariety() returns the json data correctly.
getdata.php (DB connection / fetching data)
<?php
$dsn = "mysql:host=localhost;dbname=mydb";
$username = "username";
$password = "password";
$pdo = new PDO($dsn, $username, $password);
$rows = array();
if(isset($_GET['fruitName'])) {
$stmt = $pdo->prepare("SELECT DISTINCT variety FROM fruit WHERE name = ? ORDER BY variety");
$stmt->execute(array($_GET['fruitName']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
if(isset($_GET['fruitVariety'] )) {
$stmt = $pdo->prepare("SELECT DISTINCT fruittype FROM fruit WHERE variety = ? ORDER BY fruittype");
$stmt->execute(array($_GET['fruitVariety']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($rows);
?>
HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Toevoegen</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
function populateFruitVariety() {
var fruitName = $('input[name=fruitName]:checked').val();
$.getJSON('getdata.php', {fruitName: fruitName}, function(fruit) {
var html = '';
$.each(fruit, function(index, array) {
html = html + '<label><input type="radio" name="fruitVariety" value="' + array['variety'] + '" />' + array['variety'] + '</label> ';
});
$('#varieties').html(html);
});
}
function populateFruittype() {
var fruitVariety = $('input[name=fruitVariety]:checked').val();
$.getJSON('getdata.php', {fruitVariety: fruitVariety}, function(fruit) {
var html = '';
$.each(fruit, function(index, array) {
html = html + '<label><input type="radio" name="fruitType" value="' + array['fruittype'] + '" />' + array['fruittype'] + '</label> ';
});
$('#fruittype').html(html);
});
}
$(function() {
$('input[name=fruitName]').change(function() {
populateFruitVariety();
});
});
$(function() {
$('input[name=fruitVariety]').change(function() {
populateFruittype();
});
});
</script>
</head>
<body>
<form>
<div>
<strong>Fruit:</strong>
<label><input type="radio" name="fruitName" value="Apple"/>Apple</label>
<label><input type="radio" name="fruitName" value="Banana"/>Banana</label>
<label><input type="radio" name="fruitName" value="Orange"/>Orange</label>
<label><input type="radio" name="fruitName" value="Pear"/>Pear</label>
</div>
<div>
<strong>Variety:</strong>
<span id="varieties"></span>
</div>
<div>
<strong>Type:</strong>
<span id="fruittype"></span>
</div>
</form>
</body>
</html>
The DB query and content can be found here: http://www.electrictoolbox.com/mysql-example-table/
Just add:
`fruittype` varchar(50) NOT NULL
and fill it with some custom values.
1) DB uddate
ALTER TABLE fruit ADD COLUMN `fruittype` varchar(50) NOT NULL;
UPDATE fruit SET fruittype = 'description' /* WHERE name = 'Apple'*/;
2) code update
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Toevoegen</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('input[name=fruitName]').change(function() {
var fruitName = $('input[name=fruitName]:checked').val();
$.getJSON('test.php', {fruitName: fruitName}, function(fruit) {
var html = '';
$.each(fruit, function(index, array) {
html = html + '<input type="radio" name="fruitVariety" value="' + array['variety'] + '" /><label>' + array['variety'] + '</label> ';
});
$('#varieties').html(html);
});
});
$('input[name=fruitVariety]').live('click', function() {
var fruitVariety = $('input[name=fruitVariety]:checked').val();
$.getJSON('test.php', {fruitVariety: fruitVariety}, function(fruit) {
var html = '';
$.each(fruit, function(index, array) {
html = html + '<input type="radio" name="fruitType" value="' + array['fruittype'] + '" /><label>' + array['fruittype'] + '</label> ';
});
$('#fruittype').html(html);
});
});
});
</script>
</head>
<body>
<form>
<div>
<strong>Fruit:</strong>
<label><input type="radio" name="fruitName" value="Apple"/>Apple</label>
<label><input type="radio" name="fruitName" value="Banana"/>Banana</label>
<label><input type="radio" name="fruitName" value="Orange"/>Orange</label>
<label><input type="radio" name="fruitName" value="Pear"/>Pear</label>
</div>
<div>
<strong>Variety:</strong>
<span id="varieties"></span>
</div>
<div>
<strong>Type:</strong>
<span id="fruittype"></span>
</div>
</form>
</body>
</html>

Updating MySql using AJAX in jQuery Dialog Box

I have the following code and the SELECT is working correctly to pull in the form data, however, the UPDATE statement is not working. Any suggestions on what could be corrected to make it work would be greatly appreciated.
<?php
include 'includes/config.php';
require_once('includes/auth.php');
if ( isset( $_GET['aj'] ) && $_GET['aj'] == '1' ) {
if ( isset( $_GET['ax']) && $_GET['ax'] == 'education_details' ) {
$sql =
<<<SQL
SELECT *
FROM education
WHERE id = '{$_GET['rid']}'
ORDER BY date_completed DESC
SQL;
$sql_result = mysql_query($sql) or die("Get education detail error: " . mysql_error());
while ( $row = mysql_fetch_assoc($sql_result) ) {
$education_info = array(
'member_id' => $row['member_id'],
'name' => $row['name'],
'degree' => $row['degree'],
);
}
echo json_encode($education_info);
}
exit;
}
if ( isset($_POST['aj']) && $_POST['aj'] == '1' ) {
if (isset( $_POST['ax']) && $_POST['ax'] == 'save' ) {
$sql=
<<<SQL
UPDATE education
SET name = '{$education_info['name']}',
degree = '{$education_info['degree']}'
WHERE id = '{$education_info['rid']}'
SQL;
}
// echo json_encode($education_info);
exit;
}
?>
<html>
<head>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#accordion').accordion({
header: "h3",
collapsible: true,
active: false
});
$('#dialog').dialog({
autoOpen: false,
open: true,
modal: true,
width: 525,
buttons: {
"Cancel": function() {
$(this).dialog("close");
}
}
});
$('#dialog_link').click(function() {
$('#dialog').dialog('open');
});
education_details.initialize();
});
var education_details = {
url : '<?= $_SERVER['PHP_SELF']; ?>',
initialize : function() {
$('#dialog_edit').dialog({
autoOpen: false,
open: true,
modal: true,
width: 525,
buttons: {
"Save" : function() {
$.post("educationie.php", {"aj":"1", "ax":"save" , "rid":$(this).attr("id")}, function(data) {
alert(data);
}, "json");
// $(this).dialog('close');
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
$('.edit_open').click(education_details.open);
},
open : function() {
$('#dialog_edit').dialog('open');
$.get(education_details.url, {"aj":"1","ax":"education_details","rid":$(this).attr("id")}, education_details.education_details_success, "json");
},
education_details_success : function(data) {
$("#dialog_edit input[name=name]").val(data.name);
$("#dialog_edit input[name=degree]").val(data.degree);
}
};
</script>
</head>
<body>
<?php
if ( $_POST['ax'] == "new" ) {
// echo "DID IT GO HERE"; exit;
$sql=<<<SQL
INSERT INTO education (member_id, name, degree)
VALUES (
'{$_SESSION['SESS_MEMBER_ID']}',
'{$_POST['name']}',
'{$_POST['degree']}'
)
SQL;
if(mysql_query( $sql ) or die ( "Insert failed." . mysql_error()) );
// echo $sql; exit;
// header( "Location: education.php");
}
?>
<button id="dialog_link" class="ui-button ui-state-default ui-corner-all">Add New</button>
<br>
<div id="dialog" title="Add New" style="display:none;">
<form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
<input type="hidden" name="ax" value="new">
Institution<br><input type="text" name="name" /><br>
Degree(s) Earned<br><input type="text" name="degree" /><br>
<input type="submit" name="SUBMIT" value="submit" class="ui-button ui-state-default ui-corner-all"/>
</div>
<div id='dialog_edit' title='Edit' style="display:none;">
<form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
Institution<br><input type="text" name="name" /><br>
Degree(s) Earned<br><input type="text" name="degree" /><br>
</form>
</div>
<div id="accordion">
<?
$sql = mysql_query(" SELECT *
FROM education
WHERE member_id = '" . $_SESSION['SESS_MEMBER_ID'] . "'
ORDER BY date_completed DESC");
while($row = mysql_fetch_array($sql)){
echo "<h3><a href=#>" . $row['name'] ."</a></h3>";
echo "<div>
<table>
<tbody>
<tr>
<td valign='top'>
<a id=\"" . $row['id'] . "\" class=\"edit_open\" href=\"javascript:void(0)\">Edit</a>
<br> Degree(s): ". $row['degree'] .
"</td>
<td style='padding-left:85px;'>
</td>
</tr>
</tbody>
</table>
</div>";
}
mysql_close($con);
?>
</div>
</body>
</html>
try setting error_reporting(E_ALL) and checking the errors in firebug...
the update references an undefined variable:
$education_info['name']}'
it wasn't set at that point, you probably want to use $_POST['name'] or something instead
(you might want to bind parameters in queries as it's easier as well)
You forgot to post the RID = where id
<?php
include 'includes/config.php';
require_once('includes/auth.php');
if ( isset( $_GET['aj'] ) && $_GET['aj'] == '1' ) {
if ( isset( $_GET['ax']) && $_GET['ax'] == 'education_details' ) {
$sql =
<<<SQL
SELECT *
FROM education
WHERE id = '{$_GET['rid']}'
ORDER BY date_completed DESC
SQL;
$sql_result = mysql_query($sql) or die("Get education detail error: " . mysql_error());
while ( $row = mysql_fetch_assoc($sql_result) ) {
$education_info = array(
'id' => $row['id'],
'member_id' => $row['member_id'],
'name' => $row['name'],
'degree' => $row['degree'],
);
}
echo json_encode($education_info);
}
exit;
}
if ( isset($_POST['aj']) && $_POST['aj'] == '1' ) {
if (isset( $_POST['ax']) && $_POST['ax'] == 'save' ) {
$sql=
<<<SQL
UPDATE education
SET name = '{$_POST['name']}',
degree = '{$_POST['degree']}'
WHERE id = '{$_POST['rid']}'
SQL;
}
$sql_result = mysql_query($sql) or die("Get education update error: " . mysql_error());
// echo json_encode($education_info);
exit;
}
?>
<!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" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
$(document).ready(function(){
$('#accordion').accordion({
header: "h3",
collapsible: true,
active: false
});
$('#dialog').dialog({
autoOpen: false,
open: true,
modal: true,
width: 525,
buttons: {
"Cancel": function() {
$(this).dialog("close");
}
}
});
$('#dialog_link').click(function() {
$('#dialog').dialog('open');
});
education_details.initialize();
});
var education_details = {
url : '<?= $_SERVER['PHP_SELF']; ?>',
initialize : function() {
$('#dialog_edit').dialog({
autoOpen: false,
open: true,
modal: true,
width: 525,
buttons: {
"Save" : function() {
$.post("<?= $_SERVER['PHP_SELF']; ?>", {"aj":"1", "ax":"save" , "rid":$("#dialog_edit input[name=rid]").val(), "name":$("#dialog_edit input[name=name]").val(), "degree": $("#dialog_edit input[name=degree]").val()}, function(data) {
alert(data);
}, "json");
$(this).dialog('close');
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
$('.edit_open').click(education_details.open);
},
open : function() {
$('#dialog_edit').dialog('open');
$.get(education_details.url, {"aj":"1","ax":"education_details","rid":$(this).attr("id")}, education_details.education_details_success, "json");
},
education_details_success : function(data) {
$("#dialog_edit input[name=name]").val(data.name);
$("#dialog_edit input[name=degree]").val(data.degree);
$("#dialog_edit input[name=rid]").val(data.id);
}
};
</script>
</head>
<body>
<?php
if (!empty($_POST['ax']) && $_POST['ax'] == "new" ) {
// echo "DID IT GO HERE"; exit;
$sql=<<<SQL
INSERT INTO education (member_id, name, degree)
VALUES (
'{$_SESSION['SESS_MEMBER_ID']}',
'{$_POST['name']}',
'{$_POST['degree']}'
)
SQL;
if(mysql_query( $sql ) or die ( "Insert failed." . mysql_error()) );
//echo $sql; exit;
// header( "Location: education.php");
}
?>
<button id="dialog_link" class="ui-button ui-state-default ui-corner-all">Add New</button>
<br>
<div id="dialog" title="Add New" style="display:none;">
<form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
<input type="hidden" name="ax" value="new">
Institution<br><input type="text" name="name" /><br>
Degree(s) Earned<br><input type="text" name="degree" /><br>
<input type="submit" name="SUBMIT" value="submit" class="ui-button ui-state-default ui-corner-all"/>
</form>
</div>
<div id='dialog_edit' title='Edit' style="display:none;">
<form action="<?= $_SERVER["PHP_SELF"];?>" method="post">
<input type="hidden" name="rid">
Institution<br><input type="text" name="name" /><br>
Degree(s) Earned<br><input type="text" name="degree" /><br>
</form>
</div>
<div id="accordion">
<?
$sql = mysql_query(" SELECT *
FROM education
WHERE member_id = '{$_SESSION['SESS_MEMBER_ID']}'
ORDER BY date_completed DESC");
while($row = mysql_fetch_array($sql)){
echo "<h3><a href=#>" . $row['name'] ."</a></h3>";
echo "<div>
<table>
<tbody>
<tr>
<td valign='top'>
<a id=\"" . $row['id'] . "\" class=\"edit_open\" href=\"javascript:void(0)\">Edit</a>
<br> Degree(s): ". $row['degree'] .
"</td>
<td style='padding-left:85px;'>
</td>
</tr>
</tbody>
</table>
</div>";
}
mysql_close($con);
?>
</div>
</body>
</html>

Categories