how to work with multiple ajax? - php

i am using ajax to get data in Date Label, bt it is not working. Details() function is working , but showDate() is stucked. How i get the data in dt id??
<label><b>From</b></label>
<select id="f" onchange="Details();"><?php echo $coptions; ?></select>
<label><b>Destination</b></label>
<select id="d" onchange="Details();"><?php echo $coptions; ?></select>
<label><b>Flight Name</b></label>
<select id="list" onchange="showDate();" ></select>
<label><b>Date</b></label>
<select id="dt" ></select>
<button >Go</button>
here is my ajax functions:
function Details()
{
var xmlhttp;
var fname = document.getElementById("f").value;
var dname = document.getElementById("d").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 (xmlhttp.readyState==4)
{
document.getElementById("list").innerHTML=xmlhttp.responseText;
showDate();
}
}
xmlhttp.open("GET","route.php?q="+fname+"&w="+dname,true);
xmlhttp.send();
//loadDetails();
}
function showDate()
{
var xmlhttp;
var ex=document.getElementById("list");
var id=ex[ex.selectedIndex].id;
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("dt").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","schdl2.php?f="+id,true);
xmlhttp.send();
//loadDetails();
}
my schdl2.php page is like this:
<?php
session_start();
require_once("dbconnect.php");
$fl=$_REQUEST['f'];
$sql= "SELECT date FROM schedule where flight='$f1'";
$rslt = mysql_query($sql);
$foptions="";
while ($row=mysql_fetch_array($rslt))
{
$rd=$row["date"];
$foptions.="<option>$rd<option>";
}
echo $foptions;
?>
is there is any efficient way??

I made some tests, and your id var is always empty; so i change your code like this:
This:
var id=ex[ex.selectedIndex].id;
By this on the showDate function:
var id = document.getElementById("list").value;
Jsfiddle here: http://jsfiddle.net/x97jn/

You can work with JQuery ajax.
$( document ).ready(function() {
$.ajax({
url: "ajax-1.php",
context: document.body
}).done(function(data) {
$('#result-1').html(data);
});
$.ajax({
url: "ajax-2.php",
context: document.body
}).done(function(data) {
$('#result-2').html(data);
})
});

Related

Insert data from API to mysql

Here I have a code that show restaurants beetween two cities, but there is a problem how to loop objects to insert in database...
http://jsbin.com/AlEVaCa/5
How here I can add my function loadXMLdoc();
function findPlaces(boxes,searchIndex) {
var request = {
bounds: boxes[searchIndex],
types: ["restaurant"]
};
// alert(request.bounds);
service.radarSearch(request, function (results, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert("Request["+searchIndex+"] failed: "+status);
return;
}
// alert(results.length);
document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
for (var i = 0, result; result = results[i]; i++) {
var marker = createMarker(result);
}
searchIndex++;
if (searchIndex < boxes.length)
findPlaces(boxes,searchIndex);
});
}
LoadXMLdoc() :
function loadXMLDoc()
{
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
data = "name="+place.name+"&place="+place.website;
xmlhttp.open("POST","file_to_store.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data);
}
});
so How to integrate my function loadXMLdoc for script to insert data to database?

JS events. Only one of the event handler function is being called?

Hi everybody!
I have a problem and I have no idea how to fix it!
I have a simple HTML page with a button:
<input type = "submit" value = "ok" onclick="f1()"/>
I also have a script on this page:
<script>
function f2()
{
var firstBtn = document.createElement("button");
firstBtn.appendChild(document.createTextNode("firstBtn"));
document.body.appendChild(firstBtn);
var xmlhttp = CreateRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
document.body.innerHTML += response;
}
}
firstBtn.onclick = function()
{
alert("inside f2 onclick");
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
}
function f3()
{
var secondBtn = document.createElement("button");
secondBtn.appendChild(document.createTextNode("secondBtn"));
document.body.appendChild(secondBtn);
var xmlhttp = CreateRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
document.body.innerHTML += response;
}
}
secondBtn.onclick = function()
{
alert("inside f3 onclick");
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
}
function f1()
{
f2();
f3();
}
function CreateRequest()
{
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();// code for IE7+, Firefox, Chrome, Opera, Safari
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");// code for IE6, IE5
return xmlhttp;
}
</script>
So now, when I click one of the buttons (firstBtn or secondBtn), another one is not responding to the click event! Does anybody know why?
As #vishwanath said (see comments to this question), when I doing
document.body.innerHTML += response;
I'm destroying and recreating all the content of my page therefore I'm losing my events.

