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.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMLHttpResponse return all page html from php

    Hello all
    im trying to invoke js code that submit paramter to php page , but the result
    that the XMLHttpRequest Object captures is all the html of the page + the result from the php , what im doing wrong here ?
    here is my code :

    Code:
    var rqo;
    function setReq(v) {
    
       if (v != "") {
            document.getElementById("result").innerHTML = "Fetching " + v;
            rqo = new XMLHttpRequest();
            var qs = encodeURIComponent(v);
            var qry = "index.php?_data_input=" + qs;
            rqo.open('GET',qry,true);
            rqo.onreadystatechange = handleHttpResponse;
            rqo.send(null);
            return false;
        }
    }
    function handleHttpResponse() {
      var results = "";
      qq (rqo.readyState == 4) {
        results = rqo.responseText;
        document.getElementById("result").innerHTML = results;
      }
    
    }
    .....
    ....
     <div class="c3" id="result">
               &nbsp;
    </div>
    ....
      <form method="get" name="_main_form" >
                   <input type="text"size="50" name="_data_input" id="_data_input">
                   <input type="button" value="Get Item" onclick="aboutlang(document._main_form._data_input.value)">
                </form>
    .....

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    It'd be nice to see your php api, but I'm going to guess that you're printing out entire html page (and so that's what you should expect to receive in return).

    Post your index.php code.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    if(isset($_GET["_data_input"]))
    {

        
    $user_text_input trim($_GET['_data_input']);
        print 
    "<br> this is mt response "<br>

    }
    else
    {
        print 
    "_data_input is empty";
    }
    ?>
    this is all of the php file i mean the php section

  • #4
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    So the code you just posted is the only code in index.php?


    If so then your responseText should include either

    "<br> this is mt response"

    or
    "_data_input is empty"
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes this is all my code and i dont understand why the response returns me
    again all the php page ( html and all the stuff )
    here is the full php page :

    PHP Code:
    if(isset($_GET["_data_input"]))
    {
    print  
    "<br> dddddddd </br>";
    }
    else
    {
        print 
    "_data_input is empty";
    }
    ?>
    <html>
     <style type="text/css">
    <!--
       .c1 {
          clear:both;text-align:center;
        }
        .c2 {
          float:left;width:25%;display:table-cell;vertical-align: middle;
        }
        .c3 {
          float:left;width:74%;border-left:1px solid #000000;display:table-cell;vertical-align: middle;
        }
        .c4 {
          border-top:1px solid #000000;clear:both;
        }
        .c5 {
          border:1px solid #000000;
        }
        #logo{padding-top:15px;}
    -->
        </style>

    <head>
    <script type=text/javascript language=javascript>
    <!-- Hide Javascript on old browsers
    var rqo;
    function aboutlang(v) {
       if (v != "") {
            document.getElementById("result").innerHTML = "Fetching " + v;
            rqo = new XMLHttpRequest();
            var qs = encodeURIComponent(v);
            var qry = "index.php?_data_input=" + qs;
            rqo.open('GET',qry,true);
            rqo.onreadystatechange = handleHttpResponse;
            rqo.send(null);
            return false;
        }
    }
    function handleHttpResponse() {
      var results = "";
      if (rqo.readyState == 4) {
        results = rqo.responseText;
        document.getElementById("result").innerHTML = results;
      }

    }
    -->
    </script>
    </head>
    <!-- Specific adjustments for IE -->
    <!--[if IE]>
    <style type="text/css">
    #logo{padding-top:5px;}
    </style>
    <![endif]-->
      </head>
      <body>
        <div class="c5">
          <div class="c1">
             <div class="c2"></div>
            <div class="c3">
             <h1>
                <form method="get" name="_main_form" >
                   <input type="text"size="50" name="_data_input" id="_data_input">
                   <input type="button" value="Get Item" onclick="aboutlang(document._main_form._data_input.value)">
                </form>
             </h1>
            </div>
          </div>
          <div class="c4">
            <div class="c2">&nbsp;</div>
            <div class="c3" id="result">
               &nbsp;

           </div>
          </div>
          <div class="c1"><!-- --></div>
        </div>

      </body>
    </html> 
    im running under apache 2.2 and php5 winxp

  • #6
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Ok, the responseText is supposed to return the entire requested page's output.

    You should place that php code in a separate file (it's more modular anyway).
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks it is right and it is working , now i have another problem
    and i don't what to open thread , im reading the manual and seeing the examples
    but i still cant find why this regexp doesn't work

    PHP Code:
    $sMatchResult '/dddd/';
    preg_replace('/\//','',$sMatchResult); 
    the $sMatchResult return still the string with the "/" why ? i like to clean them from the string .

  • #8
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Shouldn't it be

    preg_replace('/\//g','',$sMatchResult);

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #9
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Aye you need the global parameter to catch all the slashes.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #10
    Regular Coder
    Join Date
    Jan 2008
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no it is still not working ....
    :/


  •  

    Posting Permissions

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