18910140161

PHP/JavaScript动态选择选项-如何处理选择选项文本堆栈溢出中的撇号问题

顺晟科技

2022-10-19 14:22:15

165

我在SO上读过许多关于这个主题的帖子,但我遇到的建议解决方案都没有帮助我解决问题。

我在PHP中基于MySql查询的结果构建了一个动态HTML选择框。除了select选项的选定文本值之外,在value属性中,我还将两个值传递给Javascript以便稍后使用,它们之间用逗号分隔(与问题无关,只是为了使下面的代码更清晰)。

我完全理解字符串中的撇号会引起问题,需要采取特殊的预防措施来处理它们。我的工作原理是,当动态创建select box控件时,我只需在任何单引号前放一个''字符,就可以获得传递的值,稍后由JavaScript按字面处理。

我的动态PHP选择框代码如下所示:

$locSql = "select name, shortname, locationid from location_tbl l where ....";
    $locResultSet = mysqli_query($conn, $locSql) or die("Bad SQL " . $locSql); 

    $opt = "<select name='selFieldLocation' id='fieldLocation' onChange='setSelectedLocName(this);'>";
    $opt .= '<option value="blank">Select Location</option>';
    while ($row = mysqli_fetch_assoc($locResultSet)) {
        $selectedLocID = $row['locationid']; 
        
        // This echo statement returns the value from the database, e.g.....
        // "SUN Men's Group"
        echo 'first row shortname ' . $row['shortname'] . "<br>";

        // Applied "addslashes" to that value.
        $thisShortName = addslashes($row['shortname']);

        // Echo the new value....
        // The result is: "SUN Men\'s Group" (which is what i was after)
        echo 'new shortname is ' . $thisShortName . "<br>";

        $opt .= "<option value='{$row['locationid']},$thisShortName'> {$row['name']} </option>\n"; 
        //echo 'row shortname ' . $thisShortName . "<br>";
    }
    $opt .= "</select>";
    $opt .= '<input type="hidden" name="selLocText" id="selLocText"  />';
    $opt .= '<input type="hidden" name="selLocShortName" id="selLocShortName"  />';

    var_dump($opt);

结尾处的var_dump显示创建选择框的动态代码,正如我所期望的那样:

$locSql = "select name, shortname, locationid from location_tbl l where ....";
    $locResultSet = mysqli_query($conn, $locSql) or die("Bad SQL " . $locSql); 

    $opt = "<select name='selFieldLocation' id='fieldLocation' onChange='setSelectedLocName(this);'>";
    $opt .= '<option value="blank">Select Location</option>';
    while ($row = mysqli_fetch_assoc($locResultSet)) {
        $selectedLocID = $row['locationid']; 
        
        // This echo statement returns the value from the database, e.g.....
        // "SUN Men's Group"
        echo 'first row shortname ' . $row['shortname'] . "<br>";

        // Applied "addslashes" to that value.
        $thisShortName = addslashes($row['shortname']);

        // Echo the new value....
        // The result is: "SUN Men\'s Group" (which is what i was after)
        echo 'new shortname is ' . $thisShortName . "<br>";

        $opt .= "<option value='{$row['locationid']},$thisShortName'> {$row['name']} </option>\n"; 
        //echo 'row shortname ' . $thisShortName . "<br>";
    }
    $opt .= "</select>";
    $opt .= '<input type="hidden" name="selLocText" id="selLocText"  />';
    $opt .= '<input type="hidden" name="selLocShortName" id="selLocShortName"  />';

    var_dump($opt);

实际上,当用户选择一个选项时,将调用“setselectedlocname”javascript函数(该函数驻留在外部JS文件中)。该代码如下所示:

$locSql = "select name, shortname, locationid from location_tbl l where ....";
    $locResultSet = mysqli_query($conn, $locSql) or die("Bad SQL " . $locSql); 

    $opt = "<select name='selFieldLocation' id='fieldLocation' onChange='setSelectedLocName(this);'>";
    $opt .= '<option value="blank">Select Location</option>';
    while ($row = mysqli_fetch_assoc($locResultSet)) {
        $selectedLocID = $row['locationid']; 
        
        // This echo statement returns the value from the database, e.g.....
        // "SUN Men's Group"
        echo 'first row shortname ' . $row['shortname'] . "<br>";

        // Applied "addslashes" to that value.
        $thisShortName = addslashes($row['shortname']);

        // Echo the new value....
        // The result is: "SUN Men\'s Group" (which is what i was after)
        echo 'new shortname is ' . $thisShortName . "<br>";

        $opt .= "<option value='{$row['locationid']},$thisShortName'> {$row['name']} </option>\n"; 
        //echo 'row shortname ' . $thisShortName . "<br>";
    }
    $opt .= "</select>";
    $opt .= '<input type="hidden" name="selLocText" id="selLocText"  />';
    $opt .= '<input type="hidden" name="selLocShortName" id="selLocShortName"  />';

    var_dump($opt);

上面的alert语句中显示的内容如下所示:

$locSql = "select name, shortname, locationid from location_tbl l where ....";
    $locResultSet = mysqli_query($conn, $locSql) or die("Bad SQL " . $locSql); 

    $opt = "<select name='selFieldLocation' id='fieldLocation' onChange='setSelectedLocName(this);'>";
    $opt .= '<option value="blank">Select Location</option>';
    while ($row = mysqli_fetch_assoc($locResultSet)) {
        $selectedLocID = $row['locationid']; 
        
        // This echo statement returns the value from the database, e.g.....
        // "SUN Men's Group"
        echo 'first row shortname ' . $row['shortname'] . "<br>";

        // Applied "addslashes" to that value.
        $thisShortName = addslashes($row['shortname']);

        // Echo the new value....
        // The result is: "SUN Men\'s Group" (which is what i was after)
        echo 'new shortname is ' . $thisShortName . "<br>";

        $opt .= "<option value='{$row['locationid']},$thisShortName'> {$row['name']} </option>\n"; 
        //echo 'row shortname ' . $thisShortName . "<br>";
    }
    $opt .= "</select>";
    $opt .= '<input type="hidden" name="selLocText" id="selLocText"  />';
    $opt .= '<input type="hidden" name="selLocShortName" id="selLocShortName"  />';

    var_dump($opt);

