Ajax没有将变量发布到另一个页面

时间:2014-03-07 04:15:48

标签: php jquery ajax

您好我有使用ajax将我的变量发送到另一个PHP页面的问题。 我的问题是从主页面获取元素的id。它用于编辑操作。

主页模式:

  <a href='javascript:' 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>

它会在同一页面上打开模态:

<div class="hide" id="modal-projedit" title="Edit Project Info">
<div class="pad10A">
<h3>Edit Project Info</h3>
<p class="font-gray-dark"> Fides Admin uses colors & styles from both the default theme color schemes and the included core color helpers. </p>
<div class="divider mrg25B"></div>
<form id="project-edit" action="" class="col-md-12 center-margin" method="">
<div class="form-row">
<div class="form-label col-md-3">
<label for="name">
  Project Name:
  <span class="required">*</span>
  </label>
</div>
<div class="form-input col-md-9">
<input id="project_name" name="project_name" placeholder="Name" data-required="true" class="parsley-validated" type="text">
</div>
</div>
<div class="divider"></div>
<div class="form-row">
<div class="form-input col-md-8 col-md-offset-3">
<a href="javascript:;" class="btn medium primary-bg radius-all-4" id="project-edit-valid" onclick="javascript:$('#project-edit').parsley( 'validate' );" title="Validate!">
  <span class="button-content">
    Update
    </span>
  </a>
</div>
</div>
</form>
    </div>    
</div>

脚本:

$( ".modal-customeredit" ).click(function() {


       var myGroupId = $(this).data('id');
          alert( myGroupId);          
            $.ajax({
               type: "POST",
               url: "sample.php",
               data: { id: myGroupId }, // data retrieve on server-side ($_POST['id'])

           })




       $( "#modal-customeredit" ).dialog({
         modal: true,
         minWidth: 700,
         minHeight: 200,
         dialogClass: "modal-dialog",
         show: "fadeIn"
       });
       $('.ui-widget-overlay').addClass('bg-black opacity-60');

     });        

     });

4 个答案:

答案 0 :(得分:1)

尝试以下方法:

Change the ajax function,  
var myGroupId = $(this).attr('data-id'); // data-id
data = {'id':myGroupId };
$.ajax({
           type: "POST",
           url: "sample.php",
           data: data, // data retrieve on server-side ($_POST['id'])
           success:function(response){
           alert(response); //do the rest of operations.
           }

       });

答案 1 :(得分:0)

你没有提到alert所说的内容。

虽然我猜它显示的是Null值,因为ajax请求似乎很好。

我打赌你的选择器,试试这个:

var myGroupId = $(this).attr('data-id');

答案 2 :(得分:0)

HTML(带引号):

<a href="javascript:void(0)" data-id="{$row['customer_id']}" class="...">
    <i class='glyph-icon icon-edit' ></i>
</a>

jQuery(width data-id和callback):

$( ".modal-customeredit" ).click(function() {
   var myGroupId = $(this).attr('data-id'); // data-id
   $.ajax({
       type: "POST",
       url: "sample.php",
       data: { id: myGroupId }, 
    }).done(function(response) { // data returned by server
        var d = $('<div id="dialog">'+response+'</div>').appendTo('body');
        d.load(function(){
            $( "#dialog").dialog({
                modal: true,
                minWidth: 700,
                minHeight: 200,
                dialogClass: "modal-dialog",
                show: "fadeIn"
            });
            $('.ui-widget-overlay').addClass('bg-black opacity-60');
        });
    });
});

答案 3 :(得分:0)

你不会得到这个值,因为你在data-id属性中使用了回显你的php代码。 这是你需要对你的html

做的
<a href="javascript:void(0)" data-id="<?php echo $row['customer_id']?>" class="...">
    <i class='glyph-icon icon-edit' ></i>
</a>

并使用 @ seenath的代码检查您是否获得了值