0

I have a TreeView in my php file and i want to get selected tree value in my php code to for further process. I am able to see my selected value in alert box but not receiving in php script. I am using zTree. Any help! Thanks in advance

here's my js function

function showLog(str)  
    {
     alert(str); // str variable as Selected Value in alert box
     // i want this str value in php code. 
    }
ammarali29
  • 63
  • 2
  • 11

2 Answers2

1

JavaScript runs in the browser, and PHP runs on the server, so the two can't interact directly. You have to send an HTTP request to the server, with the string as part of the request body (i.e. as a parameter).

This can be done by putting the string into a form field and submitting the form (which causes the browser to load a whole new page from the server), or you can use AJAX to make your JavaScript code send a request to the server "invisibly", without making the browser navigate to a new page.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • Yes solution one worked! but i want to do it with ajax. Could you suggest me some basic ajax tutorial to start. – ammarali29 Sep 18 '14 at 07:28
0

This isn't possible without restrictions:

PHP is server based and will only be executed on HTTP Request. This means, only, if you load a site, php is executed. All, what will remain after Request is simple HTML with objects like CSS Stylesheet and JavaScript functionality.

If you want to work with the selection a user makes, you can use Ajax (jQuery offers a very simple API for that), that will load php scripts from JavaScript. So you can send your selected element to a php script which will for example store it in a database.

Kind regards!

Lukas Fink
  • 627
  • 6
  • 18