当输入有撇号时,无法重新打开模态(')

时间:2016-07-22 21:07:23

标签: javascript php jquery

我的系统具有编辑功能。我可以使用撇号输入文本,但是当我尝试打开编辑模式时,它不再起作用了。

我使用javascript函数将值传递给editModal。一些参数包含文本。所以我将变量包装在\"\"

这是我的代码。

输出行。其中包含调用editModal

的javascripts函数
  <?php
    $sql = $db->prepare("SELECT * from tbl_street
                        WHERE streetStatus <> 2");

            $sql->execute();
        while($result = $sql->fetch(PDO::FETCH_ASSOC))
        {
          $id           = $result['streetID'];
          $streetName    = $result['streetName'];
          $status       = ($result['streetStatus']==1) ? "checked" : "";


          echo "
          <tr>
            <td>$id</td>
            <td>$streetName</td>
            <td>
            <input type='checkbox'  onchange='switchStatus($id,this)'  data-toggle='toggle' $status>
            </td>
            <td>
            <div class='btn-group'  role='group'>
              <input type='button'  value='Manage'  onclick='Xmanage($id,\"$streetName\")'  class='btn btn-info'>
              <input type='button'  value='Remove'  onclick='Xdelete($id)'                 class='btn btn-danger'>
            </div>
            </td>
          </tr>
          ";
        }
        ?>

这里是编辑代码和模态

<div class="form-group">
<!-- Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <!--HEADER-->
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><h3>Manage Lot</h3></h4>
      </div>

      <form action="" method="POST">
      <!--BODY-->
      <div class="modal-body">

        <input name="theID"id="theID" type="text" class="form-control"  >
        <div class="row">
          <div class="row">
          <div class="col-md-5">
            <label for="streetName">Street Name</label>
            <input name="estreetName" id="strtnm"pattern="[a-zA-Z0-9]+[a-zA-Z0-9 ]+" type="text" class="form-control" required placeholder="Street Name">
          </div>
        &nbsp;
      </div>
      <!--FOOTER-->
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" name="editSubmit"class="btn btn-primary">Save</button>
      </div>
    </div>
  </div>
  </div>

  <?php
  try{
    if(isset($_POST['editSubmit'])){
      $id                       = $_POST['theID'];
      $streetName        = $_POST['estreetName'];
      $sql = $db->prepare("
      UPDATE tbl_street
      SET `streetName`      = :sName
      WHERE streetID        = :id
      ");

      $sql->execute(array(
        ":sName"            => $streetName,
        ":id"                   => $id
      ));
      echo"<meta http-equiv='refresh' content='0'>";
    }
  }catch(Exception $e)
  {
    echo $e;
  }
   ?>

1 个答案:

答案 0 :(得分:0)

对于一个更干净的PHP输出不要从PHP cos回显html你会困惑在哪里逃避。最好的方法是将php嵌入到html中,这是可读的,并且在与非后端开发人员团队合作时有所帮助。

做这样的事情

<?php
$sql = $db->prepare("SELECT * from tbl_street WHERE streetStatus <> 2");

$sql->execute();
while($result = $sql->fetch(PDO::FETCH_ASSOC)){
$id           = $result['streetID'];
$streetName    = $result['streetName'];
$status       = ($result['streetStatus']==1) ? "checked" : "";
?>

<tr>
    <td><?= $id ?></td>
    <td><?= $streetName ?></td>
    <td>
        <input type="checkbox"  onchange="switchStatus($id,this)"  data-toggle="toggle" <?= $status ?>>
    </td>
    <td>
        <div class="btn-group" role="group">
            <input type="button"  value="Manage"  onclick="Xmanage(<?= $id ?>, <?= $streetName ?>)"  class="btn btn-info">
            <input type="button"  value="Remove"  onclick="Xdelete(<?= $id ?>)" class="btn btn-danger">
        </div>
    </td>
</tr>
";
<?php
}
?>

如果必须回显html代码,请使用单引号,html将保留其双引号。