Alith7 Posted February 23, 2009 Share Posted February 23, 2009 I'm trying to modify an Ajax script that was posted elsewhere on the site and I'm getting no where. I'm trying to update the two text fields "quote_to" and "email_to" when the "customer" drop down list is changed. Any ideas?? I know the script is a bit of a mess. :-\ <?php require_once('Connections/geQuote.php'); ?> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = "quote"; $MM_donotCheckaccess = "false"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && false) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "noaccess.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = "quote.php"; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $editFormAction = "quote.php"; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertQuote")) { $insertSQL = sprintf("INSERT INTO quote (sales_id, cust_id, quote_to, email_to, title, quote, created) VALUES (%s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['sales_id'], "int"), GetSQLValueString($_POST['cust_id'], "int"), GetSQLValueString($_POST['quote_to'], "text"), GetSQLValueString($_POST['email_to'], "text"), GetSQLValueString($_POST['title'], "text"), GetSQLValueString($_POST['quote'], "text"), $_POST['created']); mysql_select_db($database_geQuote, $geQuote); $Result1 = mysql_query($insertSQL, $geQuote) or die(mysql_error()); $insertGoTo = "quote_listsort.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_geQuote, $geQuote); $query_listSales = "SELECT salesrep.sales_id, CONCAT(salesrep.first_name,' ', salesrep.last_name) AS salesRep FROM salesrep ORDER BY salesrep.last_name, salesrep.first_name"; $listSales = mysql_query($query_listSales, $geQuote) or die(mysql_error()); $row_listSales = mysql_fetch_assoc($listSales); $totalRows_listSales = mysql_num_rows($listSales); mysql_select_db($database_geQuote, $geQuote); $query_listCustomer = "SELECT customer.cust_id, customer.name FROM customer ORDER BY customer.name"; $listCustomer = mysql_query($query_listCustomer, $geQuote) or die(mysql_error()); $row_listCustomer = mysql_fetch_assoc($listCustomer); $totalRows_listCustomer = mysql_num_rows($listCustomer); $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_POST['Submit']) ) { echo "<pre>"; print_r($_POST); } if( isset($_GET['ajax']) ) { //In this if statement switch($_GET['cust_id']) { case "quote_to": $query_custData = sprintf("SELECT contact AS dbinfo FROM customer WHERE cust_id=",$_GET['ajax']); break; case "email_to": $query_custData = sprintf("SELECT email AS dbinfo FROM customer WHERE cust_id=",$_GET['ajax']); break; } $result_custData = mysql_query($query_custData); echo ""; while ($row_custData = mysql_fetch_assoc($result_custData)) { echo "{$row_custData['dbinfo']}"; } exit; //we're finished so 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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Graphic Edge Printing</title> <script language="javascript"> function ajaxFunction(ID, Param) { //link to the PHP file your getting the data from //var loaderphp = "quote.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //the line below reset the third list box incase list 1 is changed document.getElementById('quote_to').innerHTML = ""; //the line below reset the second list box incase list 1 is changed document.getElementById('email_to').innerHTML = ""; //THIS SET THE DAT FROM THE PHP TO THE HTML document.getElementById(cust_id).innerHTML = xmlHttp.responseText; } } xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true); xmlHttp.send(null); } </script> <?php include('style_rules.php'); ?> </head> <body> <div id="wrapper"> <div id="titlebar"><img src="images/header.jpg" alt="Graphic Edge Printing" /></div> <div id="maincontent"> <div id="nav"> <?php include('navbar.php'); ?> </div> <h1>New Quote</h1> <form action="<?php echo $editFormAction; ?>" method="POST" name="insertQuote" id="insertQuote" target="_self"> <p><table width="650"> <tr> <td width="200" height="25"><div align="right"> <h2>Sales Rep: </h2> </div></td> <td><select name="sales_id" id="sales_id"> <?php do { ?> <option value="<?php echo $row_listSales['sales_id']?>"><?php echo $row_listSales['salesRep']?></option> <?php } while ($row_listSales = mysql_fetch_assoc($listSales)); $rows = mysql_num_rows($listSales); if($rows > 0) { mysql_data_seek($listSales, 0); $row_listSales = mysql_fetch_assoc($listSales); } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Customer:</h2> </div></td> <td><select name="cust_id" id="cust_id" onchange="ajaxFunction('quote_to', this.value);"> <?php do { ?> <option value="<?php echo $row_listCustomer['cust_id']?>"><?php echo $row_listCustomer['name']?></option> <?php } while ($row_listCustomer = mysql_fetch_assoc($listCustomer)); $rows = mysql_num_rows($listCustomer); if($rows > 0) { mysql_data_seek($listCustomer, 0); $row_listCustomer = mysql_fetch_assoc($listCustomer); } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Quoted to:</h2> </div></td> <td><input name="quote_to" type="text" class="widebox" id="quote_to" maxlength="50" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Email to:</h2> </div></td> <td><input name="email_to" type="text" class="widebox" id="email_to" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Title:</h2> </div></td> <td><input name="title" type="text" class="widebox" id="title" maxlength="255" /></td> </tr> <tr> <td width="200" height="25" valign="top"><div align="right"> <h2>Quote:</h2> </div></td> <td><textarea name="quote" id="quote" cols="54" rows="25"></textarea></td> </tr> <tr> <td width="200" height="25"><div align="right"></div></td> <td><input type="submit" name="Submit" value="Submit Quote" /></td> </tr> </table> <input name="created" type="hidden" id="created" value="NOW()" /> <input type="hidden" name="MM_insert" value="insertQuote"> </form> </div> <div id="footer"><?php include('copyright.php'); ?></div> </div> </body> </html> <?php mysql_free_result($listSales); mysql_free_result($listCustomer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/ Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 After a quick look.. i seams theirs a few things that would cause problems.. 1. case "quote_to": $query_custData = sprintf("SELECT contact AS dbinfo FROM customer WHERE cust_id=",$_GET['ajax']); break; case "email_to": $query_custData = sprintf("SELECT email AS dbinfo FROM customer WHERE cust_id=",$_GET['ajax']); break; should probably be case "quote_to": $query_custData = sprintf("SELECT contact AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; case "email_to": $query_custData = sprintf("SELECT email AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; also why are you clearing these document.getElementById('quote_to').innerHTML = ""; document.getElementById('email_to').innerHTML = ""; as email_to will always be blank! and this document.getElementById(cust_id).innerHTML = xmlHttp.responseText; should be document.getElementById('cust_id').innerHTML = xmlHttp.responseText; Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-769944 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 ok, I made those changes, now when I try to select a customer, the whole customer list clears. sorry to post the whole code, I'm not sure what is causing the problem so I don't know what I can leave out. Thank you for your help! <?php require_once('Connections/geQuote.php'); ?> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = "quote"; $MM_donotCheckaccess = "false"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && false) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "noaccess.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = "quote.php"; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $editFormAction = "quote.php"; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertQuote")) { $insertSQL = sprintf("INSERT INTO quote (sales_id, cust_id, quote_to, email_to, title, quote, created) VALUES (%s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['sales_id'], "int"), GetSQLValueString($_POST['cust_id'], "int"), GetSQLValueString($_POST['quote_to'], "text"), GetSQLValueString($_POST['email_to'], "text"), GetSQLValueString($_POST['title'], "text"), GetSQLValueString($_POST['quote'], "text"), $_POST['created']); mysql_select_db($database_geQuote, $geQuote); $Result1 = mysql_query($insertSQL, $geQuote) or die(mysql_error()); $insertGoTo = "quote_listsort.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_geQuote, $geQuote); $query_listSales = "SELECT salesrep.sales_id, CONCAT(salesrep.first_name,' ', salesrep.last_name) AS salesRep FROM salesrep ORDER BY salesrep.last_name, salesrep.first_name"; $listSales = mysql_query($query_listSales, $geQuote) or die(mysql_error()); $row_listSales = mysql_fetch_assoc($listSales); $totalRows_listSales = mysql_num_rows($listSales); mysql_select_db($database_geQuote, $geQuote); $query_listCustomer = "SELECT customer.cust_id, customer.name FROM customer ORDER BY customer.name"; $listCustomer = mysql_query($query_listCustomer, $geQuote) or die(mysql_error()); $row_listCustomer = mysql_fetch_assoc($listCustomer); $totalRows_listCustomer = mysql_num_rows($listCustomer); $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_POST['Submit']) ) { echo "<pre>"; print_r($_POST); } if( isset($_GET['ajax']) ) { //In this if statement switch($_GET['cust_id']) { case "quote_to": $query_custData = sprintf("SELECT contact AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; case "email_to": $query_custData = sprintf("SELECT email AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; } $result_custData = mysql_query($query_custData); echo ""; while ($row_custData = mysql_fetch_assoc($result_custData)) { echo "{$row_custData['dbinfo']}"; } exit; //we're finished so 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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Graphic Edge Printing</title> <script language="javascript"> function ajaxFunction(ID, Param) { //link to the PHP file your getting the data from //var loaderphp = "quote.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //THIS SET THE DAT FROM THE PHP TO THE HTML document.getElementById('cust_id').innerHTML = xmlHttp.responseText; } } xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true); xmlHttp.send(null); } </script> <?php include('style_rules.php'); ?> </head> <body> <div id="wrapper"> <div id="titlebar"><img src="images/header.jpg" alt="Graphic Edge Printing" /></div> <div id="maincontent"> <div id="nav"> <?php include('navbar.php'); ?> </div> <h1>New Quote</h1> <form action="<?php echo $editFormAction; ?>" method="POST" name="insertQuote" id="insertQuote" target="_self"> <p><table width="650"> <tr> <td width="200" height="25"><div align="right"> <h2>Sales Rep: </h2> </div></td> <td><select name="sales_id" id="sales_id"> <?php do { ?> <option value="<?php echo $row_listSales['sales_id']?>"><?php echo $row_listSales['salesRep']?></option> <?php } while ($row_listSales = mysql_fetch_assoc($listSales)); $rows = mysql_num_rows($listSales); if($rows > 0) { mysql_data_seek($listSales, 0); $row_listSales = mysql_fetch_assoc($listSales); } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Customer:</h2> </div></td> <td><select name="cust_id" id="cust_id" onchange="ajaxFunction('quote_to', this.value);"> <?php do { ?> <option value="<?php echo $row_listCustomer['cust_id']?>"><?php echo $row_listCustomer['name']?></option> <?php } while ($row_listCustomer = mysql_fetch_assoc($listCustomer)); $rows = mysql_num_rows($listCustomer); if($rows > 0) { mysql_data_seek($listCustomer, 0); $row_listCustomer = mysql_fetch_assoc($listCustomer); } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Quoted to:</h2> </div></td> <td><input name="quote_to" type="text" class="widebox" id="quote_to" maxlength="50" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Email to:</h2> </div></td> <td><input name="email_to" type="text" class="widebox" id="email_to" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Title:</h2> </div></td> <td><input name="title" type="text" class="widebox" id="title" maxlength="255" /></td> </tr> <tr> <td width="200" height="25" valign="top"><div align="right"> <h2>Quote:</h2> </div></td> <td><textarea name="quote" id="quote" cols="54" rows="25"></textarea></td> </tr> <tr> <td width="200" height="25"><div align="right"></div></td> <td><input type="submit" name="Submit" value="Submit Quote" /></td> </tr> </table> <input name="created" type="hidden" id="created" value="NOW()" /> <input type="hidden" name="MM_insert" value="insertQuote"> </form> </div> <div id="footer"><?php include('copyright.php'); ?></div> </div> </body> </html> <?php mysql_free_result($listSales); mysql_free_result($listCustomer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770082 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 Its kinda hard for me to run the code (not having the DB etc) but from what i can read i have updated it (see below) However.. the data your getting from the database.. what do you want to do with it ? as your pointing it to a textbox.. or did you want a list ? the code below i have fixed a few parts but also re-labled the text box to quote_to-old and added a div labled quote_to this sould give you an output but probably not how you want it.. <?php require_once('Connections/geQuote.php'); ?> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = "quote"; $MM_donotCheckaccess = "false"; // *** Restrict Access To Page: Grant or deny access to this page function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && false) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "noaccess.php"; if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = "quote.php"; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit; } ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $editFormAction = "quote.php"; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertQuote")) { $insertSQL = sprintf("INSERT INTO quote (sales_id, cust_id, quote_to, email_to, title, quote, created) VALUES (%s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['sales_id'], "int"), GetSQLValueString($_POST['cust_id'], "int"), GetSQLValueString($_POST['quote_to'], "text"), GetSQLValueString($_POST['email_to'], "text"), GetSQLValueString($_POST['title'], "text"), GetSQLValueString($_POST['quote'], "text"), $_POST['created']); mysql_select_db($database_geQuote, $geQuote); $Result1 = mysql_query($insertSQL, $geQuote) or die(mysql_error()); $insertGoTo = "quote_listsort.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_geQuote, $geQuote); $query_listSales = "SELECT salesrep.sales_id, CONCAT(salesrep.first_name,' ', salesrep.last_name) AS salesRep FROM salesrep ORDER BY salesrep.last_name, salesrep.first_name"; $listSales = mysql_query($query_listSales, $geQuote) or die(mysql_error()); $row_listSales = mysql_fetch_assoc($listSales); $totalRows_listSales = mysql_num_rows($listSales); mysql_select_db($database_geQuote, $geQuote); $query_listCustomer = "SELECT customer.cust_id, customer.name FROM customer ORDER BY customer.name"; $listCustomer = mysql_query($query_listCustomer, $geQuote) or die(mysql_error()); $row_listCustomer = mysql_fetch_assoc($listCustomer); $totalRows_listCustomer = mysql_num_rows($listCustomer); $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_POST['Submit']) ) { echo "<pre>"; print_r($_POST); } if( isset($_GET['ajax']) ) { //In this if statement switch($_GET['ID']) { case "quote_to": $query_custData = sprintf("SELECT contact AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; case "email_to": $query_custData = sprintf("SELECT email AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; } $result_custData = mysql_query($query_custData); echo ""; while ($row_custData = mysql_fetch_assoc($result_custData)) { echo "{$row_custData['dbinfo']}"; } exit; //we're finished so 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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Graphic Edge Printing</title> <script language="javascript"> function ajaxFunction(ID, Param) { //link to the PHP file your getting the data from //var loaderphp = "quote.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //THIS SET THE DAT FROM THE PHP TO THE HTML document.getElementById(ID).innerHTML = xmlHttp.responseText; } } xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true); xmlHttp.send(null); } </script> <?php include('style_rules.php'); ?> </head> <body> <div id="wrapper"> <div id="titlebar"><img src="images/header.jpg" alt="Graphic Edge Printing" /></div> <div id="maincontent"> <div id="nav"> <?php include('navbar.php'); ?> </div> <h1>New Quote</h1> <form action="<?php echo $editFormAction; ?>" method="POST" name="insertQuote" id="insertQuote" target="_self"> <p><table width="650"> <tr> <td width="200" height="25"><div align="right"> <h2>Sales Rep: </h2> </div></td> <td><select name="sales_id" id="sales_id"> <?php do { ?> <option value="<?php echo $row_listSales['sales_id']?>"><?php echo $row_listSales['salesRep']?></option> <?php } while ($row_listSales = mysql_fetch_assoc($listSales)); $rows = mysql_num_rows($listSales); if($rows > 0) { mysql_data_seek($listSales, 0); $row_listSales = mysql_fetch_assoc($listSales); } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Customer:</h2> </div></td> <td><select name="cust_id" id="cust_id" onchange="ajaxFunction('quote_to', this.value);"> <?php do { ?> <option value="<?php echo $row_listCustomer['cust_id']?>"><?php echo $row_listCustomer['name']?></option> <?php } while ($row_listCustomer = mysql_fetch_assoc($listCustomer)); $rows = mysql_num_rows($listCustomer); if($rows > 0) { mysql_data_seek($listCustomer, 0); $row_listCustomer = mysql_fetch_assoc($listCustomer); } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Quoted to:</h2> </div></td> <td> <div id="quote_to"></div> <input name="quote_to-old" type="text" class="widebox" id="quote_to-old" maxlength="50" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Email to:</h2> </div></td> <td><input name="email_to" type="text" class="widebox" id="email_to" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Title:</h2> </div></td> <td><input name="title" type="text" class="widebox" id="title" maxlength="255" /></td> </tr> <tr> <td width="200" height="25" valign="top"><div align="right"> <h2>Quote:</h2> </div></td> <td><textarea name="quote" id="quote" cols="54" rows="25"></textarea></td> </tr> <tr> <td width="200" height="25"><div align="right"></div></td> <td><input type="submit" name="Submit" value="Submit Quote" /></td> </tr> </table> <input name="created" type="hidden" id="created" value="NOW()" /> <input type="hidden" name="MM_insert" value="insertQuote"> </form> </div> <div id="footer"><?php include('copyright.php'); ?></div> </div> </body> </html> <?php mysql_free_result($listSales); mysql_free_result($listCustomer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770131 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 The data is simply a contact name (John Doe) and the email (JDoe@hotmail.com) associated with the customer account that was selected. I have lazy salesmen. the DB structure is as follows: CREATE TABLE IF NOT EXISTS `customer` ( `cust_id` int(10) unsigned NOT NULL auto_increment, `sales_id` int(10) unsigned default NULL, `name` varchar(40) NOT NULL default '', `street` varchar(50) default NULL, `city` varchar(50) default NULL, `state` char(2) default NULL, `zip` int(5) default '0', `phone` varchar(12) default NULL, `fax` varchar(12) default NULL, `contact` varchar(40) default NULL, `cust_email` varchar(50) default NULL, PRIMARY KEY (`cust_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=395 ; The coding pulls all the customer ID's and names from the table to fill the dropdown, what I want to do is have it pull the contact info and email to populate the appropriate text fields when the salesman picks a customer. Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770175 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 &(*%#Q&$ this is annoying me. I'm having a heck of time getting this code to crack for me!! so VERY frustrating! Ok, I see what mean by not working the way I want. What about by using the AJAX to update a variable that's being plugged into the text field value? so instead of this: $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_POST['Submit']) ) { echo "<pre>"; print_r($_POST); } if( isset($_GET['ajax']) ) { //In this if statement switch($_GET['ID']) { case "quote_to": $query_custData = sprintf("SELECT contact AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; case "email_to": $query_custData = sprintf("SELECT email AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; } $result_custData = mysql_query($query_custData); echo ""; while ($row_custData = mysql_fetch_assoc($result_custData)) { echo "{$row_custData['dbinfo']}"; } exit; //we're finished so exit.. } maybe like this?? //call out variables as empty strings $updateContact = ""; $updatedEmail = ""; //then run script $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_POST['Submit']) ) { echo "<pre>"; print_r($_POST); } if( isset($_GET['ajax']) ) { //In this if statement switch($_GET['ID']) { case "quote_to": $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d",$_GET['ajax']); break; } $result_custData = mysql_query($query_custData); echo ""; while ($row_custData = mysql_fetch_assoc($result_custData)) { $updateEmail = $row_custData['email']; $updateContact = $row_custData['contact']; } exit; //we're finished so exit.. } only it's not working! Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770252 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 &(*%#Q&$ this is annoying me. I'm having a heck of time getting this code to crack for me!! so VERY frustrating! maybe like this?? //call out variables as empty strings //snip.. only it's not working! Nope it doesn't work like that! Okay .. Just to confirm.. Lazy Salesman selects Customer Name from a drop down (value = Customers ID) form this point you want to populate the remaining fields.. quote_to = contact email_to = email If so... (backup first.. i havn't tested any of this) first change if( isset($_GET['ajax']) ) { //In this if statement switch($_GET['ID']) { case "quote_to": $query_custData = sprintf("SELECT contact AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; case "email_to": $query_custData = sprintf("SELECT email AS dbinfo FROM customer WHERE cust_id=%d",$_GET['ajax']); break; } $result_custData = mysql_query($query_custData); echo ""; while ($row_custData = mysql_fetch_assoc($result_custData)) { echo "{$row_custData['dbinfo']}"; } exit; //we're finished so exit.. } to if( isset($_GET['ajax']) ) { $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['ajax']); $result_custData = mysql_query($query_custData); $row_custData = mysql_fetch_assoc($result_custData); echo $row_custData['contact'].":::".$row_custData['email']; exit; //we're finished so exit.. } remove this line <div id="quote_to"></div> change <input name="quote_to-old" type="text" class="widebox" id="quote_to-old" maxlength="50" /></td> back to <input name="quote_to" type="text" class="widebox" id="quote_to" maxlength="50" /></td> and now change xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //THIS SET THE DAT FROM THE PHP TO THE HTML document.getElementById(ID).innerHTML = xmlHttp.responseText; } } To xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { var re = /^(.*?)::.*?)$/i; var match = re.exec(xmlHttp.responseText); if (match != null) { document.getElementById('quote_to').innerHTML = match[1]; document.getElementById('email_to').innerHTML = match[2]; } } } Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770253 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 ok, changed that. It's not failing, but it's not updating.... does something in this line need to change for the new function? <select name="cust_id" id="cust_id" onchange="ajaxFunction('quote_to', this.value);"> Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770283 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 oops document.getElementById('quote_to').innerHTML = match[1]; document.getElementById('email_to').innerHTML = match[2]; should be document.getElementById('quote_to').value= match[1]; document.getElementById('email_to').value= match[2]; ajaxFunction function doesn't need the first parameter anymore.. Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770364 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 ok...still not working. we have this part of the script: $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_GET['ajax']) ) { $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['ajax']); $result_custData = mysql_query($query_custData); $row_custData = mysql_fetch_assoc($result_custData); echo $row_custData['contact'].":::".$row_custData['email']; exit; //we're finished so exit.. } The Function: <script language="javascript"> function ajaxFunction(ID, Param) { //link to the PHP file your getting the data from //var loaderphp = "quote.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { var re = /^(.*?)::.*?)$/i; var match = re.exec(xmlHttp.responseText); if (match != null) { document.getElementById('quote_to').value= match[1]; document.getElementById('email_to').value= match[2]; } } } xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true); xmlHttp.send(null); } </script> and the form data: <select name="cust_id" id="cust_id" onchange="ajaxFunction('quote_to', this.value);"> <?php do { ?> <option value="<?php echo $row_listCustomer['cust_id']?>"><?php echo $row_listCustomer['name']?></option> <?php } while ($row_listCustomer = mysql_fetch_assoc($listCustomer)); $rows = mysql_num_rows($listCustomer); if($rows > 0) { mysql_data_seek($listCustomer, 0); $row_listCustomer = mysql_fetch_assoc($listCustomer); } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Quoted to:</h2> </div></td> <td> <input name="quote_to" type="text" class="widebox" id="quote_to" maxlength="50" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Email to:</h2> </div></td> <td><input name="email_to" type="text" class="widebox" id="email_to" /></td> </tr> must be missing something that needs to be updated or isn't calling for the right data...but what? Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770399 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 Okay i have re-created it (kinda) as i don't have the database i have create a dumb version,, i hope it makes sense, (if you run this by itself to test first) <?php #$selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_GET['cust_id']) ) { $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['cust_id']); #$result_custData = mysql_query($query_custData); #$row_custData = mysql_fetch_assoc($result_custData); //Uncomment above and comment below //Fake Database $DATA = array( 1=> array("contact" =>"My Contact1", "email"=> "My Email1"), 2=> array("contact" =>"My Contact2", "email"=> "My Email2") ); $row_custData = $DATA[$_GET['cust_id']]; echo $row_custData['contact'].":::".$row_custData['email']; exit; //we're finished so exit.. } ?> <script language="javascript"> function ajaxFunction(cust_id) { //link to the PHP file your getting the data from //var loaderphp = "quote.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { var re = /^(.*?)::.*?)$/i; var match = re.exec(xmlHttp.responseText); if (match != null) { document.getElementById('quote_to').value= match[1]; document.getElementById('email_to').value= match[2]; } } } xmlHttp.open("GET", loaderphp+"?cust_id="+cust_id,true); xmlHttp.send(null); } </script> <select name="cust_id" id="cust_id" onchange="ajaxFunction(this.value);"> <!--Fake Values Start--> <option value="1">one</option> <option value="2">two</option> <!--Fake Values End--> <?php /* do { ?> <option value="<?php echo $row_listCustomer['cust_id']?>"><?php echo $row_listCustomer['name']?></option> <?php /* } while ($row_listCustomer = mysql_fetch_assoc($listCustomer)); $rows = mysql_num_rows($listCustomer); if($rows > 0) { mysql_data_seek($listCustomer, 0); $row_listCustomer = mysql_fetch_assoc($listCustomer); } */ ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Quoted to:</h2> </div></td> <td> <input name="quote_to" type="text" class="widebox" id="quote_to" maxlength="50" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Email to:</h2> </div></td> <td><input name="email_to" type="text" class="widebox" id="email_to" /></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770425 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 okay i finished cooking so i'm off to eat.. I hope your doing well but for some debugging if you try test.php?cust_id=1 you should get a page thats says My Contact1:::My Email1 if you try the same (with a valid id and the database is used) check to see if you get anything else that may help you find the bug Back in a while Regards Richard Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770464 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 testing the test script works, but when I try to pull the info from my db I get nothing. I've picked apart the script as best as I can and I'm thinking there's something wrong with the query. Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770494 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 thanks for the help, enjoy your dinner! I'm going to take another crack at it and see what I get debugging the query otherwise I think I'll put it aside and try again tomorrow with a clear head. Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770497 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 Heres another attempt.. i'll let you debug some and see what you get <?php $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_GET['cust_id']) ) { $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['cust_id']); $result_custData = mysql_query($query_custData); $row_custData = mysql_fetch_assoc($result_custData); echo $row_custData['contact'].":::".$row_custData['email']; exit; //we're finished so exit.. } ?> <script language="javascript"> function ajaxFunction(cust_id) { //link to the PHP file your getting the data from //var loaderphp = "quote.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { var re = /^(.*?)::.*?)$/i; var match = re.exec(xmlHttp.responseText); if (match != null) { document.getElementById('quote_to').value= match[1]; document.getElementById('email_to').value= match[2]; } } } xmlHttp.open("GET", loaderphp+"?cust_id="+cust_id,true); xmlHttp.send(null); } </script> <select name="cust_id" id="cust_id" onchange="ajaxFunction(this.value);"> <?php $listCustomer = mysql_query("SELECT cust_id, name FROM customer"); while ($row_listCustomer = mysql_fetch_assoc($listCustomer)) { echo "<option value=\"{$row_listCustomer['cust_id']}\">{$row_listCustomer['name']}</option>\n"; } ?> </select> </td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Quoted to:</h2> </div></td> <td> <input name="quote_to" type="text" class="widebox" id="quote_to" maxlength="50" /></td> </tr> <tr> <td width="200" height="25"><div align="right"> <h2>Email to:</h2> </div></td> <td><input name="email_to" type="text" class="widebox" id="email_to" /></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770509 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 I think I've narrowed down the problem to this section: $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['cust_id']); $result_custData = mysql_query($query_custData); $row_custData = mysql_fetch_assoc($result_custData); somehow I think the cust_id isn't getting passed into the query, or something else is wrong with the query. Your test script worked fine. when I replaced with the database variables, nothing happens. I tried replacing the dynamically populated drop down with hard coded numbers, 1, 10, 15, 20, 25. I confirmed that they all were cust_id #'s in the database, and that didn't work either. my mind is drawing a blank on what code to add to test the query results. Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770535 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 btw, thank you for all your help and patience with my horrid coding skills! Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770536 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 ok....so this works: $DATA = array( 1=> array("contact" =>"My Contact1", "email"=> "My Email1"), 2=> array("contact" =>"My Contact2", "email"=> "My Email2") ); $row_custData = $DATA[$_GET['cust_id']]; this does not: $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['cust_id']); $result_custData = mysql_query($query_custData); $row_custData = mysql_fetch_assoc($result_custData); The questions is...why? it's rhetorical, I'm thinking out loud...erm, so to speak. unless you have an answer? Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770549 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 I've tried dropping the sprintf from the query and running like so: $query_custData = "SELECT contact, email FROM customer WHERE cust_id=".$_GET['cust_id']; That didn't work either. Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770561 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 find this line xmlHttp.open("GET", loaderphp+"?cust_id="+cust_id,true); and add alert(loaderphp+"?cust_id="+cust_id); xmlHttp.open("GET", loaderphp+"?cust_id="+cust_id,true); now you should get an alert ie test.php?cust_id=2 try trying typing that in the browser see what you get back Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770596 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 got the following error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /data/14/1/66/102/1066102/user/1100571/htdocs/ge/custtest.php on line 16 ::: Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770598 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 Lines 14-17 $query_custData = "SELECT contact, email FROM customer WHERE cust_id=".$_GET['cust_id']; $result_custData = mysql_query($query_custData); $row_custData = mysql_fetch_assoc($result_custData); echo $row_custData['contact'].":::".$row_custData['email']; Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770599 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 changed to this: if( isset($_GET['cust_id']) ) { $query_custData = sprintf("SELECT contact, email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['cust_id']); $result_custData = mysql_query($query_custData, $geQuote) or die(mysql_error()); $row_custData = mysql_fetch_assoc($result_custData); echo $row_custData['contact'].":::".$row_custData['email']; exit; //we're finished so exit.. } got this error Unknown column 'email' in 'field list' Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770605 Share on other sites More sharing options...
Alith7 Posted February 24, 2009 Author Share Posted February 24, 2009 rawr! ok, so I found the flippin error the field in the table was cust_email not email. fixed that, passing this code in the address custtest.php?cust_id=105 returned the correct data Julie:::JGomels@custwww.com but now I need the text fields to update. here is what I have for test code atm: <?php require_once('Connections/geQuote.php'); ?> <?php mysql_select_db($database_geQuote, $geQuote); $query_listCustomer = "SELECT customer.cust_id, customer.name FROM customer ORDER BY customer.name"; $listCustomer = mysql_query($query_listCustomer, $geQuote) or die(mysql_error()); $row_listCustomer = mysql_fetch_assoc($listCustomer); $totalRows_listCustomer = mysql_num_rows($listCustomer); $selected=mysql_select_db($database_geQuote, $geQuote); if( isset($_GET['cust_id']) ) { $query_custData = sprintf("SELECT contact, cust_email FROM customer WHERE cust_id=%d LIMIT 0,1",$_GET['cust_id']); $result_custData = mysql_query($query_custData, $geQuote) or die(mysql_error()); $row_custData = mysql_fetch_assoc($result_custData); echo $row_custData['contact'].":::".$row_custData['cust_email']; exit; //we're finished so 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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> <script language="javascript"> function ajaxFunction(cust_id) { //link to the PHP file your getting the data from //var loaderphp = "quote.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { var re = /^(.*?)::.*?)$/i; var match = re.exec(xmlHttp.responseText); if (match != null) { document.getElementById('quote_to').value= match[1]; document.getElementById('email_to').value= match[2]; } } } alert(loaderphp+"?cust_id="+cust_id); xmlHttp.open("GET", loaderphp+"?cust_id="+cust_id,true); xmlHttp.send(null); } </script> </head> <body> <select name="cust_id" id="cust_id" onchange="ajaxFunction(this.value);"> <?php do { ?> <option value="<?php echo $row_listCustomer['cust_id']?>"><?php echo $row_listCustomer['name']?></option> <?php } while ($row_listCustomer = mysql_fetch_assoc($listCustomer)); $rows = mysql_num_rows($listCustomer); if($rows > 0) { mysql_data_seek($listCustomer, 0); $row_listCustomer = mysql_fetch_assoc($listCustomer); } ?> </select> <h2>Quoted to:</h2> <input name="quote_to" type="text" class="widebox" id="quote_to" maxlength="50" /> <h2>Email to:</h2> <input name="email_to" type="text" class="widebox" id="email_to" /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770615 Share on other sites More sharing options...
MadTechie Posted February 25, 2009 Share Posted February 25, 2009 Humm.. can't see a problem.. add the following 3 alerts xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { alert(xmlHttp.responseText); var re = /^(.*?)::.*?)$/i; var match = re.exec(xmlHttp.responseText); if (match != null) { alert(match[1]+"---"+match[2]); document.getElementById('quote_to').value= match[1]; document.getElementById('email_to').value= match[2]; } alert("done"); } } you should get "Julie:::JGomels@custwww.com" = getting a reply "Julie---JGomels@custwww.com" = Matched data "Done" -- no error on matching (doesn't mean it matched) Quote Link to comment https://forums.phpfreaks.com/topic/146542-solved-need-help-with-ajax-script/#findComment-770663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.