使用PHP和Javascript将alphaneumeric值作为参数传递给函数时获取错误

时间:2016-05-05 12:23:19

标签: javascript php jquery

使用PHP和Javascript在函数内传递任何字母数字值时出现一个错误。

错误:

LOAD DATA INFILE "/tmp/test_data/cities.sql"
        INTO TABLE cities
        FIELDS TERMINATED BY '\t'
        LINES TERMINATED BY '\n'
        (combined, population, country_code, region, latitude, longitude);

我正在解释下面的代码。

category.php:235 Uncaught SyntaxError: missing ) after argument list

这里我收到了此<?php foreach ($listdata as $v) { $id = '15rf67'; $edit = 1; $del = 0; echo "<tr> <td>" . $counter++ . "</td> <td>" . $v['cat_title'] . "</td> <td> <select class='form-control' id='status1' name='status1' style='height:23px; padding:0px;'> <option value=''>Select Status</option> <option value='1' " . ($v['status'] == '1' ? 'selected' : '') . ">Enable</option> <option value='0' " . ($v['status'] == '0' ? 'selected' : '') . ">Disable</option> </select> </td> <td> <img src='uploads/" . $v['image'] . "' name='pro' border='0' style='width:50px; height:50px; border:#808080 1px solid;' /> </td> <td> " . $v['cat_description'] . " </td> <td> <button type='button' class='btn btn-xs btn-green outlinedivnone' data-placement='top' title='' data-original-title='Edit' onClick='javascript:changeOfActionMenu($id,$edit)'><i class='fa fa-pencil'></i> </button> <button type='button' class='btn btn-xs btn-danger outlinedivnone' data-placement='top' title='' data-original-title='Delete' onClick='javascript:changeOfActionMenu($id,$del)'><i class='fa fa-times'></i> </button> </td> </tr>"; } ?> 行中的错误。当我设置$ {id <button type='button' class='btn btn-xs btn-green outlinedivnone' data-placement='top' title='' data-original-title='Edit' onClick='javascript:changeOfActionMenu($id,$edit)'><i class='fa fa-pencil'></i> </button>时,这个错误不会出现,但我需要传递像{{1}这样的字母数字值请帮助我。

6 个答案:

答案 0 :(得分:2)

你必须在$ id周围加上引号,类似

<button type='button' class='btn btn-xs btn-green outlinedivnone' data-placement='top' title=''   data-original-title='Edit' onClick='javascript:changeOfActionMenu(\"".$id."\",$edit)'><i class='fa fa-pencil'></i> </button> 

<button type='button' class='btn btn-xs btn-danger outlinedivnone' data-placement='top' title=''  data-original-title='Delete' onClick='javascript:changeOfActionMenu(\"".$id."\",$del)'><i class='fa fa-times'></i> </button>

答案 1 :(得分:1)

我认为你错过了按钮功能参数的引用。引用alpa或字母数字值非常重要。我正在用正确的引号编写你的代码。

<?php
    foreach ($listdata as $v) {
        $id = '15rf67';
        $edit = 1;
        $del = 0;
        echo "<tr>
            <td>" . $counter++ . "</td>
            <td>" . $v['cat_title'] . "</td>
            <td>
                <select class='form-control' id='status1' name='status1'  style='height:23px; padding:0px;'>
                    <option value=''>Select Status</option>
                    <option value='1' " . ($v['status'] == '1' ? 'selected' : '') . ">Enable</option>
                    <option value='0' " . ($v['status'] == '0' ? 'selected' : '') . ">Disable</option>
                </select>
            </td>
            <td>
                <img src='uploads/" . $v['image'] . "' name='pro' border='0' style='width:50px; height:50px; border:#808080 1px solid;' />
            </td>
            <td>
                " . $v['cat_description'] . "
            </td>
            <td>
                <button type='button' class='btn btn-xs btn-green outlinedivnone' data-placement='top' title=''   data-original-title='Edit' onClick='javascript:changeOfActionMenu(\"$id\",$edit)'><i class='fa fa-pencil'></i> </button> 
                <button type='button' class='btn btn-xs btn-danger outlinedivnone' data-placement='top' title=''  data-original-title='Delete' onClick='javascript:changeOfActionMenu(\"$id\",$del)'><i class='fa fa-times'></i> </button>
            </td>
        </tr>";
    }

答案 2 :(得分:0)

<button type='button' class='btn btn-xs btn-green outlinedivnone' data-placement='top' title=''   data-original-title='Edit' onClick='javascript:changeOfActionMenu($id,$edit)'><i class='fa fa-pencil'></i> </button> 
<button type='button' class='btn btn-xs btn-danger outlinedivnone' data-placement='top' title=''  data-original-title='Delete' onClick='javascript:changeOfActionMenu($id,$del)'><i class='fa fa-times'></i> </button>

应该是

<button type='button' class='btn btn-xs btn-green outlinedivnone' data-placement='top' title=''   data-original-title='Edit' onClick='javascript:changeOfActionMenu(\"$id\",$edit)'><i class='fa fa-pencil'></i> </button> 
<button type='button' class='btn btn-xs btn-danger outlinedivnone' data-placement='top' title=''  data-original-title='Delete' onClick='javascript:changeOfActionMenu(\"$id\",$del)'><i class='fa fa-times'></i> </button>

答案 3 :(得分:0)

如果您的$id变量字符串是字符串,则必须在Javascript中将其作为字符串处理。使用这样的东西:

onClick='javascript:changeOfActionMenu(\"$id\",$edit)'

答案 4 :(得分:0)

请忽略告诉您只需要引号的所有答案 - 毕竟,如果有一天您决定ID应该包含撇号怎么办?一切都破了!

相反,这是另一种方法:

<button type='button' class='btn btn-xs btn-danger outlinedivnone'
    data-placement='top' title='' data-original-title='Delete'
    data-id='".htmlspecialchars($id)."'
    data-action='".htmlspecialchars($del)."'
    onclick='changeOfActionMenu(this)'
><i class='fa fa-times'></i> </button>

然后可以像这样调整changeOfActionMenu函数:

function changeOfActionMenu(btn) {
    var id = btn.getAttribute("data-id"),
        action = btn.getAttribute("data-action");
    // do stuff
}

这更加灵活,除了其他功能之外,还允许您委派事件处理程序并停止使用内联处理程序 - 这两件事都应该做;)

答案 5 :(得分:0)

 "<button type='button' class='btn btn-xs btn-green outlinedivnone' data-placement='top' title=''   data-original-title='Edit' onClick='javascript:changeOfActionMenu($id,$edit)'><i class='fa fa-pencil'></i> </button>";

onClick='javascript:changeOfActionMenu($id,$edit)'应为onClick='javascript:changeOfActionMenu(\"$id\",$edit)' ,因为$ id是字母数字(字符串),您需要为javascript函数字符串参数添加""