ajax request do not response - javascript

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

Related

Pass Array From Select Into xmlhttp

I want to pass an array of values from my select into the xmlhttp.open request below so I can parse the variables in the new page. How can this be achieved? I attempted the syntax below, but it throws a 500 error when the button is pressed.
<script type="text/javascript">
function GetData() {
var xmlhttp = new XMLHttpRequest();
var hiredate = document.getElementById("hiredate").value;
var termdate = document.getElementById("termdate").value
var arr = document.getElementById("bq").value;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
document.getElementById("data").innerHTML = xmlhttp.responseText;
}
else if (xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", "Query.php?hiredate="+hiredate+"&termdate="+termdate+"&array="+arr, true);
xmlhttp.send();
}
</script>
<select name="bq" size="12" multiple="multiple" tabindex="1">
<option value="HireDate">HireDate</option>
<option value="TermDate">TermDate</option>
<option value="Dept">Dept</option>
<option value="Manager">Manager</option></select>
Start Date: <input type="date" name="hiredate" id="hiredate">
End Date: <input type="date" name="termdate" id="termdate">
<input type="submit" value="Submit" id="ajaxButton" onclick="GetData()">
<div id="data">
EDIT
This is my Query.php - I omitted the actual connection to the server as I know that is sound
<?php
$hiredate = $_GET['hiredate'];
$termdate = $_GET['termdate'];
$sql = '';
$selected_fields = array();
foreach ($_GET['array'] as $selectedOption){
$selected_fields[] = $selectedOption;
}
if(!empty($selected_fields)){
$fields = implode(',', $selected_fields);
$sql = 'SELECT '.$fields.' from testtable';
}
$sql = $sql & "WHERE hiredate = $hiredate AND termdate = $termdate";
echo $sql
?>

How do I play a media file that I downloaded using Ajax?

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

how to hit a rest webservice through ajax in java

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

onreadystatechange is too slow

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.

problem in javascript

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>

Categories

Resources