У меня есть код здесь, который я использую для изменения или замены изображений, когда мышь. Это Java-код:Использование javascript в php-скрипте
<script type="text/javascript" language="Javascript">
var imgdir = "category_icons/";
blank_icont = new Image;
blank_icont.src= imgdir+"blank_text.png";
vieworder_icon = new Image;
vieworder_icon.src = imgdir+"vieworder.png";
vieworder_icono = new Image;
vieworder_icono.src= imgdir+"vieworder2.png";
//vieworder_icont = new Image;
//vieworder_icont.src= imgdir+"vieworder.png";
function changeImages(name) {
document.images[name].src = eval(name + '_icono.src');
document.images.icon_text.src = eval(name + '_icont.src');
}
function changeImages2(name){
document.images[name].src = eval(name + '_icon.src');
document.images.icon_text.src = eval('blank_icont.src');
}
</script>
Внутри моего PHP скрипт у меня есть этот кусок кода:
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="10"><hr style="width: 60%;"></tr>
<tr>
<td width="30% valign="top" align="left"><img style="border: #66cc33 5px solid;" src="./menupictures/'. $picturepath . '" height="140" width="140" border="1"/></td>
<td width="40%" valign="top" align="left"> ' . $name . ' <br /> $' . $price . ' <br /><br /><a href="moreinfo.php?id=' . $id .'">More Info</a></td>
<td width="30% valign="top" align="left"><a onmouseover="changeImages("vieworder")" onmouseout="changeImages2("vieworder")" href="customize.php?id=' . $id .'"><img id="vieworder" src="category_icons
/vieworder.png" alt="" name="vieworder" border="0"></a></td>
Вы заметите, где я пытаюсь использовать функции changeImages
. Это не работает, потому что я не могу использовать javascript таким образом? Или у меня неправильные котировки? Я пытался изменить некоторые вещи, перемещая цитаты, но ничего не работает.
ЗДЕСЬ ПОЛНЫЙ КОД:
<html>
<head>
<script type="text/javascript" language="Javascript">
var imgdir = "category_icons/";
blank_icont = new Image;
blank_icont.src= imgdir+"blank_text.png";
vieworder_icon = new Image;
vieworder_icon.src = imgdir+"vieworder.png";
vieworder_icono = new Image;
vieworder_icono.src= imgdir+"vieworder2.png";
//vieworder_icont = new Image;
//vieworder_icont.src= imgdir+"vieworder.png";
function changeImages(name) {
document.images[name].src = eval(name + '_icono.src');
document.images.icon_text.src = eval(name + '_icont.src');
}
function changeImages2(name){
document.images[name].src = eval(name + '_icon.src');
document.images.icon_text.src = eval('blank_icont.src');
}
</script>
</head>
<body>
<!-- This is where the table section starts -->
<table><tr height="50"> </tr></table>
</body>
</html>
<?php
/* ----------------------------------- CONNECTING AND QUERYING THE DATABASE -----------------------*/
require("database.php"); //connect to the database
$start = (isset($_GET['start']) ? (int)$_GET['start'] : 0); //setting the get function for pagnation
$result = mysqli_query($con,"SELECT * FROM menuitem WHERE type='kids'LIMIT $start, 3"); //mysql statement. first argument $starat specifies the offset of the first row to return, and the second specifies the maximum number of rows to return
if (!$result) {
printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
exit();
}
echo '<table><tr height="20"></tr></table>
<table height="50" width="60%" align="center" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td width="300" align="left"><font style="font-weight:bold;">Menu Items</font></td>
<td width="400" align="left"><font style="font-weight:bold;">Menu Information</font></td>
<td width="300" align="left"><font style="font-weight:bold;">Add to Order</font></td>
</tr>
</table>';
/* ----------------------------------- PULLING IN THE DATA -----------------------*/
//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="10"><hr style="width: 60%;"></tr>
<tr>
<td width="30% valign="top" align="left"><img style="border: #66cc33 5px solid;" src="./menupictures/'. $picturepath . '" height="140" width="140" border="1"/></td>
<td width="40%" valign="top" align="left"> ' . $name . ' <br /> $' . $price . ' <br /><br /><a href="moreinfo.php?id=' . $id .'">More Info</a></td>
<td width="30% valign="top" align="left"><a onmouseover="changeImages("vieworder")" onmouseout="changeImages2("vieworder")" href="customize.php?id=' . $id .'"><img id="vieworder" src="category_icons
/vieworder.png" alt="" name="vieworder" border="0"></a></td>
</tr>
</table>';
echo $dynamiclist;
}
Почему у вас Eval в вашем JS? Вы НЕ должны этого делать. – mirosval