SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax data to mysql problem

    Hello
    I have a problem with character encoding while saving data from php/ajax to mysql. I'm using the prototype framework

    I set up a test application like this:

    PHP Code:
    <?php
    header
    ('Content-Type: text/html; charset=UTF-8'); 
    if(
    $_GET['do']){
     
     
    // insert value in database
     
    $conn mysql_connect("localhost""root""");
     
    $db mysql_select_db("test");
     
    $q mysql_query("INSERT INTO testtabel (value) VALUES ('".$_GET['print']."')");
     
     
    // print value
     
    echo $_GET['print'];
     exit;
    }
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="js/prototype.js"></script>
    </head>
    <body>
    <div id="result" style="border: 1px solid red;padding:5px">&nbsp;</div>
    <input type="text" id="txt"> <input type="button" value="Test" onclick="do_it()">
    <script type="text/javascript">
    <!--
    function do_it(){
     new Ajax.Updater('result', 'ajaxtest.php?do=nu',   {
     parameters: {print: $('txt').value},
     method: 'get'});
    }
    //-->
    </script>
    </body>
    </html>
    The returned Ajax value displays properly in any browser (ss1.gif), but when I look up the saved value in my database, I only see strange characters (ss2.gif).
    The field in my database is value varchar(50) utf8_unicode_ci.

    What to do?
    Attached Images Attached Images

  2. #2
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure the AJAX call is actually being made? Unless you specifically ask the browser not to cache the request, chances are your browser is storing the return data and recalling it immediately. Try inspecting it with firebug, and check out the examples at http://php.net/header of using cache control through the php header() function.

  4. #4
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iBram007 View Post

    PHP Code:
    <?php
    if($_GET['do']){
     
    // insert value in database
     
    $conn mysql_connect("localhost""root""");
     
    $db mysql_select_db("test");
     
    $q mysql_query("INSERT INTO testtabel (value) VALUES ('".$_GET['print']."')");
     
     
    // print value
     
    echo $_GET['print'];
     exit;
    }
    Be careful to escape the value before putting it in your database. If anyone puts a quote in the submission, things can go bad. use:
    mysql_escape_string($_GET['whatever']). This prevents quotes or slashes from interfering with your code.
    Before outputting to a website (when you retrieve the data) you can apply stripslashes($whatever) and then use htmlspecialchars($whatever).

    Sorry, I'm not to sure about what you can do to get the right value to save though.

  5. #5
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use addslashes when GPC is turn on.


Bookmarks

Posting Permissions

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