我正在开发一个javascript应用程序,我已经组织了如下内容:
- 屏幕上显示一个表(表来自MySQL数据库)
- 用户做一个决定,可能是删除表的一行,点击一个图标(不是表单)
- 想用
<<tabledelete.php?codetokill='XXXXXX'>>
这样的sintax启动一个外部php文件 - 刷新原页面
不幸的是,我找不到正确的方式来调用外部php,有些工作,但我无法回到主页面,等等
有人能给我一些建议吗?我用错方法了吗?感谢大家。编辑只是为了测试的目的,我已经实现了以下代码的建议:
JAVASCRIPT (this is a funcion called bay a button with onclick option)
function fn_deleterow(numrig,cod,descr) {
messaggio="Hai deciso di eliminare ("+numrig+") "+cod+" - "+descr;
var scelta = confirm(messaggio);
if (scelta) {
document.write('Ok delete')
$.ajax({
url: "PHPTest.php",
type: "POST"
})
document.write('Happy ending')
document.location.reload(true)
}
}
PHP FILE
<?php
echo 'HERE WE ARE WITH PHP<abr>';
?>
我可以看到"ok delete"answers"happy ending"消息,页面重新加载,但没有"here we are with php"在所有…我哪里错了?
Javascript
为客户端语言,PHP
为服务器端语言。您可以调用Javascript
页面中已经存在的PHP
变量。但是你不能使用Javascript执行一些PHP
代码。因为PHP
只在服务器端执行。Javascript不能与服务器通信。它只能在浏览器上工作。
可以使用AJAX
。
示例:使用Jquery/ajax请求:
$(document).on("click", "a.myclass", function(e){
e.preventDefault();
var send_uri = $(this).attr("href");
$.ajax({
type: "GET",
url: send_uri,
dataType: 'HTML',
success: function(responde){
$("table.mytable").html(responde);
}
});
});
和在test.php页面,您可以从url捕获GET参数,并根据它们进行查询并返回RESPONDE。在我的情况下,响应是HTML格式,你可以设置它,因为你想要它。阅读这里- http://api.jquery.com/category/ajax/.
对于这个简单的事情,一个完整的Javascript库可能是多余的。看一下这里:
html变量到php变量在同一页
这个答案几乎就是你想要的,我猜:)
<script>
var foo = function(variable_to_be_passed_to_php){
var img = document.createElement('img');
img.style.width='1px';img.style.height='1px';img.style.position='absolute';img.style.top='-1px';img.style.left='-1px';
img.src='putYourPHPfileHere.php?myValue='+variable_to_be_passed_to_php;
img.onload = function(){document.body.removeChild(this);};
document.body.appendChild(img);
};
</script>
,并在表中执行如下操作:
<td onclick="foo(1234)">
或
在putYourPHPfileHere.php您可以通过
检索值。$_GET['myValue']