Jump to content

preventing PHP page from outputting text (part of Ajax app)


cbassett03

Recommended Posts

Here is a segment of code that I'm using in a PHP script, which is called as part of an AJAX HTTP Request:

 

<?php

   
    $result = $user_conn->execQuery($sql_query);
    
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $resultText .= '<option value="'.$row['id'].'">'.$row['display_text'].'</option><br />'."\n";
    }

    echo $resultText;
?>

 

NOTE: I created a MySQL class with custom functions, so execQuery() is a custom function I created that actually runs the query() command for SQL.

 

Anyway, as you can see, to get the results back to the JavaSCript calling request function, I used ECHO.  Is there an alternative so that if someone where to run just this PHP script directly, it wouldn't output any text (because right now it will)?

 

(I asked this a few weeks ago but seem to have "lost" the post.)

You can check for $_SERVER['HTTP_X_REQUESTED_WITH'] and optionally see if it == xmlhttprequest.

 

Also, you might want to generate a token in PHP, stick it in the session, echo it to JS so it can use it as a var, send this token with each AJAX request and check for it in the PHP script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.