i have sorted xml file on the basis of item no.now i am trying to display data in javascript, but my code doesn't work, can anybody tell me what is wrong here
item.php:
$xmlFile = "items.xml";
$doc= DOMDocument::load($xmlFile);
$item = $doc->getElementsByTagName("item");
$items=array();
foreach($item as $node)
{
$itemno = $node->getElementsByTagName("itemno");
$itemno = $itemno->item(0)->nodeValue;
$quantity = $node->getElementsByTagName("quantity");
$quantity = $quantity->item(0)->nodeValue;
$available = $node->getElementsByTagName("available");
$available = $available->item(0)->nodeValue;
$items[$itemno]= array($itemno,$quantity,$available);
}
ksort($items, SORT_NUMERIC);
foreach($item AS $ite => $no)
{
$itemnum=$no[0];
$qty=$no[1];
$avail=$no[2];
echo $itemnum;
echo $qty;
echo $avail;
}
js:
var xhr = createRequest();
function getit( ) {
xhr.open("GET", 'item.php', true);
xhr.onreadystatechange = getConfirm;
xhr.send(null);
}
function getConfirm()
{
if ((xhr.readyState == 4) &&(xhr.status == 200))
{
var data = xhr.responseText;
alert(data);
}
}
try xmlrequest in this flow in your javascript:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("tbRow").innerHTML=xmlhttp.responseText;
//lo();
}
}
xmlhttp.open("GET","tbrow.php",true);
xmlhttp.send();
Here "tbRow" is a "div" id. i.e.,
<div id="tbRow"></div>
Related
here is my code what i done
function price() {
var quantity = document.getElementById("quan").value;
var select = document.getElementById("p_id");
var product_id = select.options[select.selectedIndex].value;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//alert("working");
document.getElementById("price_ajax").innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "get_product.php?p_id=" + product_id + "&quantity=" + quantity);
xmlhttp.send();
and here is my php code which execute ajax request:
$p_id = intval($_GET['p_id']);
$quantity = intval($_GET['quantity']);
$connection=new Database();
$sth=$connection->DBH->prepare("SELECT `unit_price` FROM `product_lookup` WHERE `id`=?");
$sth->bindParam(1,$p_id);
$sth->setFetchMode(PDO::FETCH_OBJ);
$rs=$sth->execute();
$row =$sth->fetch();
$product_price= intval($row->unit_price);
$total_price=$product_price*$quantity;
?>
<input value="<?php echo $p_id?>">
and here is my html :
<input class="form-control" type="number" name="price[]" id="price_ajax" >
this is where i call javascript function:
<input class="form-control" type="number" name="quantity[]" id="quan" oninput="price()" required>
this is the situation my ajax request do not response anything
I would like to be able to download media files and play them, using Ajax. My approach (below) works fine for images - but for some reason the video files (AVI) won't play.
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (type == 'Image') {
document.getElementById("image_holder").src = "data:image/png;base64," + xmlhttp.responseText;
} else {
document.getElementById("video_holder").src = "data:video/avi;base64," + xmlhttp.responseText;
}
}
};
xmlhttp.open("GET", server_url + '/get_file.php);
xmlhttp.send(null);
HTML:
<video id="outer_video_holder">
<source id="video_holder" src="" type="video/avi">
</video>
get_file.php
$sql = "SELECT * FROM file WHERE id=$file_id";
$result = $conn->query($sql);
header("Content-Type: video/avi");
if ($row = $result->fetch_assoc()) {
echo $row['content'];
}
My alert(arr[0]) equals something like
http://server/folder/images/title.png, tagname
I want to be able to display the image and use the tagname to sort the order. The build function and json etc is working. I just want to be able access path on it's own and the tag on it's own but still have them related to each other for ordering.
function importJson(str) {
if (str=="") {
document.getElementById("content").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status == 200){
//alert(xmlhttp.response);
arr = JSON.parse(xmlhttp.response);
for (var index = 0; index < arr.length; var parts = arr[i].split(',');
var url = parts[0].trim();
var tag = parts[1].trim();
alert("h"); index++){
}
buildImage(url);
}
}
xmlhttp.open("GET","http://xxxx/~xx/xxxx/content.php");
xmlhttp.responseType = "json";
xmlhttp.send();
function buildImage(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
}
}
Current Code:
var arr = [];
var index = 0;
function importJson(str) {
if (str=="") {
document.getElementById("content").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status == 200){
// alert(xmlhttp.response);
arr = JSON.parse(xmlhttp.response);
for (var index = 0; index < arr.length; index++) {
var url = arr[index][0];
var tag = arr[index][1];
buildImage1(url);
}
//buildImage(arr[0]);
}
}
xmlhttp.open("GET","http://cs1.ucc.ie/~mmg6/swipeApp/content.php");
xmlhttp.responseType = "json";
xmlhttp.send();
function buildImage1(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
}
}
You're returning a 2-dimensional array from your AJAX. So just access the appropriate index of each element of the main array.
for (var index = 0; index < arr.length; index++) {
var url = arr[index][0];
var tag = arr[index][1];
buildImage(url);
}
Then buildImage should take two arguments:
function buildImage(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
}
DEMO
here is the webservice url which gets results how to hit that url which gives a result true or false
function uniqueness(){
var xhr;
//var contextpath=document.getElementById("contextpath").value.trim();
var pub_name=document.getElementById('p_name').value.toLowerCase().trim();
var pub_trimed_name=(pub_name).replace(".", "").replace(" ", "").replace("&", "")
.replace("(", "").replace(")", "").replace("-", "");
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xhr=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
var url="http://125.63.88.114:8086/xchangeWebservice/resources/publisher/check_publisher/"+pub_trimed_name;
// alert(url);
xhr.open( 'POST',url, true );
xhr.send(null);
//xhr.add("Content-Type", "application/json");
xhr.onreadystatechange = function (response) {
if (xhr.readyState==4 && xhr.status==200)
{
var det = eval( "(" + xmlhttp.responseText + ")");
var size= det.size();
if (det[0].book_title != "" ) {
alert("invalid ");
}
}
};
}
here is the webservice url which gets results how to hit that url which gives a result true or false
Do anyone know what I'm doing wrong or why AJAX callbacks are TOO slow? Here's code:
function new_xmlhttp() {
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function ajax_get(data, args) {
xmlhttp = new_xmlhttp();
xmlhttp.open("GET", "functions.php?" + data, true);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// OK.
alert(args);
}
}
xmlhttp.send(null);
}
Sometimes it takes 2-3 seconds to load (data is max 10 bytes long.)
Tested on Firefox and Chrome under Linux.