如何在HTML" id"中存储php变量内容

时间:2017-02-14 21:53:11

标签: javascript php jquery html

我在PHP中有3个变量,message_id,message_title,message_content。我将这些变量的内容存储在HTML' id'以便我以后可以使用jQuery。

像这样:

Varibles:

data-slide

这里是" ID"部分。

<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width; initital-scale=1">
  <title> Bootstrap Example</title>

  <!-- Bootstrap References -->

  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

  <!-- jQuery library -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

  <!-- Latest compiled JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  <script src="index.js"></script>
  <link href="index.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-sm-12">
        <div id="mySlider" class="carousel slide" data-ride="carousel">

          <!-- .nav's buttons on the bottom -->
          <ol class="carousel-indicators">
            <li data-target="#mySlider" data-slide-to="0" class="active"></li>
            <li data-target="#mySlider" data-slide-to="1"></li>
          </ol>

          <!-- Image slides -->
          <div class="carousel-inner" role="listbox">

            <div class="item active">
              <figure>
                <img width=500px height=500px src="http://www.hdwallpapersfreedownload.com/uploads/large/fruits/orange-fruit-with-leaf-hd.jpg" alt="Slide1">
                <!--<div class="carousel-caption">This is a fruit leaf</div>-->
              </figure>
              <figcaption class="carousel-caption">This is a fruit leaf</figcaption>
            </div>

            <div class="item">
              <figure>
                <img width=500px height=500px src="http://www.greatgrubclub.com/domains/greatgrubclub.com/local/media/images/medium/4_1_1_kiwi.jpg" alt="Slide2">
                <!--<div class="carousel-caption">This is a fruit leaf</div>-->
              </figure>
              <figcaption class="carousel-caption">KIWIf</figcaption>
            </div>

          </div><!-- END: Image slides -->


<!-- Slide buttons (left,right) buttons -->

<a class="left carousel-control" href="#mySlider" role="button" data-slide="prev">
  <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
  <span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#mySlider" role="button" data-slide="next">
  <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
  <span class="sr-only">Next</span>
</a>

        </div>
        <!-- END: Slider -->
      </div>
    </div>
  </div>
</body>

如何查看,.img-container { max-height: 300px; overflow: hidden; } .img-container img { max-height: 100%; max-width: 100%; } $id_variable = $rows['id_mensagem']; $message_title_edit = $rows['title_text']; $message_content_edit = $rows['txt_mensagem']; echo '<a href="#" class="edit_message" id='.$id_variable.' message='.$message_content_edit.' titulo ='.$message_title_edit .' data-toggle="modal" data-target="#open_modal_edit">Detalhes </a>';

我的问题在哪里:

Id消息&amp; titulo将数据存储在第一个空白区域内。

像这样:

id=$id_variable

回声/输出:

message = $message_content_edit

Image of what's happening

3 个答案:

答案 0 :(得分:2)

这是因为你从不打印任何引号。

echo '<a href="#" class="edit_message" id='.$id_variable.' message='.$message_content_edit.' titulo ='.$message_title_edit .' data-toggle="modal" data-target="#open_modal_edit">Detalhes </a>';

实际评估为

<a href="#" class="edit_message" id=10 message=Some content titulo=Just some title data-toggle="modal" data-target="#open_modal_edit">Detalhes </a>

哪个不是有效的HTML,所以浏览器/解释器会尝试猜测你的意思。

您需要在打印输出中添加引号

echo '<a href="#" class="edit_message" id="'.$id_variable.'" message="'.$message_content_edit.'" titulo ="'.$message_title_edit .'" data-toggle="modal" data-target="#open_modal_edit">Detalhes </a>';

答案 1 :(得分:0)

你需要正确引用它们,试试:

 echo '<a href="#" class="edit_message" id="'.$id_variable.'" message="'.$message_content_edit.'" titulo ="'.$message_title_edit .'" data-toggle="modal" data-target="#open_modal_edit">Detalhes </a>';

答案 2 :(得分:0)

你需要在值周围添加“”,titulo在等于之前有一个空格。

看起来您可能在变量中有一些消息信息,因此在输出之前可能需要对变量进行html编码,这样如果$ message_content_edit包含“那么它就不会破坏HTML,例如

echo '<a href="#" class="edit_message" id="' . $id_variable . '" message="' . htmlspecialchars($message_content_edit) . '" titulo="' . htmlspecialchars($message_title_edit) . '" data-toggle="modal" data-target="#open_modal_edit">Detalhes </a>';