-1

我需要这方面的帮助。我只想将已选中的项目提交到 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>&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 - 自动递增

活动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;
}

当我检查一个项目并提交页面刷新时没有错误并且我没有插入任何内容。

4

1 回答 1

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">

编辑

从表单中成对返回的数据(即名称、值)。名称必须是唯一的。但你的不是。您有三个具有价值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>
于 2012-12-17T18:46:00.437 回答