1

my php function

<?php
    function handleException($ex)
      {
        header('HTTP/1.1 500 Internal Server Error');
        echo 'Internal error';
     }

    set_exception_handler('handleException');

    // we are using PDO -  easier to use as mysqli and much better than the mysql extension (which will be removed in the next versions of PHP)
     try 
     {
        $password = 'root';
        $db = new PDO('mysql:host=localhost;dbname=finance', "root", $password);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    mysql_set_charset('utf8');
        // note the quote thing - this prevents your script from sql injection
        mysql_query('SET NAMES utf8;');
        $data = $db->query("SELECT customer_name FROM customer where customer_id = " . $db->quote($_GET["q"]));
        $customer_name = array();
        foreach ($data as $row) {
           $customer_name[] = $row["customer_name"];


        }

        print json_encode(array(
                "customer_name" => $customer_name,
                "anotherReturnValue" => "just a demo how to return more stuff")
        );

    } catch (PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

    ?>

and my php form file

<?php include('header.php');
include('config.php');
<div>
  <form id="form" method="post" action="functions/adduser.php" enctype="multipart/form-data" >
     <table>
     <tr><td>உ.எண்</td>
     <td>
        <select id="sno" name="sno" >
        <option>உ.எண்</option>
        <?php while($row=mysql_fetch_array($result))
      {?>
           <option value="<?php echo $id=$row['customer_id'];?>" id="sno"><?php echo $id=$row['customer_id'];?></option><?php }?>
        </select>

     <td><div  id="list"></td></tr>
     <tr>
        <td><label>பெயர்</label></td>
        <td>
         <input type="text" value="" name="name" id="" class="text">
        </td>
      </tr>

     <tr>
        <td><label>ஊர்</label></td>
        <td>
         <input type="text" value="" name="city" class="text">
        </td>       
      </tr>
     <tr>
      <tr>
        <td><label>பற்று</label></td>
        <td>
         <input type="text" class="text" name="loan"></textarea>
        </td>
      </tr>
     <tr>
        <td><label>ஆரம்பம் தேதி</label></td>
        <td>
          <input type="text" class="text" name=""  value="<?php echo date('Y-m-d'); ?>" />
        </td>
      </tr>
       <tr>
        <td><label>முடிவு தேதி</label></td>
        <td>
         <input type="text" class="text" name="" value="<?php echo $end_date; ?>" />
        </td>
      </tr>
      <tr><td><input type="button" value="Save" id="save" name="save" class="text"></td></tr> 
      <tr><td><div id="error"></div></div></td></tr>
      </table>
  </form>
</div>

    <script tyep="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" charset="UTF-8">

        $(document).ready(function () {

            // if user chooses an option from the select box...
            $("#sno").change(function () {
                // get selected value from selectbox with id #department_list
                var cusno = $(this).val();
                $.ajax({

                    url: "functions/get_name.php",
                    data: "q=" + cusno,
                    contentType: "application/json; charset=utf-8",
                   dataType: "json",

                    // if successful
                    success: function (response, textStatus, jqXHR) {

                        // no teachers found -> an empty array was returned from the backend
                       if (response.customer_name.length == 0) {
                            $('#result').html("nothing found");
                       }
                       else {
                            // backend returned an array of names
                          var list = $("#list");

                            //remove items from previous searches from the result list
                           $('#list').empty();

                            //append each teachername to the list and wrap in <li>
                                $.each(response.customer_name, function (i, val) {
                                //list.append($("<li>" + val + "</li>"));

                           });
                       }
                        $.each(response.customer_name, function (i, val) {


                        });
                    }});
            });


            // if anywhere in our application happens an ajax error,this function will catch it
            // and show an error message to the user
            $(document).ajaxError(function (e, xhr, settings, exception) {
                $("#error").html("<div class='alert alert-warning'> Uups, an error occurred.</div>");
            });

        });

    </script>
    </script>

<?php include('footer.php');?>
code-jaff
  • 9,230
  • 4
  • 35
  • 56

3 Answers3

1

you should probably missing the valid meta tag in the html template, try adding this

<html lang="en">
    <head>
        <meta charset="utf-8">

and since you use the PDO, why on earth you still use mysql_* extension anymore? you should set the correct charset like this

$db = new PDO('mysql:charset=utf8mb4;host=localhost;dbname=finance', "root", $password);

make sure that your tables have been set to the utf8mb4 (not utf8) collate/charset

for better understanding and to make the application utf8 compliant

  1. utf-8-all-the-way-through

  2. The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Community
  • 1
  • 1
code-jaff
  • 9,230
  • 4
  • 35
  • 56
  • meta tag also there ...here i need to retrieve tamil names from database..other than english names shown as question mark? thanks for reply – user3305719 Jul 29 '14 at 06:12
  • @user3305719 where do those question marks get printed?, you probably missing to set the correct unicode charset encoding somewhere. please look at my edit – code-jaff Jul 29 '14 at 06:20
1

Have you set correct charset in your database?

Or there may be Problem with Connection. Try set Connection to UTF-8

mysql_query("SET NAMES utf8");

In PDO you can directly set charset.

"mysql:host=$host;dbname=$db;charset=utf8"

PHP PDO: charset, set names?

Community
  • 1
  • 1
m1k1o
  • 2,344
  • 16
  • 27
  • thank a lot ....i tried more than a day to find you simply answered me ....thanks 100 times ..thank you very much it works fine – user3305719 Jul 29 '14 at 06:28
1

You can't just use utf-8 encoding, it depends on the character set in your database.

Do SHOW CHARACTER SET FOR mydatabase; first, then also SHOW CHARACTER SET FOR mydatabase.mytable; for the table you are accessing data from to get the proper character set.

Only then can you know the correct encoding to use which may resolve your issue and set it accordingly.

EternalHour
  • 8,308
  • 6
  • 38
  • 57