2012-12-17 49 views
-1

我需要帮助。我只想提交检查过的项目到mysql表。php插入检查项目到mysql

下面是HTML代码的PHP页面呈现:

<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>[email protected]</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>[email protected]</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>&nbsp;</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 - 自动递增

CAMPAIGNID

的clientid

creationdate

我该怎么做?

我还需要避免重复插入clientid到同一个campaignid。所以如果这个campaignid的clientid已经存在,那就跳过插入。

这里是我目前在控制器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; 
} 

当我检查过的项目,并提交页面没有刷新错误,我没有插入任何东西。

+0

我在哪里来自 - 例如'campaignid','clientid'和'creationdate'的信息? – BenM

+0

info正在进入一个php搜索结果页面。 creationdate只是它插入的当前日期(时间戳)。 – DSmith

+0

我试过这个,但帮助得到了复杂的方式,所以我简化了我的问题。 http://stackoverflow.com/questions/13897179/post-selected-items-to-table – DSmith

回答

0

而不是在第一个例子中完成所有四项操作(即让你头脑发热),试试表格中的一行,看看是否有效。

此外,您应该考虑不使用每个项目的相同名称。即使用

<input type="checkbox" name="selected[1]" class="checkbox" value="yes"> 
<input type="checkbox" name="selected[92]" class="checkbox" value="yes"> 
<input type="checkbox" name="selected[1024]" class="checkbox" value="yes"> 

EDIT

的数据从一种形式回成对(即名称,值)。该名称需要是唯一的。但你的不是。您有三个名称值为selected[]且具有不同(可能)值的名称。你如何区分他们?

我的第一款是解释它是如何最好地去调试(先简单再添加更多的并发症。

进一步编辑

我已经增值位以上。

或者你也可以做到这一点

<input type="checkbox" name="selected[]" class="checkbox" value="1"> 
<input type="checkbox" name="selected[]" class="checkbox" value="92"> 
<input type="checkbox" name="selected[]" class="checkbox" value="1024"> 

米老鼠例

PHP第一

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
</head> 
<body> 
<pre> 

The forms data 
<?php 
$selector = $_GET['selector']; 
print_r($selector); 
echo "Now go through than data\n"; 

foreach ($selector as $name=>$value) 
{ 
    // Find the name 
    $character = $_GET["id$name"]; 

    echo "$character is $name or $value depends on the method you chose\n"; 
} 
echo "\n\nThose are the checked ones"; 
?> 
</pre> 
</body> 
</html> 

这里是形式:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Wibble</title> 
</head> 
<body> 
<form action="send.php" method="get"> 
<input type="hidden" name="id0" value="client Joe Bloggs"> 
<input type="hidden" name="id1" value="Client Bugs Bunny"> 
<input type="hidden" name="id2" value="Client Al Capone "> 
Check 1 <input type="checkbox" name="selector[]" value="1"> 
Check 80 <input type="checkbox" name="selector[]" value="80"> 
Check 200 <input type="checkbox" name="selector[]" value="200"> 
<input type="submit" value="depends"> 
</form> 
<form action="send.php" method="get"> 
<input type="hidden" name="id1" value="client Joe Bloggs"> 
<input type="hidden" name="id80" value="Client Bugs Bunny"> 
<input type="hidden" name="id200" value="Client Al Capone "> 
Check 1 <input type="checkbox" name="selector[1]" value="yes"> 
Check 80 <input type="checkbox" name="selector[80]" value="yes"> 
Check 200 <input type="checkbox" name="selector[200]" value="yes"> 
<input type="submit" value="otherway"> 
</form> 

</body> 
</html> 
+0

不确定你在这里是什么意思? – DSmith

+0

参见上面的编辑。 –

+0

Dev - 我添加了clientid的属性值并在上面进行了编辑。埃德谢谢,但我读到,选定必须像它是以某种方式检查它,看看它是否被选中? – DSmith