PDA

View Full Version : Resolved Getting two values from AJAX json array



auriaks
03-27-2010, 08:03 AM
Hi,

I have script.php page:

<?php
if (isset($_GET['nick'])) {
$name = 'aurimas';
$ats = $_GET['nick'];
if ($ats == '') {$nikas = "Įveskite niką";} elseif ($name == $ats) {$nikas = "Užimtas";} else {$nikas = "Laisvas";}
}
if (isset($_GET['email'])) {
$email = 'auriaks@gmail.com';
$get_mail = $_GET['email'];
if ($email == $get_mail) {$emailas = "Užimtas";} else {$emailas = "Laisvas";}
}
$answer = array ($nikas, $emailas);
echo json_encode($answer);
?>

and I use this AJAX script to get the array from script:

// Get the HTTP Object
function gethttpObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}

// Change the value of the outputText field
function setOutput(){
if(httpObject.readyState == 4){
var answer = eval('(' + httpObject.responseText + ')');
document.getElementById('nikas').innerHTML = answer[0];
document.getElementById('emailas').innerHTML = answer[1];
}

}

// Implement business logic
function doWork(){
httpObject = gethttpObject();
if (httpObject != null) {
httpObject.open("GET", "script.php?nick=" + document.getElementById('inputas').value+"&email="+document.getElementById('email').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
setInterval(doWork, 5000);

var httpObject = null;

//-->


I know that something is wrong with this part:

function setOutput(){
if(httpObject.readyState == 4){
var answer = eval('(' + httpObject.responseText + ')');
document.getElementById('nikas').innerHTML = answer[0];
document.getElementById('emailas').innerHTML = answer[1];
}

}

Because I have to get those two values from array in two different places like:

<div id='nikas'>FIRST VALUE HERE</div>
<div id='emailas'>SECOND VALUE HERE</div>

auriaks
03-27-2010, 11:17 AM
Btw, my output was in format

["Laisvas","Laisvas"]

maybe this will help...

jscheuer1
03-27-2010, 12:17 PM
Works here. Except that the ž in Užimtas was messing things up, and in IE the script.php page was getting cached, so try this for the script.php page (works here):


<?php
if (isset($_GET['nick'])) {
$name = 'aurimas';
$ats = $_GET['nick'];
if ($ats == '') {$nikas = "Iveskite nika";} elseif ($name == $ats) {$nikas = "U#0382;imtas";} else {$nikas = "Laisvas";}
}
if (isset($_GET['email'])) {
$email = 'auriaks@gmail.com';
$get_mail = $_GET['email'];
if ($email == $get_mail) {$emailas = "U#0382;imtas";} else {$emailas = "Laisvas";}
}
$answer = array ($nikas, $emailas);
echo json_encode($answer);
?>

and to fix the caching problem (on the requesting page, addition highlighted):


httpObject.open("GET", "script.php?nick=" + document.getElementById('inputas').value+"&email="+document.getElementById('email').value+"&bust="+new Date().getTime(), true);

auriaks
03-27-2010, 12:37 PM
what about ajax?? Here is something wrong:

function setOutput(){
if(httpObject.readyState == 4){
var answer = eval('(' + httpObject.responseText + ')');
document.getElementById('nikas').innerHTML = answer[0];
document.getElementById('emailas').innerHTML = answer[1];
}

}

jscheuer1
03-27-2010, 12:49 PM
All I can say is that it works here under WAMP in both Firefox and IE. Nothing wrong with that part of the code, other than the fact that it uses eval, but I suppose that would be hard to avoid here, and causes no error for the page (it might if an unexpected value were entered). Here is my requesting page:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
// Get the HTTP Object
function gethttpObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}

// Change the value of the outputText field
function setOutput(){
if(httpObject.readyState == 4){
var answer = eval('(' + httpObject.responseText + ')');
document.getElementById('nikas').innerHTML = answer[0];
document.getElementById('emailas').innerHTML = answer[1];
}

}

// Implement business logic
function doWork(){
httpObject = gethttpObject();
if (httpObject != null) {
httpObject.open("GET", "script.php?nick=" + document.getElementById('inputas').value+"&email="+document.getElementById('email').value+"&bust="+new Date().getTime(), true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
setInterval(doWork, 5000);

var httpObject = null;

</script>
</head>
<body>
<input type="text" id="inputas">
<input type="text" id="email">
<div id='nikas'>FIRST VALUE HERE</div>
<div id='emailas'>SECOND VALUE HERE</div>
</body>
</html>

My script.php page is in my previous post.

auriaks
03-27-2010, 01:00 PM
have you tried to enter those two inputs??? OUTPUT IS: undefined

jscheuer1
03-27-2010, 02:09 PM
Fully tested and works here. Go back to post #3. I had some trouble getting the editor to show the entities. Without substituting the ž for the two ž characters, I was getting 'null' or an empty string (depending upon the browser) for aurimas as nick and auriaks@gmail.com as email. But even without that change, it was fine for other values. You must pass a string value for each of the _GET's (nick and email) with the request though (even if it is an empty string):


<input type="text" id="inputas">
<input type="text" id="email">

Without those on the requesting page, you will have an error on the script.php and one on the requesting page.

However, perhaps your problem is a lack of support for some part(s) of the PHP code on the server.

If you want more help:

Please post a link to a page on your site that contains the problematic code so we can check it out.

auriaks
03-28-2010, 11:38 AM
it works... I don't know why in wamp server it doesn't worked... Thanks for help anyway.

jscheuer1
03-28-2010, 01:24 PM
So you are saying it doesn't work on your WAMP and does work live? It could depend upon settings in WAMP, like default encoding and/or PHP version.

auriaks
03-28-2010, 05:41 PM
what I can change in wamp?

Also, can you write me an example how to connect to wamp mysql? I googled, but found nothing valuable...

jscheuer1
03-28-2010, 07:42 PM
I'm not much of a WAMP expert. I'm a late adopter there actually. Therein may lie the solution though, as it works fine in WAMP here except for the character. Try uninstalling WAMP completely (including anything under users or documents and settings) and getting the latest version.

Alternatively, if there are any error messages/notices, those may give you a clue to an upgrade path and/or feature tweak that might solve it. For one thing, the json_encode function requires PHP 5.2+ and a separate json unit. Check what version of PHP your WAMP is running and if it has:

json support enabled
json version 1.2.1

on its info screen (if using the default index page, this should be: http://localhost/index.php?phpinfo=1). Other information that may provide clues may be gotten there as well. Like check for encoding. Mine apparently doesn't allow UTF-8 or encoding setting/switching in its current configuration, that's probably at least partly why I have that problem with the character.

auriaks
03-29-2010, 10:50 PM
In conclusion... It works... Thanks for your attention to my post :)

sentnel
04-13-2011, 10:38 PM
Hello!

My problem here using basically same ajax functions is that I can't split the result into the two fields, I get both results in both fields, what am I missing here?

Thanks!

php script:



<?php

$producto = $_GET['inputText'];

function buscarProducto ($producto) {
$query = "SELECT `scan_result`, `peso` FROM `pos`.`jos_inpospak_track` WHERE inputText = $producto";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
return array ($row[scan_result], $row[peso]);
}

echo implode(",", buscarProducto($producto));
?>


form with ajax / javascript functions:



<script language="javascript" type="text/javascript">
<!--
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}



// Change the value of the outputText field
function setOutput(){
if(httpObject.readyState == 4){
document.getElementById('outputText0').value = httpObject.responseText;
document.getElementById('outputText1').value = httpObject.responseText;
}

}

// Implement business logic
function doWork(){
httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "procesador.php?inputText="
+document.getElementById('inputText').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}

var httpObject = null;

//-->
</script>

<form name="testForm">
Scan:
<input type="text" onkeyup="doWork();" name="inputText" id="inputText" />
Articulo:
<input type="text" name="outputText0" id="outputText0"/>
Peso:
<input type="text" name="outputText1" id="outputText1" />

</form>

<script language="JavaScript">
<!--

document.testForm.inputText.focus();

//-->
</script>





Thanks a lot!