can not send data to php by ajax

Hi all I am new in ajax and I am trying to get data from php code this is my ajax code:
function blodvotingview(contentid)
{
var xmlhttp;
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
var url="index.php";
url=url+"?hp=1";
url=url+"&m=blogenvoting";
url=url+"&contentid="+contentid;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
And this is my html code {entry_id} is numeric parameter:
view
<p>Suggestions: <span id="txtHint"></span></p>
And this is php code I want to echo:
<?php
showcomment()
function showcomment()
{
echo "yes";
}
?>
But it doesn't work, please help me.
If you consider use jQuery and jquery.serialize plugin you can easy do this by this example:
$.post('URL', $('#form_id').serialize(), function(r) {
console.log(r);
});
or
$.post('URL', $('#form_id').serialize(), function(r) {
console.log(r);
},'json'); // to parse response as JSON
or
$.get('URL', function(r) {
console.log(r);
},'json'); // to parse response as JSON
Try this
function blodvotingview(contentid)
{
var xmlhttp;
if (str==""){
document.getElementById("txtHint").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){
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","index.php?hp=1q="+str,true);
xmlhttp.send();
}
Should work fine,
Wezy

HTTPRequest not working

I have the following code but my <a onclick="meldaan()">Aanmelden</a> isn't working. The function doesn't get called.
This is the function:
<script type="text/javascript">
function meldaan()
{
var voornaam = document.getElementById('voornaam').value;
var achternaam = document.getElementById('achternaam').value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').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 (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("results").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","aanmelden.php?naam=" + encodeURIComponent(voornaam) + "&achternaam=" + encodeURIComponent(achternaam) + "&email=" + encodeURIComponent(email) + "&password="
+ encodeURIComponent(password),true);
xmlhttp.send();
}
</script>
I have the function placed in my header.php file which is included in the footer.php (the place where <a onclick="meldaan()">Aanmelden</a> is located.
What am I doing wrong?
The results are properly deliverd to my page because I tried echoing something and it did show up, so it must be in this code.
Thanks for your time!
You missed a semicolon:
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("results").innerHTML=xmlhttp.responseText;
}
};
^------ HERE

2 AJAX functions on one page - Follow/Unfollow button with PHP - not working

I want to have a Follow/Unfollow button on a page.
The user clicks Follow and this passes the parameter 'artist' to the PHP script, updates a db and the button then says Unfollow.
When the user clicks Unfollow, this passes the parameter 'artist' to the same PHP script and this then updates a db via PHP and then button then says Follow.
I can get the Follow to change to Unfollow but cannot get Unfollow to change to Follow.
My code is as follows:
index.php
<script type="text/javascript">
function followArtist(str)
{
if (str=="")
{
document.getElementById("artist").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)
{
document.getElementById("artist").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=y",true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function unfollowArtist(str)
{
if (str=="")
{
document.getElementById("artist").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)
{
document.getElementById("artist").innerHTML=XMLHTTPlhttp.responseText;
}
}
xmlhttp.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=n",true);
xmlhttp.send();
}
</script>
<!--Follow button for user to click-->
<div class="follow_text" id="artist">
<h1>Follow artist</h1>
</div>
follow.php
<?php
if(isset($_GET['follow']))
{
$follow = $_GET['follow'];
if($follow=='y')
{
$artist = $_GET['artist'];
#############do a database action
?>
<h1>Unfollow artist</h1>
<?php
}
else
{
$artist = $_GET['artist'];
#############do a database action
?>
<h1>Follow artist</h1>
<?php
}
}
?>
Any ideas why this is not working?
You can see a live version of the script at http://soundshelter.net/release.php?id=421928
Thanks in advance!
I have changed the second xmlhttp object which throws error.
<script type="text/javascript">
function unfollowArtist(str)
{
if (str=="")
{
document.getElementById("artist").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.getElementById("artist").innerHTML=xmlhttp2.responseText;
}
}
xmlhttp2.open("GET","/follow.php?artist=<?php echo $artist; ?>"+str+"&follow=n",true);
xmlhttp2.send();
}
</script>
<!--Follow button for user to click-->
<div class="follow_text" id="artist">
<h1>Follow artist</h1>
</div>
Your second
<h1>Unfollow artist</h1>
shoud be Follow artist

Categories