[当用户单击图像时,运行JS函数,还将值发布到php脚本中

问题描述 投票:0回答:1

我想知道我的代码中是否存在一种方法,用户可以单击图像,但是在此单击上获取图像的值并通过发布请求将其发送到PHP代码。例如,目前我有这个:

<div class="grid-2 dashboardIcons">
    <h3 class="fontAmaticH1">Mindfulness</h3>
    <a class="cursor" onclick="toggleWindow()"><img value="6" src="images/Mindfulness.png"></a>
</div>

我是否必须将其包装在如下所示的表单标签中?

<form method="post">
    <a class="cursor" onclick="toggleWindow()"><img value="6" name="module" src="images/Mindfulness.png"></a>
</form>

然后,我的PHP代码将使用该值并对其进行处理:

<?php
 // I need to get the value from the clicking on an image
    if(isset($_POST['module']))
{
 //Do something
}
?>

任何帮助,将不胜感激!

php
1个回答
0
投票

使用jQuery,您可以执行以下操作...

function toggleWindow() {
    $("#theForm").ajaxSubmit({url: 'server.php', type: 'post'})
}

0
投票

为此,您可以使用AJAX,您可以在其中使用onclick事件执行JavaScript代码。在您的JavaScript中,最后写下一个AJAX脚本,以便它将在执行JavaScript代码后执行php代码。

© www.soinside.com 2019 - 2024. All rights reserved.