Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post

    xmlhttp.open() target the same page

    I need some help getting the included ajax script's xmlhttp.open() to target a php code within the same page --something like $_SERVER[PHP_SELF]

    here is the script:
    PHP Code:
    <script type="text/javascript">
    function 
    showResult3(strss)
    {
    if (
    strss.length==0)
      { 
      
    document.getElementById("livesearch3").innerHTML="";
      
    document.getElementById("livesearch3").style.border="0px";
      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==&& xmlhttp.status==200)
        {
        
    document.getElementById("livesearch3").innerHTML=xmlhttp.responseText;
        
    document.getElementById("livesearch3").style.border="1px solid #A5ACB2";
        }
      }

    xmlhttp.open("GET","q="+strss,true);//$_SERVER[PHP_SELF]
    xmlhttp.send();

    }
    </script> 
    I want to pass the variable $q to a php code in the same page as the code above. I know typically the xmlhtpp.open() need a url which tent to be an external one. I'm hoping that there is a work around.

    I appreciate any help!
    Last edited by mbarandao; 02-17-2011 at 07:57 AM.

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    The window.location object contains all the information you need.

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    not quite clear of window.location implementation in the xmlhttp.open() based on some docs i have read so far.

    If the page in which my ajax function showResult(str) is to send the $q variable to the php code is "client_info.php", then how do I implement the window.location object to
    PHP Code:
    xmlhttp.open("GET","?q="+str,true);
    xmlhttp.send(); 

  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Here's some documentation: https://developer.mozilla.org/en/DOM/window.location

    Since you want the current URL without the querystring (because you add that manually), the "pathname" property is probably what you're looking for.

    So, something like this:
    PHP Code:
    xmlhttp.open("GET"window.location.pathname "?q=" strtrue); 

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    No success in passing $q within the same page. I modified like this

    PHP Code:
    <script type="text/javascript">
    function 
    showResult3(strss)
    {
    if (
    strss.length==0)
      { 
      
    document.getElementById("livesearch3").innerHTML="";
     
    // document.getElementById("livesearch3").style.border="0px";
      
    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==&& xmlhttp.status==200)
        {
        
    document.getElementById("livesearch3").innerHTML=xmlhttp.responseText;
        
    //document.getElementById("livesearch3").style.border="1px solid #A5ACB2";
        
    }
      }
    //xmlhttp.open("GET", window.location./i-cmsys/ + "?q=" + strss, true);//the actual path, also with no success
    xmlhttp.open("GET"window.location.pathname "?q=" strsstrue);
    xmlhttp.send();

    }
    </script> 

  • #6
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Well, debug the thing!

    What's
    Code:
    window.location.pathname + "?q=" + strss
    ?

    Alert it and have a look!

    What is it supposed to be, and why is it different?

    Those are things you have to be able to ask (and answer) yourself, if you want to be able to get something working without the help of others (whose job is considerably more difficult than yours, because they don't see all of your code).

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Well, I have been debugging and still at it. Just posted the code for another set of eyes for something I maybe missing.

    PHP Code:
    //xmlhttp.open("GET", window.location.pathname + "?q=" + strss, true); 
    I meant to comment this line out when I posted --I used it inside the code for guidance purpose only. It is commented out in the actual code.

    Since there are no errors being reported, it is a bit more of a challenge.

  • #8
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I don't know what you mean by "debugging", but I mean exactly what I said before:

    Alert the thing, tell us what it is, and tell us what it's really supposed to be. There's no way to help you otherwise.

    Also, if that line is commented out in your actual code, that means that you don't have any xmlhttp.open() command in your code at all, so it's no wonder it does nothing.

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Code:
    xmlhttp.open("GET", window.location./i-cmsys/client_information.php + "?q=" + strss, true);
    when I alert the page it shows the correct path as in above

  • #10
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by mbarandao View Post
    Code:
    xmlhttp.open("GET", window.location./i-cmsys/client_information.php + "?q=" + strss, true);
    when I alert the page it shows the correct path as in above
    Well this is certainly not a correct path. It isn't even a string, it's just a sytax error.

    You're a bit hard to help, because you don't really tell us the relevant stuff.

    1.) If, instead of the window.location.pathname thing, you put in the real URL, does it work? I suppose so — if not, that's a completely different problem. Please post that line with the real URL in it.

    2.) Assuming, it works, with the real URL put in, change it to the window.location.pathname one. If that now doesn't work, see if it evaluates to anything different than the real URL. Please post what it evaluates to.

  • #11
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    My apology, I was of the notion that I was answering your questions fully --but my mistake.

    I'll follow you last advice and note back shortly!

    Thanks for the continued help.

  • #12
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,388
    Thanks
    11
    Thanked 565 Times in 558 Posts
    Again ... most certainly mbarandao seems to be consumed by the common misunderstanding that you can indeed influence the already executed PHP from Javascript. I conclude this from his question ... "I want to pass the variable $q to a php code in the same page as the code above".

    Passing "?q=" from Javascript to the PHP that originally created the current page will never end up on the same page ... it will be on a new page that has been created using the same PHP file. You can indeed process the result of the newly created output from the PHP using Javascript and assemble it into the current page though

    But we will see ...
    Last edited by devnull69; 02-18-2011 at 07:54 AM.

  • #13
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by devnull69 View Post
    Again ... most certainly mbarandao seems to be consumed by the common misunderstanding that you can indeed influence the already executed PHP from Javascript. I conclude this from his question ... "I want to pass the variable $q to a php code in the same page as the code above".
    Well, the AJAX callback is all set up to add the response to the DOM, so I don't think that that's the problem.

  • #14
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    hello,

    it appears that using the following:
    PHP Code:
    var url window.location.pathname '?q=' strss;
    alert(url);//to inspect the url string.
    xmlhttp.open("GET"urltrue);
    xmlhttp.send(); 
    puts me in the right direction for this task of mine. However, I'm still facing some difficulties.

    There is definitely progress with this last solution. However, when the alert appears --displaying the correct path and the correct $q variable, after click the ok button, a duplicate of the same page loads at the bottom. I have echo'ed the $q inside the php code, it only appears in the duplicate page --this obviously is not something I want.

    Nothing happens when I comment out the alert(url);

    any ideas?
    Last edited by mbarandao; 02-18-2011 at 03:16 PM.

  • #15
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You, sir, are really difficult to help. Just like I said before:

    1.) If, instead of the window.location.pathname thing, you put in the real URL, does it work?

    I suppose it doesn't, because you say alerting that generated URL shows that it's correct. If that's really the case, this is a completely different problem, because your server doesn't seem to return what you expect it to return.

    Btw, if you don't have your server script set up to not build a whole page if that "q" parameter is set, it's obvious, too, that the whole page will be loaded and put into the current page.

    Also "Nothing happens when I comment out the alert(url);" sounds like you're doing something wrong. If that's really the case, that means that you have a race condition in there and probably call showResult3 before the page is fully loaded. Since you never cared to share with us, how and when you call that function, there's no way to know.

    Anyway, you seem to be in way over your head with a rather general problem, yet you're only talking about a very specific one, which should have been solved by my first post.

    Either you post a link to your site and start to really explain what you want (generally, and in plain english), or, I'm afraid, you won't get any advice that really helps you get this working.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •