SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax and Ampersand Issue

    I'm working on an app that grabs some html off of a page and using Ajax puts the html into a database. The problem I'm having is that when a url in the html contains an ampersand the html is cutoff at that point, like:

    <a title="mysite" href="mysite.com.php?id=1&?day=2">hello</a>

    becomes

    <a title="mysite" href="mysite.com.php?id=1"></a>

    Is there a good way to deal with this? Javascript's escape seems to be one workaround but then I need to unescape using php before the code gets put into the db.

    Also, the amount of html I'm sticking into a db is fairly large(about 2000 lines) and it's actual structure changes outside of my control.

    Here's the javascript Ajax code:
    Code:
    function createXMLHttpRequest(){
      try{ return new ActiveXObject("Msxml2.XMLHTTP");} catch(e){}
      try{ return new ActiveXObject("Microsoft.XMLHTTP");} catch(e){}
      try{ return new XMLHttpRequest();} catch(e){}
      alert("ajax not supported");
      return null;
    }
    
    
    function sendBody(){
      var bod = document.getElementsByTagName("body")[0];
      var innerBod = bod.innerHTML;
      var params = "bod=" + innerBod;
      var xhr=createXMLHttpRequest();
      xhr.onreadystatechange = function(){
        if(xhr.readyState==4){
          if(xhr.status==200){
            bod.innerHTML = xhr.responseText;
          }else{
            alert("error");
          }
        }
      }
      xhr.open("POST", "process.php", true);
      xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xhr.setRequestHeader("Content-length", params.length);
      xhr.setRequestHeader("Connection", "close");
      xhr.send(params);
    }
    Thanks.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Probably that second ? which shouldn't be there.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Typing mistake, there is no second ? in the code.
    <a title="mysite" href="mysite.com.php?id=1&day=2">hello</a>
    Thanks.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Try converting the & into &amp;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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