1

我用 jquery ajax 在 mysql 服务器中添加了一个联系人。但是当我添加联系人时,我必须刷新我的页面才能看到添加的联系人。

我的脚本:

<script type="text/javascript">

$(document).ready(function() {
$('#form_contact').on('submit', function() {

    var nom_contact = $('#nom_contact').val();
    var prenom_contact = $('#prenom_contact').val();

    if(prenom_contact == '' || nom_contact == '') {
        alert('Les champs doivent êtres remplis');
    } else {
        $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            dataType: 'json',
            success: function(json) {
                if(json.reponse == 'ok') {
                    alert('Tout est bon');
                } else {
                    alert(''+ json.reponse);

                }
            }
        });
    }
    return false;
});
});
</script>

我的表格(表格下方有联系人列表):

<div class="tab-pane" id="contacts">
<h2 class="text-info">Contacts</h2>
<form class="form-horizontal" id="form_contact" method="post" action="modif/add_contact.php" >
      <label for="Nom">Nom</label>
      <input type="hidden" name="id_user" value="<?php echo $_GET['modifier'] ?>">
      <input class="span5" type="text" name="nom_contact">

      <label for="Prenom">Prenom</label>
      <input class="span5" type="text" name="prenom_contact">

      <label for="tel">Telephone</label>
      <input class="span5" type="text" name="telephone_contact">

      <label for="desc">Description courte (qui est-ce?)</label>
      <input class="span5" type="text" name="description_contact">

      <input type="submit" name="contact" class="btn btn-primary pull-right" value="Valider" />


</form>
<div id="div_contact">
      <table class="table table-striped">
      <thead>
        <tr>
          <th>Nom</th>
          <th>Prénom</th>
          <th>Description</th>
          <th>Télephone</th>

        </tr>
      </thead>
    <tbody>

    <?php
    $result = mysql_query("SELECT * FROM Contact WHERE id_user = ".$_GET['modifier']."");

    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
       printf ("
              <tr>
                  <td>".$row["nom_contact"]."</td>
                  <td>".$row["prenom_contact"]."</td>
                  <td>".$row["description_contact"]."</td>
                  <td>".$row["telephone_contact"]."</td>
                  <td><a href='utilisateur.php?modifier=".$_GET['modifier']."&supprimer_contact=".$row[0]."' onclick='return(confirm(\"Etes-vous sûr de vouloir supprimer cette entrée?\"));'>Supprimer</a></td>


                </tr>
                ");
    }

    mysql_free_result($result);
    ?>

      </tbody>

    </table>

有人可以帮助我吗?

4

1 回答 1

0

我在代码的 ajax 成功部分中没有看到任何内容,您实际上将联系人添加到列表中。查询您的联系人表的 PHP 代码块只会在您加载页面时进行评估,这就是为什么它会在页面刷新时显示,而不是在基于 ajax 的表单提交之后显示。

于 2013-06-10T14:19:34.067 回答