1

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

  • a table is displayed on screen (table is coming from a MySQL database)
  • 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'>>
  • 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

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

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

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?

1
  • 2
    simple use ajax to call a php page from javascript.. Commented Mar 21, 2013 at 7:57

3 Answers 3

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.

You can use AJAX for this purpose.

Sign up to request clarification or add additional context in comments.

Comments

1

For example: With Jquery / ajax request:

$(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/.

1 Comment

Just for testing pourpose i've implemented following lines:
0

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

html variable to php variable in same page

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

$_GET['myValue']

3 Comments

well, yes and no;1st of all thank you, to be honest is REALLY what i need, to pass a variable to local php, but i don't want to use a form... so i was thinking to build an external php functions library to call with parameters... totally stuck here...
@RobertoAlfieri That is why I suggested this answer. But I guess I need to know your HTML markup, so I can send you a working example :)
i've placed some code above, so it could be helpful... TY very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.