So, I would like to know how to parse an xml document with JavaScript.
I've got it working in php (view below)
<?php
$xmlGmailFeed = file_get_contents("https://gmail_username:gmail_password#mail.google.com/mail/feed/atom/");
$unreadMessages = $xmlGmailFeed->fullcount;
echo $unreadMessages;
?>
But the only thing with this, is that it's running using the server's IP address and doesnt let users login to their gmail. It pops up saying that there was a suspicious login attempt on their account. So what Id like to know is how I would be able to do the same thing, but run it from the hosts computer. I was thinking javascript to do it? But please let me know if theres a better way!
Edit:
Heres the code that im using..
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
$.ajax({
type: "GET",
url: "https://mail.google.com/mail/feed/atom.",
dataType: "xml",
crossDomain: true,
success: parseXml
});
function parseXml(xml)
{
var results = $(xml).find("fullcount").text();
alert(results);
}
</script>
Here's a screen shot of my problem:
http://cbog.co.nr/SC.php?img=Admin/AE0BCBAA4F532BC69A932E5DDD8F14F2
and you can see for yourself, live at:
http://cbogausch.com/test.php
I believe you can just use a DOMParser. jQuery also offers .parseXML() if you're willing to use it.
Related
I'm trying to call a function with a link in html. I found the following example:
click to run function!
if(isset($_POST['runfunction'])){
}
This works perfectly fine, the problem is that when I click the link, "?runfunction" keeps standing in my url bar. So when I submit a form on my page it goes totally wrong (it's way to long to upload here). I do some SQL queries and I'm getting weird values in my SQL database. When I type in just my normal url it works fine. So I'm pretty sure that's the problem. I found another example with ajax :
$("a").click(function(){
jQuery.ajax({
url: "path/to/controller",
type: "POST",
dataType: 'json',
data: {'mentod':'ExportExcel'},
success: successCallback,
error:failureCallback
});
});
I don't fully understand this example (because I never use AJAX) but my php script is included in the html page "include("")". So I can't type in url because it has to be the same page. Can someone give a little bit of info about this, or give an example of how I can fix this? Thanks in advance!
You can add a callback method then remove it from the url by javascript
function successCallback () {
url = window.location.href;
window.location = url.replace("runfunction", "");
}
I'm completely baffled with this. I've tried seemingly everything, and nothing has worked so far!
Basically, I have a PHP script which does some stuff and will feed out a number. Now, this PHP file is being hosted on my server and I need to extract that number from the PHP file through a HTML script which I can put anywhere.
This script works fine when both the PHP file, and the HTML file (which contains this script) are in the same directory:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.get("index.php",function(data){
document.write(data);
});
});
</script>
However, as the PHP script will be uploaded on a server, I need to be able to get the number which is displayed on the PHP script from wherever I put this code. So basically, I need to be able to use this code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.get("http://www.myserver.com/scripts/index.php",function(data){
document.write(data);
});
});
</script>
But I am really unsure as to why it is not working when it isn't in the same directory. I'm not looking to overcomplicate this, and just need a small snippet of code that I can use to collect the number which is fed from that PHP script. Please note that I cannot put these files in the same directory as I'm going to be using this code to feed the number onto a site-editor made website.
Thanks!
Your requests are getting blocked because of the Same-origin policy, your only choice is to use the same domain or enable cors.
So as long as your page AND php file are both placed under www.myserver.com, it will work.
JSONP Examples can be found here and here
$.ajax({
url: "http://www.myserver.com/php/file.php",
jsonp: "callback",
dataType: "jsonp",
success: function( response ) {
console.log( response ); // server response
}
});
I wonder whether someone can help me please.
I've put together this page which allows users to view a gallery of their uploaded images.
I'm now working on the deletion functionality, so that users can delete any image. From some really useful guidance from members of this site here, I've been looking at the jQuery POST method, unlinking the images from the server where they're stored.
From their guidance and subsequent research I've been doing, I think I can use the relative path i.e. $path.
I've come up with the following which I've tried to add to my script:
<script type="text/javascript">
Galleria.ready(function() {
this.$('thumblink').click();
$(".galleria-image").append(
"<span class='btn-delete ui-icon ui-icon-trash'></span>");
$(".btn-delete").live("click", function()
{ var img = $(this).closest(".galleria-image").find("img");
alert('Deleting image... ');
$(this).closest(".galleria-image"). fadeOut('slow');
$.ajax({
type: "POST",
url: "$path",
}
});
return false;
});
});
Unfortunately though, I've clearly done soemthing wrong because this has affected my gallery functionality and it no longer works. Perhaps, because I'm quite new to this, I've looked at it too simplistically, I'm not sure.
I just wondered whether someone could perhaps have a look at this and let me know where I've gone wrong.
Many thanks and kind regards
Your brackets don't match after the
$.ajax({
type: "POST",
url: "$path",
}
which is probably why the javascript is broken.
HI im trying to send a jquery post/ajax to send some variables to a php file.
<script src="/jquery.js"></script>
<script type="text/javascript">
var music ='';
var genre ='';
var language ='';
var nasheedid = "<? echo $postid; ?>";
var session = "<? echo $_SESSION['SESS_TMP']; ?>";
$(function(){
$('.submit').click(function(){
var postdata = {pmusic: music, pgenre: genre, plang: language, psession: session};
$.ajax({
type: 'post',
cache: false,
url: 'addmusic.php',
data: postdata,
success: function (msg) {
alert(msg);
},
dataType: "text",
});
});
});
now regardless of where i post the stuff to, (even if i change the url to url: blahblah.php123 I still get the same results (which is):
The alert displays the entire source code of the file (that the above code is on)
May be you are having an exception on those pages and its redirecting to the same page so you are seeing the same page everytime. Try to debug your server side code and see.
thanks guys i fixed the problem myself. My server was using rewrite so i had to use url: "/addmusic.php" instead of url: "addmusic.php".
So you see the PHP code in the alert? There are a few causes for this.
You don't have a WAMP stack installed. If not, you need to install WampServer or XAMPP.
It's not configured properly. It could be that PHP isn't setup correctly.
Your configuration doesn't support short tags (<?). Try changing them to <?php.
You're not accessing the file through the server properly. Look at the beginning of the URL, make sure it starts with http://localhost/ .
You might also try using jQuery.post() instead.
It's also best to use json_encode() when outputting from PHP to Javascript.
var nasheedid = <?php echo json_encode($postid); ?>;
var session = <?php echo json_encode($_SESSION['SESS_TMP']); ?>;
It will take care of quoting, escaping, etc.
It is me again. I am getting so frustrated with this code it is not even funny. It's not that I am wanting to post it again. It is just that now I understand the where the problem was in the code and wanted to see if you guys can help me figure the last part out.
Basically I am trying to refresh a div without reloading the entire page. It's killing me. Here is some more information on it:
here is my js file first
$(function() {
$(".button").click(function() {
// validate and process form
// first hide any error messages
var email = $("input#email").val();
//var dataString = '&email=' + email; commented out
var dataString = email;
//try insted this //alert (dataString);return false;
$.ajax({ type: "POST", dataType:'HTML',
//or the appropiate type of data you are getting back
url: "http://www.edshaer.com/EdinburgCISD/Gorena/Gorena.php", data: {email:dataString},
//in the php file do $email = $_POST['email'];
//not a good practice but you can try with it and without it
success: function() {
$("#div").fadeOut($("#div").html());
$("#div").fadeIn($("#div").html());
$("#email").val('');
// Change the content of the message element
// Fade the element back in
} });
//ajax ends
return false; });
//click ends
});//document ready ends
Now the problem that I am running into with this code is on the Ajax part. After placing the alert(), I have relized that if I use the function() like this:
success: function(data)
Then the alert came out blank. The reason behind it is that my URL is going to my php file, but my div that I am trying to refresh is on my html file. Meaning if I do this:
success: function(data) {
$("#div").html(data)}
I am sending blank data because it's trying to get the div from my php file instead of my html file.
Now if I do this:
$("#div").html()
Then that gives me the div that is in my html file.
By knowing what is going on now, Can you guys please help me???
My dear you should generate some sort of html in your php file that you want to generate in your div. Then you will see that you are having some content in data in the success function. This is an easy approach.
But there is also some other approach that is more efficient but it needs some sort of search. This is the implementation of Client Side Scripting. You can do this with the help of a jquery plugin jquote2. I hope it will work for you.
You're using
$("#div").fadeOut($("#div").html());
$("#div").fadeIn($("#div").html());
Both are wrong, jQuery .fadeIn() and .fadeOut() arguments are either [duration,] [callback] or [duration,] [easing,] [callback]. None take HTML as input.
Try changing
$("#div").fadeOut($("#div").html());
to
$("#div").fadeOut();
and moving it outside the $.ajax call to hide the previously showed (if any) results before the post and also change
$("#div").fadeIn($("#div").html());
to
$("#div").html(result).fadeIn();
Also change
success: function()
to
success: function(result)
Hope it helps.
This might be a problem relating to the response from the php script. Jquery doesn't always correctly render the Ajax response as html.
Setting dataType: html in the $.ajax({ ... }) call can help. Also setting header("Content-Type: text/html"); at the top of your php ajax script.