如何从Javascript执行外部PHP?

时间:2021-06-16 09:50:39

I'm working on a javascript application, and i've organized things as follow:

我正在开发一个javascript应用程序,我已经组织了以下内容:

  • a table is displayed on screen (table is coming from a MySQL database)
  • 屏幕上显示一个表(表来自MySQL数据库)

  • user take a decision, may be to delete a row of the table, clicking on an icon (NOT A FORM)
  • 用户做出决定,可能是删除表格的一行,点击图标(不是表格)

  • want to launch an external php file with a sintax like <<tabledelete.php?codetokill='XXXXXX'>>
  • 想要启动一个带有sintax的外部php文件,例如<< tabledelete.php?codetokill ='XXXXXX'>>

  • refresh the original page
  • 刷新原始页面

Unfortunately i can't find the right way to call the external php, something works but i'm not able to get back to the main page, and so on

不幸的是我无法找到调用外部php的正确方法,有些工作但我无法返回主页,依此类推

can someone please give me some suggestion? Am I using a wrong approach? TY to all.

有人可以给我一些建议吗?我使用了错误的方法吗? TY给所有人。

EDIT Just for testing pourpose i've implemented following code as suggested:

编辑只是为了测试pourpose我已经按照建议实现了以下代码:

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>';
?>

I can see the "ok delete" and the "happy ending" messages, and the page reloads, but no "here we are with php" at all... where am i wrong?

我可以看到“确定删除”和“快乐结局”消息,并且页面重新加载,但没有“这里我们用PHP”...我错在哪里?

3 个解决方案

#1


0  

A full Javascript library for this simple thing might be a total overkill. Have a look at this over here:

这个简单的东西的完整Javascript库可能是一个完全矫枉过正。在这看看这个:

html variable to php variable in same page

html变量到同一页面的php变量

This answer pretty much is what you are looking for I guess :)

这个答案几乎就是你在寻找我猜:)

<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>

and in your table do something like:

并在你的表中做类似的事情:

<td onclick="foo(1234)">

or

in the putYourPHPfileHere.php you can retrieve the value by

在putYourPHPfileHere.php中,您可以通过检索值

$_GET['myValue']

#2


1  

Javascript is client side language where PHP is server side. You can call PHP variables already in the page in Javascript. But you cant execute some PHP code using Javascript. Because PHP is executed in server only. Javascript cant communicate with Server. It can only work with browser.

Javascript是客户端语言,其中PHP是服务器端。您可以在Javascript中调用页面中已有的PHP变量。但你不能使用Javascript执行一些PHP代码。因为PHP仅在服务器中执行。 Javascript无法与Server通信。它只能与浏览器一起使用。

You can use AJAX for this purpose.

您可以将AJAX用于此目的。

#3


0  

For example: With Jquery / ajax request:

例如:使用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);
        }
    });     
});

and at your test.php page you can catch GET params from the url and depending on them to make your queries and return the RESPONDE. In my case the responde is in HTML format, you can set it as you want it .. read about that here - http://api.jquery.com/category/ajax/.

在您的test.php页面上,您可以从URL中捕获GET参数,并根据它们进行查询并返回RESPONDE。在我的情况下,response是HTML格式,你可以根据需要设置它..在这里阅读 - http://api.jquery.com/category/ajax/。

#1


0  

A full Javascript library for this simple thing might be a total overkill. Have a look at this over here:

这个简单的东西的完整Javascript库可能是一个完全矫枉过正。在这看看这个:

html variable to php variable in same page

html变量到同一页面的php变量

This answer pretty much is what you are looking for I guess :)

这个答案几乎就是你在寻找我猜:)

<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>

and in your table do something like:

并在你的表中做类似的事情:

<td onclick="foo(1234)">

or

in the putYourPHPfileHere.php you can retrieve the value by

在putYourPHPfileHere.php中,您可以通过检索值

$_GET['myValue']

#2


1  

Javascript is client side language where PHP is server side. You can call PHP variables already in the page in Javascript. But you cant execute some PHP code using Javascript. Because PHP is executed in server only. Javascript cant communicate with Server. It can only work with browser.

Javascript是客户端语言,其中PHP是服务器端。您可以在Javascript中调用页面中已有的PHP变量。但你不能使用Javascript执行一些PHP代码。因为PHP仅在服务器中执行。 Javascript无法与Server通信。它只能与浏览器一起使用。

You can use AJAX for this purpose.

您可以将AJAX用于此目的。

#3


0  

For example: With Jquery / ajax request:

例如:使用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);
        }
    });     
});

and at your test.php page you can catch GET params from the url and depending on them to make your queries and return the RESPONDE. In my case the responde is in HTML format, you can set it as you want it .. read about that here - http://api.jquery.com/category/ajax/.

在您的test.php页面上,您可以从URL中捕获GET参数,并根据它们进行查询并返回RESPONDE。在我的情况下,response是HTML格式,你可以根据需要设置它..在这里阅读 - http://api.jquery.com/category/ajax/。