我对ajax的概念很陌生...
我从ajax发布数据,但它仅从foreach循环发布第一个数组。
代码----
<tbody id="presentor">
<?php if(!empty($data)):?>
<?php $counter = '0'; foreach ($data as $lead):?>
<?php $prospect_type = $this->admin_model->getData('prospect_type',$lead->prospect_type);?>
<?php $language = $this>admin_model->getData('language',$lead>language);?>
<?php $source = $this->admin_model->getData('source',$lead->source);?>
<?php $location = $this->admin_model->getData('location',$lead>location);>
<?php $requirement = $this->admin_model->getData('requirement',$lead->requirement);?>
<?php $lead_type = $this>admin_model->getData('lead_type',$lead>lead_type);?>
<?php $lead_status = $this->admin_model->getData('status',$lead->lead_status);?>
<tr role="row" class="odd">
<td tabindex="0" class="sorting_1" style="text-transform: uppercase; text-align: center;"><?php echo ++$counter;?></td>
<td id="lead_date"><?php echo $lead->leadDate;?></td>
<td id="first_name"><?php echo $lead->first_name;?></td>
<td id="last_name"><?php echo $lead->last_name;?></td>
<td id="prospect_type"><?php echo $prospect_type[0]->prospect_type;?></td>
<td id="number"><?php echo $lead->number;?></td>
<td id="email_id"><?php echo $lead->email_id;?></td>
<td id="language"><?php echo $language[0]->language_name;?></td>
<td id="source"><?php echo $source[0]->source_name;?></td>
<td id="location"><?php echo $location[0]->location_name;?></td>
<td id="requirement"><?php echo $requirement[0]->requirement;?></td>
<td id="lead_type"><?php echo $lead_type[0]->lead_type;?></td>
<td><?php echo $lead->executive_comment;?></td>
<td><?php echo $lead->admin_remark;?></td>
<td><?php echo $lead_status[0]->status;?></td>
<td style="display: none;text-align: center;">
<a href="<?php echo $base_url;?>dashboard/edit_Data/<?php echo $lead->id;?>/add_lead/"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
| <a href="javascript:void(0);" onmousedown="deleteLead('<?php echo $base_url;?>dashboard/delete_Data/<?php echo $lead->id;?>/lead/');"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
| <a data-toggle="modal" href="#myModal" id="lead_id" data-custom-value="<?php echo $lead->id;?>"><i class="fa fa-paper-plane" aria-hidden="true"></i></a>
</td>
</tr>
<?php endforeach;?>
<?php endif;?>
</tbody>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content" id="leadId">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Select Employee</h4>
</div>
<div class="modal-body">
<div class="item form-group">
<div for="name">
<span class="required">Assigned To</span>
</div>
<div>
<?php $user = $this->admin_model->getData('user');?>
<select class="form-control" name="user" id="assign_user">
<option value="">Select User to Assign</option>
<?php foreach ($user as $row): ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->first_name . " " . $row->last_name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<a id="assignResult" class="btn btn-primary modal-toggle" style="background-color: #000;border-color: #000;">Assign</a>
</div>
</div>
</div>
我也使用html模型调用用户ID,下面的名称是代码
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content" id="leadId">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Select Employee</h4>
</div>
<div class="modal-body">
<div class="item form-group">
<div for="name">
<span class="required">Assigned To</span>
</div>
<div>
<?php $user = $this->admin_model->getData('user');?>
<select class="form-control" name="user" id="assign_user">
<option value="">Select User to Assign</option>
<?php foreach ($user as $row): ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->first_name . " " . $row->last_name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<a id="assignResult" class="btn btn-primary modal-toggle"style="background-color: #000;border-color: #000;">Assign</a>
</div>
</div>
jquery和ajax文件在这里----
<script>
$(document).ready(function() {
$("#assignResult").click(function () {
var lead_date = $( '#lead_date' ).text();
var lead_id = $( '#lead_id' ).data("custom-value");
var assign_userId = $( "#assign_user option:selected" ).val();
var first_name = $( "#first_name" ).text();
var last_name = $( "#last_name" ).text();
var prospect_type = $( "#prospect_type" ).text();
var number = $( "#number" ).text();
var email_id = $( "#email_id" ).text();
var language = $( "#language" ).text();
var source = $( "#source" ).text();
var location = $( "#location" ).text();
var requirement = $( "#requirement" ).text();
var lead_type = $( "#lead_type" ).text();
$.ajax({
url: '<?php echo $base_url?>dashboard/send_leads',
type: 'POST',
data: {
lead_date : lead_date,
lead_id : JSON.stringify(lead_id),
assign_user: assign_userId,
first_name : first_name,
last_name : last_name,
prospect_type : prospect_type,
number : number,
email_id : email_id,
language : language,
source : source,
location : location,
requirement : requirement,
lead_type : lead_type,
},
success: function (data) {
alert(data);
$('#myModal').modal('hide');
},
error: function(errorThrown){
alert(errorThrown);
alert("There is an error with AJAX!");
}
});
});
});
</script>
这就是我的数组的样子
Array
(
[0] => stdClass Object
(
[id] => 2
[leadDate] => 2018-08-01
[first_name] => Rishab
[last_name] => Patel
[prospect_type] => 1
[number] => 9988665523
[email_id] => rishab@gmail.com
[language] => 2
[source] => 1
[location] => 2
[requirement] => 1
[lead_type] => 3
[executive_comment] => Call him
[admin_remark] => good
[lead_status] => 1
)
[1] => stdClass Object
(
[id] => 3
[leadDate] => 2018-08-09
[first_name] => Ajay
[last_name] => Patel
[prospect_type] => 4
[number] => 1232426225
[email_id] => ajaypatel@gmail.com
[language] => 2
[source] => 2
[location] => 1
[requirement] => 1
[lead_type] => 3
[executive_comment] => Call him
[admin_remark] => good
[lead_status] => 1
)
[2] => stdClass Object
(
[id] => 4
[leadDate] => 2018-08-13
[first_name] => praney
[last_name] => panday
[prospect_type] => 3
[number] => 7845123265
[email_id] => praneypanday@gmail.com
[language] => 2
[source] => 1
[location] => 2
[requirement] => 1
[lead_type] => 2
[executive_comment] => Call him
[admin_remark] => good
[lead_status] => 1
)
)
但是当我通过ajax发布它时,它仅发布[0]数组,然后我尝试提醒每个var,但它显示相同的[0]数组值。
任何帮助,或者我做错了
提前谢谢...。
答案 0 :(得分:0)
您的错误是您为表中相同“ 类型”的每一行分配了相同的HTML ID。
让我解释一下:如果您输入
<tr role="row" class="odd"></tr>
每个单元格进入循环
<td id="lead_date"></td>
的ID为#lead_date
。
由于元素ID在整个文档中应该是唯一的,因此生成的HTML无效。
jQuery脚本将#lead_date
的值分配给内部变量
var lead_date = $( '#lead_date' ).text();
但是,如果您有多个ID为#lead_date
的字段,将分配哪个字段?只有第一个。
这适用于当前html表的每个字段,这就是为什么仅发布第一个数组的原因。
要解决您的问题,首先不要重复HTML ID。