Jamen
unread,Apr 24, 2009, 1:36:17 PM4/24/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Webmasters Helping Webmasters, jamen.mc...@vanderbilt.edu
I'm fairly new to trying to use AJAX, so please forgive my ignorance.
I know it is possible to update a form with new content with AJAX, and
I've actually been able to do that - but just on one form element. The
problem I have is trying to update another form element on that same
page.
Here is my js code (update.js):
----------------------------------------------
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var priority = document.getElementById('priority').value;
var id = document.getElementById('id').value;
var express = document.getElementById('express').value;
var queryString = "?priority=" + priority + "&id=" + id + "&express="
+ express;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
-----------------------------------
Here is my ajax-example.php code:
<?php
//Database connection
include ('db.php');
// Set Table Variable
$table = purchase_os;
// Retrieve data from Query String
$listitem=array(1 =>
'priority','express','expressdate','location','bibname','fundnum','lowuse','received','userid','addcomments','notify','name','idnum','depart','telnum','email','mat','access','lic','neg','fileloc','url','col','numtit','recs','funds','author','title','pubplace','publisher','edition','price','quantity','series','standorder','isnnum','source','catnum','itemnum','comments','acornnum','vendor','verifier','verifydate','status','ccemail');
$listvars=array(1 =>
'$priority','$express','$expressdate','$location','$bibname','$fundnum','$lowuse','$received','$userid','$addcomments','$notify','$name','$idnum','$depart','$telnum','$email','$mat','$access','$lic','$neg','$fileloc','$url','$col','$numtit','$recs','$funds','$author','$title','$pubplace','$publisher','$edition','$price','$quantity','$series','$standorder','$isnnum','$source','$catnum','$itemnum','$comments','$acornnum','$vendor','$verifier','$verifydate','$status','$ccemail');
$numitems = count($listitem);
$i = 1;
while ($i <= $numitems) {
$x = $listitem[$i];
if (isset($_GET[$x]) && !is_null($_GET[$x])) {
$listvars[$i] = $_GET[$x];
$z = $listvars[$i];
$listvars[$i] = mysql_real_escape_string($z);
} else {
$listvars[$i] = null;
}
$i++;
}
// Build update query
$query_1 = "UPDATE $table SET ";
$query_3 = "' WHERE id = '$id'";
$k = 1;
while ($k <= $numitems) {
if (isset($listvars[$k]) && !is_null($listvars[$k])) {
$query[$k] = $query_1 . $listitem[$k] . "='" . $listvars[$k] .
$query_3;
}
$k++;
}
// Build results query
$query2 = "SELECT * FROM $table WHERE id = '$id'";
// Execute update query
$m = 1;
while ($m <= $numitems) {
if (isset($query[$m]) && !is_null($query[$m])) {
$qry_result = mysql_query($query[$m]) or die(mysql_error());
}
$m++;
}
$qry_result2 = mysql_query($query2) or die(mysql_error());
//Build Result String
$display_string = "<table>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result2)){
$display_string .= "<tr>";
$display_string .= "<td>This has been updated to<br />$row[priority]</
td>";
$display_string .= "</tr>";
}
$display_string .= "</table>";
echo $display_string;
?>
---------------------
And here is my form (modify-backup.php):
<?php
// Start session to save session data
session_start();
//Header/footer
include('headfoot.php');
//Database connection
include ('db.php');
$id = $_GET["i1"];
$query = "SELECT * FROM purchase_os WHERE id=$id";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
echo "$header";
while ($myrow = mysql_fetch_array($result)) {
echo "<table width='100%' border='1'>";
echo "<tr><td colspan='3' align='center'>Record Number: " . $myrow
['id'] . "</td></tr>";
echo "<tr><td width='33%'> </td><td width='33%'>Current Value</
td><td width='33%'>Update Value With:</td></tr>";
echo "<tr><td> Priority:</td><td>" . $myrow['priority'] . "<br /
><div id='ajaxDiv'></div></td>";
echo "<td><form name='myform'>Priority: <select id='priority'>";
echo "<option> </option><option>Level 1 - Patron Request</option>
<option>Level 1 - Rush</option>
<option>Level 1 - SuperRush (Leisure Reading Collection Only)</option>
<option>Level 1 - Reserve</option>
<option>Level 2 - Reference</option>
<option>Level 2 - Leisure Reading</option>
<option>Level 2 - Other</option>
<option>Level 3</option>";
echo "</select>";
echo "<br />";
echo "<input type='button' value='Update data' onclick='ajaxFunction
();' />";
echo "</td></tr>";
echo "<tr><td>Date Sensitive Order: </td><td>" . $myrow['express'] .
"<br /><div id='ajaxDiv'></div></td>";
echo "<td>Date Sensitive?<br />";
echo "<input type='radio' name='express' id='express' value='yes' /
>Yes";
echo "<input type='radio' name='express' id='express' value='no' /
>No";
echo "<br />";
echo "<input type='hidden' id='id' value='" . $myrow['id'] . "' />";
echo "<input type='button' value='Update data' onclick='ajaxFunction
();' />";
echo "</form>";
echo "</td></tr>";
echo "</table>";
}
echo "$footer";
?>