我需要將這些值傳遞給頭部的javascript函數。問題是,當從數據庫中提取一些不止一個單詞時,使用href在使用javascript:function(vars)傳遞時將其切斷,我無法使用onclick工作。我已經嘗試過圍繞變量(「」,「',/」和{)的引號和括號的各種組合,並且在我通過傳遞之前將會有不止一個單詞的引號(見下文)這裏是PHP:將PHP變量傳遞給帶有引號的javascript函數
<?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();
?>
而且它要在頭功能:
<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>
這是一個使用頁面加載()在主索引頁我有問題與對話框,但它甚至沒有達到這一點,所以下一步。如果需要知道,所有jquery包括在主索引頁面上。load()將進入索引頁上的div,並且這可以工作目前爲止,罰款爲
是的,我已經做到了。就像我的OP所說的那樣,它不一定是它傳遞變量的整個字符串的實際傳遞。它切斷。我是否應該將鏈接指向此頁面的頂部以供您重新閱讀? – Grant 2012-07-29 23:19:15
很高興看到有人*最後*使用'MySQLi'而不是'mysql'! – 2012-07-29 23:25:52