views:

296

answers:

7

Hi... i make a Jquery function that (for the moment) call a function dinamically and print it with an alert. with firefox, chrome : it works! when i try on IE7 (the first time), it fails. If i reload the page (F5) and retry , it works! o_O

I FINALLY understand why that's happen. In my old website i used the jquery-1.3.2.min.js library. On this i use the jquery-1.4.2.js and in fact it doesnt work. So what's up? A bug in this new version?

cheers

EDIT

actual functions (with Bryan Waters suggestions):

// html page
<a href="#" onClick="pmNew('1');return false">prova</a>    

// javascript page
function pmNew(mexid) {
    var time = new Date;
    $.ajax({
        type: 'POST',
        cache: false,
        url: './asynch/asynchf.php' + '?dummy=' + time.getTime(),
        data: 'mexid='+escape(mexid)+'&id=pmnew',
        success: function(msg) {
            alert(msg);
        }
    });
    return false;
}

// ajax.php
if($_POST['id']=="pmnew") {
    echo "please, i will just print this";
}

Fiddler result : if i use http://localhost/website fiddler doesnt capture the stream. if i use http://ipv4.fiddler/website it capture the stream, but at the ajax request doesnt appair. if i refresh the page, yes it works. mah...i really don't know how resolve this problem...

A: 

I believe you have an error in your SQL code. Is userd supposed to be userid?

Gaby is absolutely right that your SQL code is wide open for injection. Please consider learning PDO, which will reduce the likelihood of SQL injection significantly, particularly when using placeholders. This way you will have query($sql) and execute($sql), rather than the code going directly into your DB.

Josh Smith
uhm no, SQL code is correct (userd is corret); in fact it works with other browser...about SQL injection, yeah i think i can resolve every problem with an easy mysql_escape_string() before update/select/delete;but, for this example, i don't understand how an user can hack that query (session is after an AND, so that's evalutated everytime...)
markzzz
A: 

As a matter of habit you should deal with your request variables early in your script, and sanitize them to death -- then assign the cleaned results to new variables and be strict in only using them throughout the rest of the script. As such you should have alarm bells ringing whenever you have a request variable in or near an sql query.

For example at the very least you should be stripping any html tags out of anything that will get printed back to the page.

That is in addition to escaping the quotes as part of the sql string when inserting into the database.

I'm all for coding things up quickly -- sure, neaten up your code later... but get security of request vars right before doing anything. You can't tack on security later.

Anyway sorry for harping on.... as for your actual problem, have you tried what Gaby suggested: change your html to:

<a class="lblueb" href="#" onclick="return pmNew('<?php echo $value; ?>')"><?php echo $value; ?></a>

And then update your JS function to:

function pmNew(mexid) {
    $.ajax({
        type: 'POST',
        cache: false,
        url: './asynch/asynchf.php',
        data: 'mexid=' + escape(mexid) + '&id=pmnew',
        success: function(msg) {
            $('#pmuser').html('<a class="bmenu" href="./index.php?status=usermain">PANEL (' + msg + ')</a>');
        }
    });
    return false;
}

Also, with IE -- check the obvious. Clear the browser cache/history

Jhong
i tried with your suggestion, but it still doesnt work on IE. If i click on show details, "Error : Access Denied, Code : 0". It works only if i refresh the page... after tried to click on the link.
markzzz
Is ./asynch/asynchf.php the actual url of the script you are calling? what's the url of the calling page? What server platform is this?
Jhong
yes. i'm running on apache 2.2.the file is in /htdocs/website/asynch/asynch.php
markzzz
so the calling page us asynch.php and the back-end page is asynchf.php, and they're both in the asynch folder? In that case, you should change the "url" to "./asynchf.php", or is it a typo?
Jhong
uhm no :) the calling page is into /htdocs/website/users/pmcheck.php. it call the jquery function that call htdocs/website/asynch/asynch.phpis it a bit more clear now?
markzzz
no wait, i wrong!!! the calling page is /htdocs/website/users/pmcheck.php. it call the jquery function that call htdocs/website/asynch/asynchf.php:)
markzzz
so the url should be '../asynch/asynchf.php', not './asynch/asynchf.php'
Jhong
why? anyway, if i put the double dot, it doesnt work, neither on firefox on chrome. with single dot just IE fails... arghh i can't resolve this problem...
markzzz
+1  A: 

ok, I'm not exactly sure what the issue is here but I think you could probably fix this by simply letting jquery handle the click instead of the inline attribute on the tag.

first change your link like this to get rid of the inline event

<a class="lblueb" href="./asynch/asynchf.php?mexid=<?$value?>"><?=value?></a>

then in your javascript in the head of your page add a document.ready event function like this if you don't already have one:

$(function(){

});

then bind a click event to your link inside the ready function using the class and have it pull the mexid from the href attribute, then call your pmNew function like so:

