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?
Related
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);
})
});
i'm trying to execute a javascript function every 'n' periods of time ,the function will call a php file which will execute a sort of code,but unfortunately it doesn't work ,here;s the code
<p id="code"></p>
<script>
var myVar=setInterval(function(){showUser()},1000);
function showUser() {
var xmlhttp = null;
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', 'getuser.php');
xmlhttp.send();
document.getElementById("code").innerHTML=t;
}
</script>
//edit this
document.getElementById('txtHint').innerHTML = xmlhttp.responseText;
//to
document.getElementById("code").innerHTML = xmlhttp.responseText;
//and delete
document.getElementById("code").innerHTML=t;
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.
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
This is a very simple function if one where to use an input tag, unfortunately i have to use a link tag since i'm working with alphabet grouping that would retrieve the values on a different page.
here's a code i found for link tag to hold a value to be submitted to ajax
echo '<a onclick="passPagination(\'3\');passLetter(\'S\')">NEXT</a>';
i want to call all letters "s" page "3" from the other page.
function passLetter(str)
{
if (str=="")
{
document.getElementById("retail_group").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("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?letter="+str,true);
xmlhttp.send();
}
function passPagination(str)
{
if (str=="")
{
document.getElementById("retail_group").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("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?pageno="+str,true);
xmlhttp.send();
}
also i want to call again the letter that was passed to the otherpage so that i would make it as a reference for my pagination links that is also being hold in this page.
echo 'NEXT';
i am really at lost here since i am not familiar with link tag and ajax working together, also as you may have noticed i am not also that good with ajax judging from my very long ajax code.
Your help is greatly appreciated, Thank You.
i am really new at ajax. :)
The solution is to make a function with multiple arguments:
function passPaginationAndLetter(page, letter)
{
if (page=="" || letter == "")
{
document.getElementById("retail_group").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("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?letter="+letter+"&pageno="+page,true);
xmlhttp.send();
}
You can now call this with
echo '<a onclick="passPaginationAndLetter(\'3\',\'S\')">NEXT</a>';
The difference is that you are not calling two AJAX methods, just one.
I'm not pretty sure if i got what you mean, but i guess you want to something like that
echo '<a onclick="get(\'S\', '. ($_GET['page'] + 1) .')">NEXT</a>';
function get(letter, page) {
if (!letter) {
document.getElementById("retail_group").innerHTML="";
return;
}
if (!page) {
page = 1;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("retail_group").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","otherpage.php?letter=" + letter + "&pageno=" + page,true);
xmlhttp.send();
}
if it's really that, don't forget to validate the variables got from $_GET before use that, i just didn't because it's not the point...
I think this is what you're looking for:
html
PREVIOUSNEXT
javascript
var current = {
page: 0,
letter: a
};
function ajax(url) {
var xmlhttp = window.XMLHttpRequest || new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function(xmlhttp) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByI("retail_group").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function next() {
ajax("otherpage.php?letter=" + current.letter + "&pageno=" + (++current.page));
}
function previous() {
ajax("otherpage.php?letter=" + current.letter + "&pageno=" + (--current.page));
}
This code is not complete of course, it doesn't check for the max/min values of the pages, and there's no way of replace the letter, but it should point you in the right direction.