0

所以这是我的文档的 html 列表:

<form action="submitPage.html" method="post" onsubmit="submit();">
<img id="img1" src="inventory_images/berg.jpg" onclick="changeColor(this, 1);" />
<img id="img2" src="inventory_images/berg.jpg" onclick="changeColor(this, 2);" />

和 JavaScript 代码,用于:

<script>
markedImage = 0; // The variable used for the submit;
function unmarkAll() {   
document.getElementById("img1").style.borderColor = "#000000";
document.getElementById("img2").style.borderColor = "#000000";
}
function changeColor(obj, markedId) {
unmarkAll();
obj.style.borderColor  ="#00FF00";
markedImage = markedId;
}
</script>
<script>
function submit()
{
    document.getElementById("markedImage").value = markedImage;
}
</script>

因此,用户可以选择一张图片并单击“提交”。然后他将被引导到“submitPage.html”:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
if ($_SERVER['REQUEST_METHOD'] = "POST") {
    echo "Variables coming<br />";
} else {
    die("No Post Variables");
}
if ($_POST['markedImage'] = "POST") {
echo "Picture has been chosen<br />";
echo "This one:<br />";
$picture_id = $_POST['markedImage'];
echo $picture_id;
} else {
    echo "FAIL";
}
?>

我只想显示所选图像的“id”。(显示“img1”或“img2”)我应该改变什么?问候

4

1 回答 1

0

代替

<form action="submitPage.html" method="post" onsubmit="submit();">
<img id="img1" src="inventory_images/berg.jpg" onclick="changeColor(this, 1);" />
<img id="img2" src="inventory_images/berg.jpg" onclick="changeColor(this, 2);" />

尝试

<form action="submitPage.html" method="post" onsubmit="submit();">
<img id="img1" src="inventory_images/berg.jpg" onclick="changeColor(this.id, 1);" />
<img id="img2" src="inventory_images/berg.jpg" onclick="changeColor(this.id, 2);" />

所以markedId实际上是图像ID。

于 2013-05-21T11:16:23.493 回答