$(".lblueb").click(function(e){

  e.preventDefault();

  //your query string will be in parts[1];
  parts = $(this).attr("href").split("?");
  //your mexid will be in mexid[1]
  mexid = $parts[1].split("="); 

  //call your function with mexid[1] as the parameter
  pmNew(mexid[1]);

});

Your final code should look like this:

<script type="text/javascript">

function pmNew(mexid) {
    $.ajax({
        type: "POST",
        url: "./asynch/asynchf.php",
        data: "mexid="+mexid+"&id=pmnew",
        success: function(msg){
            $("#pmuser").html('<a class="bmenu" href="./index.php?status=usermain">PANEL ('+msg+')</a>');
        }
    });
}

//document.ready function
$(function(){

  $(".lblueb").click(function(e){

    //prefent the default action from occuring   
    e.preventDefault();

    //your query string will be in parts[1];
    parts = $(this).attr("href").split("?");

    //your mexid will be in mexid[1]
    mexid = $parts[1].split("="); 

    //call your function with mexid[1] as the parameter
    pmNew(mexid[1]);

  });

});

</script>
Kelly Copley
i've already try this solution, and no. it doesnt' work :(
markzzz
If you have tried this and it doesn't work then you must be using it improperly. I have used this same technique on many sites with no issues.
Kelly Copley
it works (as usual) with firefox, chrome, etc! only on IE it doesnt works. can you give to me a link of your site/section where i can try it with my IE?
markzzz
You can actually test this on Ben Alman's hashchange plugin example page located here:http://benalman.com/code/projects/jquery-hashchange/examples/hashchange/#test2
Kelly Copley
uhm yeah that works. but i don't think it use any ajax call. in fact, if i use your script on my code and i insert an easy "alert" (without any ajax function) it works on IE too. the real problem is the ajax call...
markzzz
Return `false` click event handler, or else the browser will first fire the event, then follow the URL in the `href` attribute.
asbjornu
A: 

I didn't understood the "fail", but here's another example..

function pmNew(mexid) {
    $.post("./asynch/asynchf.php", {mexid: mexid, id: "pmnew"},
        function(msg) {
            $("#pmuser").html('<a class="bmenu" href="./index.php?status=usermain">PANEL ('+msg+')</a>');
        }
    });
}
grilix
A: 

It appears that this issue is faced by several people. One of them had luck with clean installation of browser:

http://www.geekstogo.com/forum/topic/22695-errorpermission-denied-code0/

Tahir Akhtar
A: 

Check to make sure the content returned to the DOM is valid for the DOCTYPE specified.

I've had a similiar problem with Chrome, FF and Safari all working just fine, but finding the ajax result broken in IE. Check to make sure you don't have any extra divs or spans in the ajax result breaking your markup.

Bob Gregor
how can check it?
markzzz
Use the validate at w3.org. Use ie developer toolbar to look at the target element, if it's empty there is something broken in your markup. This will check static pages: http://validator.w3.org/unicorn/
Bob Gregor
not sure about what you are meaning. anyway im trying the Fiddler tool, as Bryan suggest. i found the error, now i have to understand which kind of error is it :)
markzzz
+1  A: 

Best way to debug is to download Fiddler and see what the HTML traffic is going on and if the browser is even making the ajax request and what the result is 200 or 404 or whatever.

I've had problems with IE cacheing even on posts. And not even sending out the requests. I usually create a date object in javascript and add a dummy timestamp just to make the url unique so it won't be cached.

Bryan Waters
what a awesome plugins :) tnx for the tips. so i tried with chrome and with internet explore. those the results :CHROME : # Result Protocol Host URL Body Caching Content-Type Process Comments Custom 1 200 HTTP localhost /gtw/asynch/asynchf.php 1 no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Expires: Thu, 19 Nov 1981 08:52:00 GMT text/html chrome:3524
markzzz
IE : # Result Protocol Host URL Body Caching Content-Type Process Comments Custom 2 502 HTTP www.download.windowsupdate.com /msdownload/update/v3/static/trustedr/en/authrootstl.cab 512 text/html svchost:1220
markzzz
the ie call is missing completely - your windowsupdate call is a red herring. try tacking a timestamp on your url like var time = new Date;'asyncf.php' + '?dummy=' + time.getTime();that should force the url to be unique and fix the caching issues.
Bryan Waters
ok, edited the function. now it doesnt work neither on chrome. fiddler message : # Result Protocol Host URL Body Caching Content-Type Process Comments Custom 1 404 HTTP localhost /gtw/asynch/asyncf.php?dummy=1282599018754 219 text/html; charset=iso-8859-1 chrome:3012
markzzz
asynchf.php - your missing the h that's why your getting the 404
Bryan Waters
uhm...tried to change it too but nothing! is incredible...all my other website work fine with these method (on all browser). I don't know why this method doesnt work on THIS website (with same html event call, same jquery method and same php page; i've tried to do a copy and paste for the same fucntions, but still doesnt work). what's happened?
markzzz