...

View Full Version : updating a text field and drop down at same time...



Alith7
07-17-2009, 09:03 PM
I have a DB query that returns CustomerID, CustomerName, CustomerContact, CustomerSalesID. This Query populates a dropdown.

I need to have the Customer Contact applied to a text field and the customer Sales ID used to select the SalesRep from a different drop down when you select a customer.

Not too hard I'd think. Any ideas on the code though? I don't think I need to go as fancy as AJAX since I've already called the query when the page loads, that data is already there.

fside
07-19-2009, 08:51 AM
that data is already there.

Seems strange to me that as eager as people are to answer questions, no one has tried with this - if just to ask.

That data is already - where? If "there", then why can't you use script to enter it into the appropriate controls?

Alith7
07-19-2009, 03:12 PM
by there I mean that I already called the info in the DB Query when I pulled the info populate the customer dropdown. i'm just not sure how to write the Javascript to pull the info specific to the customer selected and apply it to the text field and select the correct item from the second dropdown.

Does that make sense?

fside
07-19-2009, 03:23 PM
i'm just not sure how to write the Javascript to pull the info specific to the customer selected and apply it to the text field and select the correct item from the second dropdown.


It's coming in as XML, it parses without error, and you can use XPATH in Javascript to find nodes?

What would a portion of the relevant XML look like? Again, I'm guess about all this. You could be using JSON for all I know. But . . . to ask.

Alith7
07-20-2009, 12:32 AM
actually, that's where I'm hung up.
I don't have it as XML.
I'm looking for how to grab the XML from the PHP file and pass it through the AJAX.
I'm REALLY new to this. Just started learning a few days ago, if I can find an example script of how to get AJAX to process multiple XML elements, I think I can figure it out, but I can't FIND anything!

Oh yeah, and I'm looking to use AJAX and javascript and maybe Jquery if necessary.

fside
07-20-2009, 03:43 AM
I'm looking for how to grab the XML from the PHP file and pass it through the AJAX.


The xmlhttprequest object, which IS 'ajax', has certain properties and methods. After creating such an object, a send() method creates communication with the server so that state change and ready properties are updated. Just a simple check - so you have:

a) You successfully have created an new xmlhttprequest object in Javascript? It's ready to be used.

b) You have a routine to handle changes in this object's state? You have a function/handler for this object's .onstatechange property.

c) You have set the object's .open method to GET whatever from your PHP file on the server

d) We'll say your PHP is using the "echo" command. You're echoing something from the PHP file

e) You started things going with the object's .send() method? So now the server is sending something.

f) in your state change function, you've checked .readyState property to see if it's equal to 4 (using at least two equals signs, not one)

g) you finally pass through on the .readyState and you assigned the .responseText property a name.

If you have a name for the response text, you can then simply use the .innerHTML property of whichever page element, and it will show up on your page in that HTML element.

Somewhere it's breaking down in that list. If so, let me know. Those items concern just one or a few lines of Javascript. Or is it a matter of using mySQL or some database, a PHP question?

Alith7
07-20-2009, 05:33 PM
ok, Where I'm stuck is G.
How do I write the script to pull the XML data to pass back to the page.

Give me a bit and I'll put the code up for what I have so far.

Thank you!

fside
07-20-2009, 06:41 PM
ok, Where I'm stuck is G.
How do I write the script to pull the XML data to pass back to the page.

Give me a bit and I'll put the code up for what I have so far.

Thank you!

If you're all the way to g) and you passed ready state 4 - your response is waiting for you in the ".responseText" property of your object. You've got the XML, in other words.

It's XML and you want to parse it? Correct?

Just like with getting your 'ajax' object, you've got to try for both IE and - I guess every other browser. It seems to break down that way, these days. You need a DOM Document in which to read and hold your XML response. People used to first generate XML files and then read in this way. Before 'ajax'. No .responseText in other words.

So IE uses ActiveX. Your DOM Document will be an ActiveX object.



try{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(text);
} catch


If it's not IE:



} catch (e){
try{
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(text, "text/xml");
} catch (e){
alert( error.message );
return;
}
}


Hope you aren't getting the error message. Might be malformed XML (unlikely, though, if it's being generated with built-in server functions).

But if all went well, you've got the hierarchy stored as such in the xmlDoc object you just created and loaded.

Now XPath can be used to pick out certain nodes, as I'm sure you know.

