duangu4980 2012-07-29 23:13
浏览 59
已采纳

使用引号将PHP变量传递给javascript函数

I need to pass these values to a javascript function in the head. The problem is when pulling from the DB some are more than one word, and using a href it cuts it off at a space when passing using javascript:function(vars), and I can't get onclick to work at all. I've tried all sorts of combinations of quotes and brackets around the variables ("",'',/", and {) as well as putting quotes around the ones I know will have more than one word before passing (see below). Here is the PHP:

<?php
    require('config/dbconfig.php');
    $query = "SELECT * FROM news ORDER BY id DESC LIMIT 4";
    if ($stmt = $mysqli->prepare($query)) {
        /* execute statement */
        $stmt->execute();

        /* bind result variables */
        $stmt->bind_result($idn, $titlen, $categoryn, $descn, $postdaten, $authorn);

        /* fetch values */
        while ($stmt->fetch()) {
            //echo 'id: '. $id .' title: '. $title;
            echo "<table border='0'>";
            $shortDescLengthn = strlen($descn);
            if ($shortDescLengthn > 106) {
                $sDCutn = 106 - $shortDescLengthn;
                $shortDescn = substr($descn, 0, $sDCutn);
            } else {
                $shortDescn = $descn;
            }
                        $titlenQuote = "'". $titlen ."'";
                        $descnQuote = "'". $descn ."'";
                        $authornQuote = "'". $authorn ."'";
            echo "
                <h1>". $titlen ."</h1>
                <tr><td>". $shortDescn ."...</td></tr>
                <tr><td><a href='javascript:return false;' onclick='readMore(". $idn .",". $titlenQuote .",". $categoryn .",
                            ". $descnQuote .",". $postdaten .",". $authornQuote .")'>Read More</a></td></tr>
                <tr><td>Written by: " . $authorn . "</td></tr>
                <tr><td><img src='images/hardcore-games-newsbar-border.png' width='470px' /></td></tr>
            ";
        }
        echo "</table><br />";

        /* close statement */
        $stmt->close();
    }

    /* close connection */
    $mysqli->close();
?>

And the function it's going to in the head:

<script type="text/javascript">
    function readMore(id,title,cat,desc,post,auth) {
        alert(id +","+ title +","+ cat +","+ desc +","+ post +","+ auth);
        var $dialog = $('<div></div>').html('This dialog will show every time!').dialog({autoOpen: false,title: 'Basic Dialog'});
        $dialog.dialog('open');
        $dialog.title = title;
        $dialog.html(desc);
    }
</script>

This is a page using load() in the main index page. I've had issues with dialog too, but it's not even hitting that point yet so thats the next step. In case need to know, all jquery includes are on the main index page. The load() is going into a div on the index page, and this works fine so far.

  • 写回答

3条回答 默认 最新

  • drsfgwuw61488 2012-07-29 23:17
    关注

    You're using the same type of quote for both the attribute value delimiter and the JavaScript string delimiter; don't do that. Also, json_encode the values instead of just wrapping them in quotes, like so:

    echo "
        <h1>". $titlen ."</h1>
        <tr><td>". $shortDescn ."...</td></tr>
        <tr><td><a href='javascript:return false;' onclick='readMore(". $idn .",". json_encode($titlen) .",". json_encode($categoryn) .",
        ". json_encode($descn) .",". json_encode($postdaten) .",". json_encode($authorn) .")'>Read More</a></td></tr>
        <tr><td>Written by: " . $authorn . "</td></tr>
        <tr><td><img src='images/hardcore-games-newsbar-border.png' width='470px' /></td></tr>
    ";
    

    … and you'll also want to HTML-encode them.

    echo "<h1>$titlen</h1>";
    echo "<tr><td>$shortDescn...</td></tr>";
    echo '<tr><td><a href="javascript:return false;" onclick="'
        . 'readMore(' . $idn . ',' . htmlspecialchars(json_encode($titlen)) . ','
        . htmlspecialchars(json_encode($categoryn)) . ','
        . htmlspecialchars(json_encode($descn)) . ',' . htmlspecialchars(json_encode($postdaten)) . ','
        . htmlspecialchars(json_encode($authorn)) . ')">Read More</a></td></tr>';
    echo "<tr><td>Written by: $authorn</td></tr>";
    echo '<tr><td><img src="images/hardcore-games-newsbar-border.png" width="470px" /></td></tr>';
    

    ProTip™: <tr> can't be directly after <h1>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误