简而言之,我一直认为用斜杠(“”)转义单引号(正如PHP“addslashes”函数所做的那样)会告诉javascript按字面意思处理单引号,但JS似乎仍然会截断单引号处的字符串。有趣的是,“x.options[x.selectedindex].text”值(在字符串中也包含一个单引号)在没有任何程序员干预的情况下正确传递。(?)

有人能提出一个方法让我在这里达到我的目标吗?它似乎(至少对我来说)我已经设置了动态选择框,完全像我希望的那样。 非常感谢您的帮助!


顺晟科技:

您可以尝试其他方法,而不是将创建的整个html加载到string html($opt)变量中,然后一次全部显示。

您可以在需要html的地方放置一个循环(while或收集其中的数据,并在正确的位置创建一个循环),然后像这样在那里创建它:

$locSql = "select name, shortname, locationid from location_tbl l where ....";
    $locResultSet = mysqli_query($conn, $locSql) or die("Bad SQL " . $locSql); 

    $opt = "<select name='selFieldLocation' id='fieldLocation' onChange='setSelectedLocName(this);'>";
    $opt .= '<option value="blank">Select Location</option>';
    while ($row = mysqli_fetch_assoc($locResultSet)) {
        $selectedLocID = $row['locationid']; 
        
        // This echo statement returns the value from the database, e.g.....
        // "SUN Men's Group"
        echo 'first row shortname ' . $row['shortname'] . "<br>";

        // Applied "addslashes" to that value.
        $thisShortName = addslashes($row['shortname']);

        // Echo the new value....
        // The result is: "SUN Men\'s Group" (which is what i was after)
        echo 'new shortname is ' . $thisShortName . "<br>";

        $opt .= "<option value='{$row['locationid']},$thisShortName'> {$row['name']} </option>\n"; 
        //echo 'row shortname ' . $thisShortName . "<br>";
    }
    $opt .= "</select>";
    $opt .= '<input type="hidden" name="selLocText" id="selLocText"  />';
    $opt .= '<input type="hidden" name="selLocShortName" id="selLocShortName"  />';

    var_dump($opt);

这样可以节省很多撇号!--而不是echo$opt; 我们创建html,并在它之间放置循环,这实际上是在PHP代码中构建html的一种更容易的方法,但有时它会迫使您在html中构建循环...

这是PHP最好的特性之一...就像你把它放在JS脚本中一样,你也可以把它放在HTML中!:)

使用代码中的模拟数据:

$locSql = "select name, shortname, locationid from location_tbl l where ....";
    $locResultSet = mysqli_query($conn, $locSql) or die("Bad SQL " . $locSql); 

    $opt = "<select name='selFieldLocation' id='fieldLocation' onChange='setSelectedLocName(this);'>";
    $opt .= '<option value="blank">Select Location</option>';
    while ($row = mysqli_fetch_assoc($locResultSet)) {
        $selectedLocID = $row['locationid']; 
        
        // This echo statement returns the value from the database, e.g.....
        // "SUN Men's Group"
        echo 'first row shortname ' . $row['shortname'] . "<br>";

        // Applied "addslashes" to that value.
        $thisShortName = addslashes($row['shortname']);

        // Echo the new value....
        // The result is: "SUN Men\'s Group" (which is what i was after)
        echo 'new shortname is ' . $thisShortName . "<br>";

        $opt .= "<option value='{$row['locationid']},$thisShortName'> {$row['name']} </option>\n"; 
        //echo 'row shortname ' . $thisShortName . "<br>";
    }
    $opt .= "</select>";
    $opt .= '<input type="hidden" name="selLocText" id="selLocText"  />';
    $opt .= '<input type="hidden" name="selLocShortName" id="selLocShortName"  />';

    var_dump($opt);

将返回:

$locSql = "select name, shortname, locationid from location_tbl l where ....";
    $locResultSet = mysqli_query($conn, $locSql) or die("Bad SQL " . $locSql); 

    $opt = "<select name='selFieldLocation' id='fieldLocation' onChange='setSelectedLocName(this);'>";
    $opt .= '<option value="blank">Select Location</option>';
    while ($row = mysqli_fetch_assoc($locResultSet)) {
        $selectedLocID = $row['locationid']; 
        
        // This echo statement returns the value from the database, e.g.....
        // "SUN Men's Group"
        echo 'first row shortname ' . $row['shortname'] . "<br>";

        // Applied "addslashes" to that value.
        $thisShortName = addslashes($row['shortname']);

        // Echo the new value....
        // The result is: "SUN Men\'s Group" (which is what i was after)
        echo 'new shortname is ' . $thisShortName . "<br>";

        $opt .= "<option value='{$row['locationid']},$thisShortName'> {$row['name']} </option>\n"; 
        //echo 'row shortname ' . $thisShortName . "<br>";
    }
    $opt .= "</select>";
    $opt .= '<input type="hidden" name="selLocText" id="selLocText"  />';
    $opt .= '<input type="hidden" name="selLocShortName" id="selLocShortName"  />';

    var_dump($opt);
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航