0
<div class="accordion" id="accordion2">
<?php $info= mysql_query("SELECT id, title, description FROM event"); ?>
  <?php while ($row = mysql_fetch_array($info, MYSQL_NUM)): ?>
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse<?php print $row[0]; ?>"><?php print $row[1]; ?></a><span class="delete" id="<?php print $row[0]; ?>">×</span>
    </div>
    <div id="collapse<?php print $row[0]; ?>" class="accordion-body collapse">
      <div class="accordion-inner">
        <div class="row-fluid">
          <div class="span6">
            <?php print $row[2]; ?>
          </div>
          <div class="span6 ppl">
            <ul>

            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
  <?php endwhile; ?>          
</div>

我有一个非常简单的方法可以打印出我的 twitter bootstrap 手风琴。我在我的页面上显示了这个,我在手风琴本身的顶部也有一个表单,我可以使用 ajax 将另一个表单提交到数据库中。这是表格:

<form class="form">
  <fieldset>
    <legend>Stuff</legend>
    <input class="class1" type="text" placeholder="xx">
    <input class="class2" type="text" placeholder="xx">
    <input class="class3" type="password" placeholder="xx">
    <textarea maxlength="320" rows="5" class="class4" placeholder="xx"></textarea>
    <div class="submit btn">Add</div>
  </fieldset>
</form>

这是jQuery:

$(".form .submit").bind("click", function() {
  var title = $(".form input.class1").val();
  var email = $(".form input.class2").val();
  var stuff1 = $('.form input.class3').val();
  var stuff2 = $('.form textarea.class4').val();

  if (title && email) {
    var data = {title: title, email: email, stuff1: stuff1, stuff2: stuff2};

    $.post("action.php", data, function() {
      $(".form input, .form textarea").val("");
        alert('done');
    });
  }
  else {
    alert('something went wrong');
  }
});

最后是我的 action.php 文件:

<?php require('access.php');

if ($_POST['title'] && $_POST['email']) {

  $title = $_POST['title'];
  $email = $_POST['email'];
  $stuff1 = $_POST['stuff1'];
  $stuff2 = $_POST['stuff2'];

  mysql_query("INSERT INTO events (title, email, stuff1, stuff2) VALUES('$title', '$email', '$stuff1', '$stuff2')");

}

一切正常,但正如标题所说,我希望输入的信息作为新的手风琴组添加到手风琴 div 之前(不刷新页面)。我的大脑拒绝在这方面与我合作,因为在我看来,如果我提交值,我必须向数据库发出另一个请求,以获取新事件的新生成的 id,以使我的手风琴布局正常工作。做什么?

并且很抱歉使用不推荐使用的 mysql 方式:O

4

1 回答 1

0

So mysql functions aside, all you are looking to do is to prepend some html to an element. You already have a callback function on your post and so all you need to do is to make it actually do something i.e.

$.post('action.php', data, function() {
    var newID = 123;  // get this from the response text
    $('div.accordion-inner').prepend('<div class="row-fluid"><div class="span6">'+newID +'</div><div class="span6 ppl"><ul></ul></div></div>');
});

This will attempt to prepend the div as specified to div.accordion-inner

Just replace the prepended with whichever html you are looking for.

And as an additional note, http://php.net/manual/en/function.mysql-insert-id.php will get you the PRIMARY KEY value for the last INSERT performed on your MySQL connection

于 2013-06-11T23:26:15.957 回答