Javascript 过早执行超链接上的onclick事件
我想能够选择一个最喜欢的产品从购物车。 这是我的代码(来自修改过的IBKCart):Javascript 过早执行超链接上的onclick事件,javascript,php,html,Javascript,Php,Html,我想能够选择一个最喜欢的产品从购物车。 这是我的代码(来自修改过的IBKCart): 您混淆了服务器端和客户端事件。您正在使用的onclick事件是一个javascript(客户端)事件,将在用户单击元素时执行,但由于您从PHP(服务器端)输出字符串$Out,并且您将字符串常量的值与updFavourites函数连接在一起,PHP执行服务器端代码,然后给出客户端代码,即评估的PHP函数。发布呈现的HTML。您还没有理解服务器端PHP代码和客户端JavaScript代码之间的区别……当然,updF
您混淆了服务器端和客户端事件。您正在使用的onclick事件是一个javascript(客户端)事件,将在用户单击元素时执行,但由于您从PHP(服务器端)输出字符串$Out,并且您将字符串常量的值与updFavourites
函数连接在一起,PHP执行服务器端代码,然后给出客户端代码,即评估的PHP函数。发布呈现的HTML。您还没有理解服务器端PHP代码和客户端JavaScript代码之间的区别……当然,updFavourites
在那里执行,因为它是一个PHP函数。PHP早就完成了它的工作,甚至在页面到达客户端之前。正如我对Bikonja所说的,updFavourites
依赖于会话变量favorites
,它只由onclick evernt设置,所以我看不出这是如何发生的。我当然明白你的意思,但是为什么函数updFavourites()
接到电话。代码中只有一个对它的调用,这似乎取决于onclick事件。这可以解释吗?@Geoff它不应该依赖于onclick事件。连接updFavourites
函数的PHP变量始终在foreach
循环的迭代中调用。您是否可能没有输入foreach的
?PHP不知道onclick是什么,所以它不依赖于它。当您认为onclick
未被“触发”时,尝试查看是否正在调用foreach
。我从以下问题获得了此onclick事件的代码:.*html标记的onClick属性只接受Javascript而不接受PHP代码。但是,通过使用JS document.write()函数,您可以轻松地从Javascript代码中调用PHP函数-通过“编写”其对浏览器窗口的调用来有效地调用PHP函数**答案写得不好。使用document.write(“”)
只是让您可以将经过评估的PHP代码添加到页面onclick
,因此,虽然它确实会将对HTML页面的写入延迟到onclick
事件,但它不会延迟对将放入文档中的代码进行PHP评估。write
部分。从javascriptonclick
调用PHP函数的唯一方法是使用AJAX。
<?php
function ShowCartWtPrd(){
global $fav_id;
$itemClass = ($this->getCartItems() > 2) ? "insideContMainArea2": "insideContMainArea";
$plural = ($this->getCartItems() > 1)? 's': '' ;
if (isset($_SESSION['favourites']) && !$_SESSION['favourites'] == "False") {
$this->updFavourites();
unset($_SESSION['favourites']);
}
$Out =
'<div id="theOverlay">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td width="20%"><div align="right"><img name="" src="./imgs/wait.gif" width="32" height="32" alt="" /></div></td>
<td width="50%"><div align="left"> <strong>Updating...Please Wait</strong> </div></td>
</tr>
</table>
</div>
<div id="Cart_Container">
<div id="Car_top_bg"></div>
<div id="Cart_middle">
<div id="'. $itemClass. '">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr bgcolor="#E6EDD2">
<td width="55%" height="21" bgcolor="#E6EDD2"><div align="center"><strong>Item Description </strong>
</div>
</td>
<td width="11%"><div align="center"><strong>Qty</strong></div></td>
<td width="11%"><div align="center"><strong>Prc/Qty</strong></div></td>
<td width="11%"><div align="center"><strong>Fav</strong></div></td>
<td width="11%"><div align="center"><strong>Del</strong></div></td>
</tr>' ;
$cnt=1;
foreach($this->cart as $itemId=>$Qty){
++$cnt;
$ans = fmod($cnt,2);
if($ans == 0){$bg = '#ECF9FF';}else{$bg='';};
$ProdDts = $this->getProdDts($itemId);
$this->totAmt += $ProdDts[$this->prodPrc] * $Qty ;
$fav_id = $ProdDts[$this->prod_id];
$Out .= '<tr bgcolor="'. $bg .'">
<td valign="top">
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="60%" valign="top">
' . $ProdDts[$this->prodNm] .'
<br />
</td>
</tr>
</table>
</td>
<td valign="top"><div align="center">' . $Qty .'</div></td>
<td valign="top">' . $ProdDts[$this->prodPrc] .'</td>
<td valign="top">
<div align="center">
<a href="#?favourites=true">
<img src="./imgs/fav_trnsp_icon.png"
alt="Add to favourites" width="16" height="16" border="0"
title="Add to favourites" name="favourites"
onclick="document.write('. $this->updFavourites() .');"
/>
</a>
</div>
</td>
<td valign="top">
<div align="center">
<a href="#"
onclick="doCart(\'DelItem\', \'' . $ProdDts[$this->prodId] . '\', 0, \' \', \'Small\');">
<img src="./imgs/cart_remove.png" alt="Delete Item" width="16" height="16" border="0" title="Delete Item"/></a>
</div>
</td>
</tr>
<tr>
<td colspan="4" valign="top"><div class="clear"></div></td>
</tr>';
}
$Out .= '
</table>
</div>
</div>
<div id="Cart_gtotal">
<div class="theCont">Grand Total => '. $this->ShowCurSymbol() .' '. $this->toMoney($this->totAmt) . '</div>
</div>
</div>' ;
echo $Out;
}
function updFavourites() {
global $username;
global $password;
global $database;
global $fav_id;
$conn = mysqli_connect("localhost",$username,$password, $database);
// Check connection
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// mysql_connect("localhost","$username","$password") or die("Error: ".mysqlerror());
// mysql_select_db("$database");
if (isset($_SESSION['fav_id'])) {
$fav_id = $_SESSION['fav_id'];
}
$dealer_id = $_SESSION['dealer_id'];
$ProdDts = $this->getProdDts($fav_id);
$part_id = $ProdDts[$this->prod_id];
$sql = "INSERT INTO favourites VALUES (0,'$dealer_id','$part_id')";
$result = $conn->query($sql) or exit("Error code ({$conn->errno}): {$conn->error}");
/* close up */
//$conn->close();
}
?>
}
$Out .= '<tr bgcolor="'. $bg .'">