tags:

views:

144

answers:

3

I don't know why I am having a hard time with this, but it's about time I came up for air and asked the question.

What is the best way to call a php file with jquery ajax, have it process some inputs via $_GET, return those results and again use ajax to replace the content of a div?

So far, I can call the php file, and in the success of the jquery ajax call I get an alert of the text.

$.ajax({
        url: "xxx/xxx.php", 
        type: "GET",
        data: data,     
        cache: false,
        success: function (html) {
            alert("HTLM = " + html);
            $('#product-list').html(html);
            }
 });

I set the php function to echo the result, the alert spits out the html from the php file. But I get a side effect. I get the code that the php generated echoed at the top of the page. So php is doing it's job (by echoing the content). Next jquery is doing it's job by replacing the div's content with the values from the php file.

How do I stop the php file from echoing the stuff at the top of the page?

Sample code being echoed from php and alerted in the success of jquery ajax

HTML =

<div class="quarter">
  <div class="thumb">
    <a href="productinfo.php?prod-code=Y-32Z&cat=bw&sub=">
      <img src="products/thumbs/no_thumb.gif" alt="No Image" border="0" />
    </a>
  </div>
  <div class="labels"><span class="new-label">New</span></div>
  <p><span class="prod-name">32OZ YARD</span></p>
  <p><span class="prod-code">Y-32Z</span></p>
</div>

Thanks!!!

-Kris

+3  A: 

Are you including your php file that you use for your ajax call at the top of the page? If so you dont need to. That would cause the data to dump at the top.

Ask your question :)

EDIT TO ANSWER QUESTION

<div id="product-list">
<?php include 'products.php' ?>
</div>

Your ajax function will now overwrite the php content when it runs and outputs to #product-list

Luke
If I use jquery ajax to call the php file and get the content, once the jquery is complete, if I view the source, the content isn't in the source view. It's there on the page, but not when you view the source. When I run a spider simulator on it, it doesn't show the content. Is this how jquery ajax works or is there another way to do it?
Kris.Mitchell
No jquery/javascript modifys the page once it is live, spiders do not have javascript enabled so cannot pick up the content. Why do you need jquery to include the content, can you not just use php? If anyone were to view your site without javascript enabled (if you were not including the php file) they would not be able to view your products
Luke
How do I include the file so that I can run the initial content load, but then not include it when I need to echo for the jquery+ajax? I didn't want to do a separate file.
Kris.Mitchell
I have modified my answer, dont include the php file at the top but where you want the contents outputted, i.e div product-list.
Luke
I think I have design issues. ROFL. Luke. Thank You.
Kris.Mitchell
No problem, practice makes perfect right ;)
Luke
A: 

If you mean you want to avoid showing the header and body tags, just the contents, you need to detect when the request is AJAX at PHP side.

Some pseudo-code is:

IF (!IS_AJAX)
    HTML
    HEADER
    BODY
ENDIF

CONTENTS

IF (!IS_AJAX)
    /BODY
    /HTML
ENDIF

Search for HTTP_X_REQUESTED_WITH here at stackoverflow

Ast Derek
HTTP_X_REQUESTED_WITH is not always available in the $_SERVER array
sunwukung
I didn't know that, but I'd add a variable to retrieve AJAx content. So, the search for the given header would be closely related
Ast Derek
A: 
Daniel
No I want all of the response data. I am getting the response echoed at the top of the page, as well as the content being loaded into a div with jquery.
Kris.Mitchell