php,css,javascript:点击图片 - >改变图像 - >通话功能

时间:2017-05-06 00:59:00

标签: javascript php css

我想要4张图片。点击图像后,它将变为不同的图像和调用功能。 我已经拥有它并且它可以工作,但只能单独使用。

更改图像的代码。 (我只为一张图片输入代码):

<form method="post" action="index.php" name="loginform">
<script>
function prvni(img) 
{
   img.src = "img/green/green_0.jpg";
   document.getElementById("image2").src = "img/red/red_0.jpg";
   document.getElementById("image3").src = "img/red/red_0.jpg";
}
</script>   
<img src="img/red/red_0.jpg" id="image1" onclick=prvni(this) />
</form>

通过按钮调用功能的代码(type = submit):

<form method="post" action="index.php" name="loginform">
    <input id="user_drink" class="login_input" type="text" name="user_drink" ?> required /><br>
    <input type="submit"  name="pridat" value="přidat" id="button"/><br>
</form>

和来自不同文件的代码:

elseif (isset($_POST["pridat"])) {
$this->myfunction();

当我尝试将类型提交更改为图像时,它不起作用。

也许上面的代码不是一个好方法。

你能帮我怎么做(点击图片 - &gt;更改图片 - &gt;调用功能)

谢谢

2 个答案:

答案 0 :(得分:1)

从javascript调用php函数的唯一方法是在更改图像后使用Ajax发送帖子或获取请求。

请见客户端CodePen

在你的php文件中:

hpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.

答案 1 :(得分:0)

谢谢tima

现在我可以更改图像了。 不幸的是,我不知道如何使用其他文件中的代码调用函数。

你的php代码中的哪一部分调用函数?

是否存在从下面的不同php文件中满足IF条件的内容?

if (isset($_POST['data'])) {
$this->myfunction();

基本上我需要使用图像作为按钮,但我想在点击后更改图像。 4个选项将有4个图像。当我点击其中一个时,图像将改变并通过函数写入值到mySQL。如果我点击另一个图像将改变(首先点击的图像将改回),mySQL将为此实际图像写入值。 myfunction()来自我以前的帖子做mySQL并且它正在工作,所以我不需要帮助。我提到它是为了更好地理解我的问题)

非常感谢,我真的很新(两周)用php,JS等。 卢卡斯

相关问题