Javascript函数只运行一次

时间:2016-12-27 14:04:24

标签: javascript php

在php类中,我定义了一个表,显示了一个需要联系的客户端列表。在第7列中,我集成了一个复选框,因此当用户呼叫客户端时,他/她会勾选此复选框。

问题是JS函数第一次运行然后第二次运行:default.php?page = _PhoneBankingP1:1未捕获的ReferenceError:未定义联系人(...)

使用的模块如下:

public function phonebank($townCode,$streetCode){           
            $query  =   "SELECT clientId, clientFirstname1, clientLastname1, clientAddress, ";
            $query  .=  "       clientMailshot, clientPhone1, clientMobile1, clientContacted, min(clientDoB) ";
            $query  .=  "FROM _clients ";
            $query  .=  "WHERE  _clients.streetCode = '{$streetCode}' and ";
            $query  .=  "       _clients.townCode   = '{$townCode}' and ";
            $query  .=  "       _clients.GE = 'Y' ";
            $query  .=  "GROUP BY clientAddress ";
            $result =   $this->db->query($query);


            $output = "";
            if ( $result->num_rows > 0 ){
                $output .=  "<div class='alert alert-info'><strong>Information!</strong> All the residents shown below have been extracted from the last Electoral Register.</div>";
                $output .=  "<table class='table table-striped' style='font-size:10pt;' id='myTable' >";
                $output .=      "<thead>";
                $output .=          "<tr>";
                $output .=              "<th>ID #</th>";
                $output .=              "<th>Name</th>";
                $output .=              "<th>Address</th>";
                $output .=              "<th>T</th>";
                $output .=              "<th>Phone</th>";
                $output .=              "<th>Mobile</th>";
                $output .=              "<th class='text-center'>Contacted</th>";
                $output .=          "</tr>";
                $output .=      "</thead>";
                $output .=      "<tbody>";


                while ( $record = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    $output .=  "<tr>";
                    $output .=      "<td><a href='default.php?page=_clientDetails&id=".$record['clientId']."&mode=edit' style='color: #000;'><span class='pb-clientId'>".$record['clientId']."</span></a></td>"; 
                    $output .=      "<td><span class='pb-fullname'>".$record['clientFirstname1']." ".$record['clientLastname1']."</span></td>";
                    $output .=      "<td>".$record['clientAddress']."</td>";
                    $output .=      "<td>".$record['clientMailshot']."</td>";
                    $output .=      "<td>".$record['clientPhone1']."</td>";
                    $output .=      "<td>".$record['clientMobile1']."</td>";

                    // Makes a checkbox selected
                    if ( $record['clientContacted'] == 'Y'){
                        $optContacted = ' checked ';
                    } else {
                        $optContacted = '';
                    }

                    //$output .=    "<td class='text-center' ><button id='btn-contacted-".$record['clientId']."' onclick='street.clientContacted(&quot;{$record['clientId']}&quot;,&quot;{$record['clientContacted']}&quot;)' class='btn btn-success'>Contacted</button></td>";

                    $output         .=  "<td align='center'>";
                    $output         .=      "<input type='checkbox' id='col7-".$record['clientId']."'  onclick='contacted(&quot;".$record['clientId']."&quot;);' value='1' ".$optContacted." />";
                    $output         .=  "</td>";


                    $output .=  "</tr>";
                }

                $output .=      "</tbody>";
                $output .=  "</table>";
                $output .=  "<br/>";

                echo $output;

            } else {
                echo "No Clients Found in this street";
            }
        }       

然后更新MYSQL所需的测试JS函数是:

function contacted(id) {
var clientId    =   id;
var col7        =   "col7-"+clientId;
var col7value   =   $("#"+col7).is(':checked');
var data        =   id+"\n"+col7+"\n"+col7value;

alert(data);

//Read checkbox state
if (col7value =='false'){
    contacted = 'N';
} else {
    contacted = 'Y';
}   

$.ajax({
    type:       "POST",
    url:        "_backend/_core/_database/update_Phonebank.php",
    data:       {
        "id":           clientId,
        "contacted":    contacted
    },
    dataType:   "text",
    success:    function(data){
    }
})  
}

如果你能帮助我决定第二次没有阅读这个功能,我将不胜感激。

1 个答案:

答案 0 :(得分:4)

您定义一个函数:

function contacted(id) {
    //...
}

那个函数中,你用一个值覆盖函数:

contacted = 'N';

因此,下次您尝试调用contacted()时,您尝试调用字符串,就好像它是一个函数一样。

为变量赋予唯一的名称:

var wasContacted = '';
//...
wasContacted = 'N';
// etc.

这样你就不会覆盖你不想覆盖的东西。

此外,使用var关键字声明变量将在特定范围内(例如在该函数内)定义它们,而不是仅将它们放在window对象上。 (你可以在不同的范围内具有相同名称的变量而不会相互影响。)