Ajax返回相同的最后一个值而不是更新的值

时间:2014-03-12 04:52:18

标签: php jquery ajax

您好我必须使用模式弹出更新我的数据库。所以我的主页php是

Main.php

<a href='javascript:void(0):' data-id={$row['customer_id']} class='btn small bg-blue-alt tooltip-button modal-customeredit' data-placement='top' title='Edit'><i class='glyph-icon icon-edit' ></i>

</a>

它将打开一个jquery弹出窗口,我将id作为jquery变量并通过ajax将其发送到php 使用此代码。

jquery页面

$( ".modal-customeredit" ).click(function() {
    event.preventDefault();
    var myGroupId = $(this).attr('data-id'); // data-id

    $.post("sample.php",
           {
               name:myGroupId,
               async: false           
           },
           function(data,status){
               alert(status)
           });
    $( "#modal-customeredit" ).dialog({
        modal: true,
        minWidth: 700,
        minHeight: 200,
        dialogClass: "modal-dialog",
        show: "fadeIn"
    });

    $('.ui-widget-overlay').addClass('bg-black opacity-60');
});

sample.php将获取id并设置会话

<?php
session_start();
if(isset($_POST['name']))
    {

        $value = $_POST['name'];
        $_SESSION["user_id"]=$value;
        print_r ($value);
    }
?>

所以在模态形式中php将是。

</div>
<!-- Customer Edit -->

<div class="hide" id="modal-customeredit" title="Edit Customer">
    <div class="mrg25T">
    <!--<h3>new Customer Info</h3>
    <p class="font-gray-dark"> Fides Admin rdfgdf colors & styles from both the default theme color schemes and the included core color helpers. </p>
    <div class="divider"></div>-->
    <?php

    $_SESSION["user_id"];
$id=  $_SESSION["user_id"];
$result = mysql_query("SELECT * FROM customer_mast  where  customer_id=$id") or trigger_error(mysql_error());
if($result==false)
    {
        echo "error";
    }

while($row = mysql_fetch_array($result)){
    foreach($row AS $key => $value){
        $row[$key] = stripslashes($value);
    }

    echo '  <form id="edit-customer" action="customer_edit.php" class="col-md-12 center-margin" method="post">
        <div class="form-row">
          <div class="form-label col-md-3">
            <label for="name">
              Name:
              <span class="required">*</span>
                       </label>
            </div>
          <div class="form-input col-md-9">
            <input id="name" name="customer_name" placeholder="Name" data-required="true" class="parsley-validated" type="text" value="'.$row['customer_name'].'">
            </div>
          </div>
        <div class="form-row">
          <div class="form-label col-md-3">
            <label for="details">
              Details :
              <span class="required">*</span>
              </label>
            </div>
          <div class="form-input col-md-9">
            <input id="details" placeholder="details" name="customer_details" data-required="true" class="parsley-validated" type="text" value="'.$row['customer_details'].'">
            </div>
          </div>
        <div class="form-row">
          <div class="form-label col-md-3">
            <label for="domain">
              Domain :
              <span class="required">*</span>
              </label>
            </div>
          <div class="form-input col-md-9">
            <input id="domain" placeholder="Domain" name="domain" data-required="true" class="parsley-validated" type="text" value="'.$row['domain'].'">
            </div>
          </div>
        <div class="form-row">
          <div class="form-label col-md-3">
            <label for="vertical">
              Vertical :
              <span class="required">*</span>
              </label>
            </div>
          <div class="form-input col-md-9">
            <input id="vertical" placeholder="Vertical" name="vertical" data-required="true" class="parsley-validated" type="text" value="'.$row['vertical'].'">
            </div>
          </div>
        <div class="form-row">
          <div class="form-label col-md-3">
            <label for="taxonomy">
              Taxonomy :
              <span class="required">*</span>
              </label>
            </div>
          <div class="form-input col-md-9">
            <input id="taxonomy" placeholder="Taxonomy" name="taxonomy" data-required="true" class="parsley-validated" type="text" value="'.$row['taxonomy'].'">
            </div>
          </div>
          <div class="divider"></div>
        <div class="form-row">
          <div class="col-md-2 col-md-offset-3">
         <input type="submit" class="btn medium primary-bg" id="edit-customer-valid" name="submit" onclick="javascript:$(\'#edit-customer\').parsley( \'validate\' );" value="Submit" />
            </div>
          </div>
        </form>
            </div>
          </div>';

}

?>
<?php } elseif ($pfile=="project.php") { ?>

所以我在主页面中获取id通过jquery传递给它并通过ajax将其发送到另一个页面,设置会话并以模态形式使用它来更新。但问题是它只能用于最后传递的id 。假设我从主页面传递id为2它只会在2.even进行更新操作虽然我下次通过3.所以我必须刷新页面并再次传递id 3。 我试着提供所有的细节。我知道这可能会令人困惑,如果你有任何想法,请帮助我。谢谢你!

1 个答案:

答案 0 :(得分:0)

您需要使用modal page加载ajax,否则每次尝试将模态框html移动到session userid等其他页面时,它都会加载相同的modal-user.php使用load()之类的,

$( "#modal-customeredit" )
     .load('modal-user.php') // load new session user details
                         // also use session_start() before using session user id
     .dialog({
        modal: true,
        minWidth: 700,
        minHeight: 200,
        dialogClass: "modal-dialog",
        show: "fadeIn"
     });