button点击是client side而PHP是server side ,但你可以通过使用ajax来实现
$('.button').click(function() { $.ajax({ type: "POST", url: "some.php", data: { name: "John" } }).done(function( msg ) { alert( "Data Saved: " + msg ); }); });
在你的php文件中:
是的,你需要在这里的Ajax。 有关更多详细信息,请参阅下面的代码。
像这样改变你的标记
jQuery的: –
$(document).ready(function(){ $('.button').click(function(){ var clickBtnValue = $(this).val(); var ajaxurl = 'ajax.php', data = {'action': clickBtnValue}; $.post(ajaxurl, data, function (response) { // Response div goes here. alert("action performed successfully"); }); }); });
在ajax.php
你不能像调用HTML中的button那样调用php函数。 因为HTML在客户端而PHP在服务器端运行。
要么你需要使用一些Ajax或像下面的代码片段那样做。
<?php if($_GET){ if(isset($_GET['insert'])){ insert(); }elseif(isset($_GET['select']