撇号在OpenJS Grid中不起作用

时间:2015-03-12 23:48:30

标签: php

我正在使用OpenJS Grid在页面上显示网格,我写了一个小模态脚本来向网格添加消息,以便用户向网格添加一行..我特意使用了PDO,因此特殊字符将被转义但是我注意到每次我使用带有撇号的消息时,撇号之后的所有字符都不会被添加到消息中。我以为PDO自动处理了这个?这是代码: PHP -

<?php

ini_set('display_errors',1); 
error_reporting(E_ALL);


header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include ("dbinfo.inc.php"); //include login info file

//Start a PDO session to insert the form data into the MySQL Table

try
    {
      $conn=new PDO($dsn, $username,$password);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
catch (PDOException $e)
    {
    $error_message=$e->getMessage();
    echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>";
    }

$prsn_from=$_POST['from_val'];
$note=$_POST['note_val'];
$crnt_date=date('Y-m-d H:i:s');


   //Clean input to make sure it is formatted with a leading Capital letter.
$prsn_from=ucfirst($prsn_from);

if ($prsn_from !='') { $stmt = $conn->prepare("INSERT INTO Messages SET prsn_from = :prsn_from, note = :note, crnt_date = :crnt_date");


     $stmt->execute(array(
            ':prsn_from' => $prsn_from,
            ':note' => $note,
            ':crnt_date' => $crnt_date));


     //Return to Main Page.      
     $url = 'http://' . $_SERVER['HTTP_HOST'];            // Get the server
     $url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
     $url .= '/index.html';            // <-- Your relative path
     header('Location: ' . $url, true, 302);              // Use either 301 or 302 
     die();
  }

else {

echo '<h2>Attention. You Did Not Enter a Message. Please enter the Message</h2>';

echo '<h2>At a Minimum...</h2>';
}
?>

php OpenGrid使用:

<?php
    // connect to db
    mysql_connect("localhost","alank","alank");
    mysql_select_db("Bulletin_Board");

require_once("grid.php");
$grid = new Grid("Messages", array(
"save"=>true,
"delete"=>true,
"select" => 'selectFunction'
));

和HTMl标记:

<!--OpenJS Grid Markup -->
        <h2>Messages</h2>
        <!--Note that adding  type="text" to a table column makes it editable.-->

        <table class="grid users" action="ajax.php">
            <tr>    
                <th col="prsn_from" width="100">From:</th>
                <th col="note" width="350" type="text">Message</th>
                <th col="crnt_date" width="200">Date/Time Entered</th>
            </tr>
        </table>

        <!-- End OpenJS Grid Markup --> 

        <!-- Add a Message Modal here...-->

    <!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Add a Message
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h3 class="modal-title" id="myModalLabel">Add a Message</h3>
      </div>
      <div class="modal-body">



      <!-- Add Message Form--------------------------------------------------------------------------->
        <div class="container">
  <h2>Memo</h2>
  <form role="form" action="add.php" method="post">
    <div class="form-group">
      <label for="from">From:</label>
      <input type="text" class="form-control" id="prsn_from" name="from_val" placeholder="Message From">
    </div>
    <div class="form-group">
      <label for="note">Enter Message:</label>
      <input type="textarea" class="form-control" rows="5" id="note" name="note_val" placeholder="Enter Your Message">
    </div>
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" id="newData" class="btn  btn-primary btn-md">Submit</button>
  </form> 

有关如何解决此问题的任何意见。

1 个答案:

答案 0 :(得分:1)

在GitHub网站上进一步阅读后,上面引用的帖子给出了解决方法。在grid.php更新代码

// form an array of the data to send back
        $data = array();
        $data['rows'] = array();
        foreach($rows as $i=>$row) {
            foreach($row as $col=>$cell) {
                // use primary key if possible, other wise use index
                $key = $primaryKey ? $row[$primaryKey] : $i;

                // primary key has an _ infront becuase of google chrome re ordering JSON objects
                //http://code.google.com/p/v8/issues/detail?id=164

                //This is fix for apostrophe problem
                if (strpos($cell, "'")) {
                  $_c = str_replace("'", "&#39", $cell); 
                   $cell = $_c;
                }
                //End fix
                $data['rows']["_".$key][$col] = utf8_encode($cell);
            }
        }

似乎作者没有更新grid.js来解决问题。