开发者

How to dismiss the <meta> tag?

I want to show in an alert a french sentence containing accentuated letters such as "é" and "à". So I use the utf-8 charset in the meta tag of my php file so that the letters "é" and "à" are displayed normally in a javascript alert. The problem is that when getting the ajax responseText then I get among the responses the code of the meta tag. So how to get only the echo result of the php file ?

Here is the php file used by the ajax :

<开发者_如何学Pythonhead>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php   
define("ROOT_PATH", "../../");
include ROOT_PATH . 'config.ajax.php';
include ROOT_PATH . 'config.inc.php';
require_once RP_MODELS.'produit.class.php';

$prod_code = $_GET['prod_code'];

$db =& new DbConn() ;   
$produit =& new produit($db->getInstance()) ;
$tab = $produit->lire($prod_code) ;

if ( $tab['cnt'] > 0)
    $rep = "Le code produit " . $prod_code . " existe déjà !" ;
else 
    $rep = "0" ;

echo $rep;

?>

So I want only the $rep variable to be returned by the ajax responseText. How to achieve that ?


<?php   
header('Content-Type: text/html; charset=utf-8'); 
define("ROOT_PATH", "../../");
include ROOT_PATH . 'config.ajax.php';
include ROOT_PATH . 'config.inc.php';
require_once RP_MODELS.'produit.class.php';

$prod_code = $_GET['prod_code'];

$db =& new DbConn() ;   
$produit =& new produit($db->getInstance()) ;
$tab = $produit->lire($prod_code) ;

if ( $tab['cnt'] > 0)
    $rep = "Le code produit " . $prod_code . " existe déjà !" ;
else 
    $rep = "0" ;

echo $rep;    

?>

Read:

http://php.net/manual/en/function.header.php


You don't need to have the meta tag on this script. You only need it on pages that the browser is going to have to render.


Remove the html code including meta tag from this file and use

echo utf8_encode($rep);


You need to check if the request is an Ajax request, try something like this:

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
    // This is an ajax request
    echo $rep;
    die();
}


You can either add a variable to the url by which you can recognize the AJAX request, or you can set a special X-Requested-With header (which I think it automatically set by JQuery). In PHP you can check if this header is set.

Also, don't forget to set the Content-Type header, in which you should also specify UTF-8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