我需要这方面的帮助。我只想将已选中的项目提交到 mysql 表。
这是从 php 页面呈现的 html:
<table id="mylist" data-campaignId="2" cellspacing="0" width="100%">
<tbody>
<tr>
<td><input type="checkbox" name="selected[]" value="92" class="checkbox"></td>
<td><a href="/client/view/92">Bob</a></td>
<td><ul class="keywords">
<li><a href="#">Peter T</a></li>
<li class="orange-keyword"><a href="#">New Lead</a></li>
</ul></td>
<td>(555) 555-5555</td>
<td>dad@msn.com.com</td>
<td>2011-03-21</td>
<td class="table-actions"><a href="/client/view/92" title="View / Edit" class="with-tip"><img src="/images/icons/fugue/pencil.png" width="16" height="16"></a></td>
</tr>
<tr>
<td><input type="checkbox" name="selected[]" value="1074" class="checkbox"></td>
<td><a href="/client/view/1074">Elinda Canty</a></td>
<td><ul class="keywords">
<li><a href="#">Mary</a></li>
<li class="orange-keyword"><a href="#">New Lead</a></li>
</ul></td>
<td></td>
<td>mary@live.com</td>
<td>2011-06-29</td>
<td class="table-actions"><a href="/client/view/1074" title="View / Edit" class="with-tip"><img src="/images/icons/fugue/pencil.png" width="16" height="16"></a></td>
</tr>
</tbody>
</table>
<button id="add_users_to_campaign">Add Selected to Subscribers</button>
这是页面的完整来源。
<? $campaignid = $this->uri->segment(4); ?>
<script type="text/javascript">
<!--
$(document).ready(function(){
toggleChecked = function(status)
{
$(".checkbox").each( function() {
$(this).attr("checked",status);
});
}
});
-->
</script>
<script type="text/javascript">
$(function() {
$('#add_users_to_campaign').click(function() {
var campaignId = $("#mylist").attr("data-campaignId");
var clientid = [];
$("#mylist :checked").each(function() {
clientid.push($(this).closest("tr").attr("data-clientid"));
});
if (clientid.length === 0) {
return;
}
console.log(campaignId, clientid);
$.post('/ajaxmodels/add_users_to_campaign', {
campaignId: campaignId,
"clientid[]": clientid
}, function(data) {}, "json");
});
});
</script>
<!-- Content -->
<article class="container_12">
<section class="grid_12">
<div class="block-border">
<?
$attributes = array('class' => 'block-content form', 'id' => 'table_form');
echo form_open('', $attributes);
?>
<h1>Search Fields</h1>
<div class="columns">
<p class="colx4-left">
<label>Find</label>
<?php echo form_dropdown('quicksearch',$qs_array,'','class="full-width"');?>
</p>
<p class="colx4-mid">
<label>Where</label>
<?php echo form_dropdown('where',$where_array,'','class="full-width"');?>
</p>
<p class="colx4-center">
<label>Search</label>
<?php echo form_input('searchfor','','class="full-width"');?>
</p>
<p class="colx4-right">
<label> </label>
<input type="submit"/>
</p>
</div>
<? echo form_close(); ?></div>
</section>
<section class="grid_12">
<div class="block-border">
<form class="block-content form" id="table_form" method="post" action="">
<h1>Sortable table</h1>
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
<thead>
<tr>
<th class="black-cell"><span class="loading"></span></th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Name
</th>
<th scope="col">Client Of</th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Home Phone
</th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Email
</th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Created On
</th>
<th scope="col" class="table-actions">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($r->result() as $row) : ?>
<tr>"
<td>
<input type="checkbox" name="selected[]" value="<?=$row->id?>" class="checkbox">
</td>
<td><a href="/client/view/<?=$row->id?>"><?=$row->firstname?> <?=$row->lastname?></a></td>
<td><ul class="keywords">
<li><a href="#"><?=$row->clientof?></a></li>
<li class="orange-keyword"><a href="#"><?=$row->status?></a></li>
</ul></td>
<td><?=$row->homephone?></td>
<td><?=$row->email?></td>
<td><?=$row->created?></td>
<td class="table-actions">
<a href="/client/view/<?=$row->id?>" title="View / Edit" class="with-tip"><img src="/images/icons/fugue/pencil.png" width="16" height="16"></a>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="clear"></div>
<div class="block-footer">
<img src="images/icons/fugue/arrow-curve-000-left.png" width="16" height="16" class="picto">
<a href="javascript:toggleChecked('checked')" class="button">Select All</a>
<a href="javascript:toggleChecked('')" class="button">Unselect All</a>
<span class="sep"></span>
<button id="add_users_to_campaign">POST selected</button>
</div>
</form></div>
</section>
</article><!-- End Content -->
我只想将选中的项目插入此表:
ID - 自动递增
活动ID
客户编号
创建日期
我怎样才能做到这一点?
我还需要避免将客户端 ID 重复插入到相同的活动 ID 中。因此,如果此活动 ID 的客户端 ID 已存在,则跳过插入。
这是我目前在控制器 ajaxmodels 中的功能:
function add_users_to_campaign(){
$campagnid=$this->input->post('campaignid');
$client = $this->input->post('client');
foreach ($client as $client_id) {
$submit['clientid'] = $clientid;
$submit['campaignid'] = $this->input->post('campaignid');
$submit['creationdate'] = date('Y-m-d H:i:s');
$submit['createdby'] = $this->session->userdata('uid');
$newid = $this->campaign_model->add_user_to_campaign($submit);
}
}
这是campaign_model->add_user_to_campaign
function add_user_to_campaign($data)
{
$this->db->insert('campaign_to_client',$data);
$newid = $this->db->insert_id();
return $newid;
}
当我检查一个项目并提交页面刷新时没有错误并且我没有插入任何内容。