使用php和ajax显示除英语以外的问号的结果

时间:2014-07-29 06:00:25

标签: php ajax

我的php功能

<?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();
    }

    ?>

  

和我的php表单文件

<?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');?>

3 个答案:

答案 0 :(得分:1)

您可能错过了html模板中的有效元标记,请尝试添加此

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

既然你使用PDO,为什么你还在继续使用mysql_ *扩展? 你应该像这样设置正确的字符集

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

确保您的表格已设置为utf8mb4( not utf8)collat​​e / charset

为了更好地理解并使应用程序符合utf8

  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!)

答案 1 :(得分:1)

您是否在数据库中设置了正确的字符集?

或者可能存在连接问题。尝试将Connection设置为UTF-8

mysql_query("SET NAMES utf8");

在PDO中,您可以直接设置字符集。

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

PHP PDO: charset, set names?

答案 2 :(得分:1)

您不能只使用utf-8编码,它取决于数据库中的字符集。

首先执行SHOW CHARACTER SET FOR mydatabase;,然后对要访问数据的表执行SHOW CHARACTER SET FOR mydatabase.mytable;以获取正确的字符集。

只有这样才能知道要使用的正确编码,这可能会解决您的问题并相应地进行设置。