utf-8 problem with ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hesameagle
    New Member
    • Dec 2007
    • 1

    utf-8 problem with ajax

    Hi

    I want to use ajax to connect to database and echo some data in my page, I wrote these two codes that can handle my need:

    index.php
    -----------------------------------------
    [PHP]<?php

    //This file is text.php

    mysql_connect(" localhost", "root", ""); //Connect to the mysql server with your host (most likely localhost), username, and password
    mysql_select_db ("dictionary "); //Select your database by name

    $page = $_GET["page"]; //This is the variable we retrieve through GET to know which row of content to retrieve

    $sql = "SELECT * FROM table1 WHERE f1 = '$page'"; //This is the text of the query. We will select the content field from the table ‘pages’ where the page field has the same value as the one we want to retrieve

    $query = mysql_query($sq l) or die(mysql_error ()); //Make the actual query

    if( mysql_num_rows( $query) == 1 ) //Check to see if we found 1 row with that page name
    {
    $r=mysql_fetch_ assoc($query); //Set a mysql fetching variable for the query
    echo $r["f2"]; //Echo out the content of the page we want
    }
    else
    {
    echo "Sorry, that page was not found."; //Otherwise, echo out an error message saying the page was not found
    }

    ?>
    [/PHP]--------------------------------------------
    text.php
    ---------------------------------------------
    [PHP]<?php

    //This file is text.php

    mysql_connect(" localhost", "root", ""); //Connect to the mysql server with your host (most likely localhost), username, and password
    mysql_select_db ("mydatabse" ); //Select your database by name

    $page = $_GET["page"]; //This is the variable we retrieve through GET to know which row of content to retrieve

    $sql = "SELECT * FROM table1 WHERE f1 = '$page'"; //This is the text of the query. We will select the content field from the table ‘pages’ where the page field has the same value as the one we want to retrieve

    $query = mysql_query($sq l) or die(mysql_error ()); //Make the actual query

    if( mysql_num_rows( $query) == 1 ) //Check to see if we found 1 row with that page name
    {
    $r=mysql_fetch_ assoc($query); //Set a mysql fetching variable for the query
    echo $r["f2"]; //Echo out the content of the page we want
    }
    else
    {
    echo "Sorry, that page was not found."; //Otherwise, echo out an error message saying the page was not found
    }

    ?>
    [/PHP]------------------------------------------

    I works good, when my database is normal, but for utf-8 data it gives me ??? instead of the words retrieved from database, I believe there should be a code that I missed that would show the utf-8 data in JAVA script.

    will someone help me, thank/
    Last edited by acoder; Feb 25 '08, 07:57 AM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Either set the header in PHP or with setRequestHeade r("content-type"...) via the XMLHttpRequest object.

    Comment

    Working...