Alith7
07-20-2009, 11:19 PM
I'm sorry, I think you have the mis-understanding that I know what I'm doing (not your fault, completely mine :o ).
I sort of do. At least in that I -think- I know what should be done, just have no clue how to go about doing it. I don't know much (pretty much nothing) about javascript/AJAX/Jquery

so, here is what I have, this might help you to know where I'm at and what I need help with.

Form code:

<form id="order1" name="order1" method="post" action="">
<table width="650" border="0" cellspacing="0" cellpadding="2" align="center">
<tr>
<td width="128">Customer:</td>
<td width="189"><select name="select" tabindex="1" id="custName" onchange="selCust(this.value)">
<?php do {?>
<option value="<?php echo $row_customerlist['cust_id']?>"><?php echo $row_customerlist['name']?></option>
<?php } while ($row_customerlist = mysql_fetch_assoc($customerlist));
$rows = mysql_num_rows($customerlist);
if($rows > 0) {
mysql_data_seek($customerlist, 0);
$row_customerlist = mysql_fetch_assoc($customerlist); } ?>
</select> </td>
<td width="128">Contact:</td>
<td width="189"><input type="text" name="textfield" tabindex="2" id="custContact" /></td>
</tr>
<tr>
<td>PO:</td>
<td><input type="text" name="textfield2" tabindex="3" /></td>
<td>Sales Rep:</td>
<td><select name="select2" tabindex="4" id="salesRep">
<?php do { ?>
<option value="<?php echo $row_salesList['sales_id']?>"><?php echo $row_salesList['first_name'].' '.$row_salesList['last_name']?></option>
<?php } while ($row_salesList = mysql_fetch_assoc($salesList));
$rows = mysql_num_rows($salesList);
if($rows > 0) {
mysql_data_seek($salesList, 0);
$row_salesList = mysql_fetch_assoc($salesList); } ?>
</select> </td>
</tr>
</table>
</form>

My Javascript:

function selCust(str)
{
//autofil custinfo from custName
$(str).blur(function()
{
var customername = $(str).val();
//AJAX send to customer lookup, which outputs XML Data
$.post("/scripts/updatecust.php",{customer:customername},
function(xml)
{
//fill location fields with data returned from customer lookup
var custContact = $("custContact", xml).text();
var salesRep = $("salesRep", xml).text();
//now update fields
$("#custContact").attr("value",custContact);
$("#salesRep").val(salesRep);
});
}

and my php script:

<?php
header('Content-type:text/xml'); //output as xml

require_once('geQuote.php');


$custName = $_REQUEST['custName'];

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;
}
}

mysql_select_db($database_geQuote, $geQuote);
$query_getCustInfo = "SELECT customer.sales_id, customer.name, customer.contact FROM customer WHERE (customer.cust_id=$custName)";
$getCustInfo = mysql_query($query_getCustInfo, $geQuote) or die(mysql_error());
$row_getCustInfo = mysql_fetch_assoc($getCustInfo);
$totalRows_getCustInfo = mysql_num_rows($getCustInfo);

while ($row_getCustInfo = mysql_fetch_array($getCustInfo)) {
$custContact = $row_getCustInfo['customer.contact'];
$salesRep = $row_getCustInfo['customer.sales_id'];
}

$returnXML = "<response>
<custContact>$custcontact</custContact>
<salesRep>$salesRep</salesRep>
</response>";

echo $returnXML;

mysql_free_result($getCustInfo);
?>

fside
07-21-2009, 12:51 PM
so, here is what I have, this might help you to know where I'm at and what I need help with.


For showing some of this, maybe someone else here will spot an obvious error. But I asked to your OP if you were already getting a response. And then I asked if you made it to a point in a request where you were receiving. And you said you've made it that far.

Are you getting anything from the server, or not?

If you are already able to fill in some of the data, then maybe you are looking through the returned XML in the wrong way?

I suggested a very simplified form of 'ajax', really the minimum you'd need to send/receive. So are you able to tell if you're receiving any response to your server request? Maybe a simplified form of 'ajax' would show if at least the basics are working. Then it might suggest a mistake using either or both PHP or jQuery.

In fact, you could just save the xml, some test xml, as a document, a file, and just load it directly into the DOM Document as I showed. See if the XML will even load properly, though if it's being server generated I would expect that's probably not where the problem lies - but ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum