php - Apostrophe not working in OpenJS Grid


I am using OpenJS Grid to show a grid on a page and I wrote a little modal script to add a message to the grid for users to add a row to grid.. I specifically used PDO so special characters would be escaped but I noticed every time I use a message with an apostophe in it all characters past the apostrophe don't get added to message. I thought PDO handled this automatically? Here is code: php-



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

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

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

$crnt_date=date('Y-m-d H:i:s');

   //Clean input to make sure it is formatted with a leading Capital letter.

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

            ':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 

else {

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

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

The php OpenGrid uses:

    // connect to db

$grid = new Grid("Messages", array(
"select" => 'selectFunction'

And the HTMl markup:

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

    <table action="ajax.php">
        <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>

    <!-- End OpenJS Grid Markup --> 

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

  <!-- Button trigger modal -->
<button type="button" data-toggle="modal" data-target="#myModal">
 Add a Message

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

   <!-- Add Message Form 

Any input on how to resolve this appreciated.



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



After reading further on GitHub site the post cited above gives the work around. In grid.php update code to

// 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

                //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);

It doesn't seem as if the author ever updated grid.js to fix the problem.

People are also looking for solutions to the problem: php - Protecting admin page without database


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.