Run PHP code if row is displayed using jQuery [closed]
I have PHP code in an id element. The row can be displayed using id in jQuer开发者_开发问答y. If the row is hidden I want PHP code not to run because I am creating a report based on user selection. The problem is that the PHP code always runs whether the row is displayed or hidden. Also the PHP is displaying data from my database so I am compelled to use it.
I may just be reading your question incorrectly, but it sounds like you'd like to use jQuery to trigger PHP code once it's finished rendering on the server. This is possible, but maybe not as you think.
PHP's job is done when it generates HTML (or performs other actions), and passes data to the browser. Once the browser has this data (usually just static HTML), then JavaScript's job begins. JavaScript isn't able to "reach back" and call functions in PHP at this point, because the PHP has already finished executing. However, there is another way.
The best way to call PHP code using JavaScript is by using an XMLHttpRequest (XHR). jQuery provides a great method for doing this. Using a slightly modified example from the document I linked to:
$.ajax({
url: 'dbCall.php?arg1=foo&arg2=bar',
success: function(data) {
$('.result').html(data);
alert('Load was performed.');
}
});
This code would call the file dbCall.php
— which could be passed arguments via GET (arg1=foo&arg2=bar
) — and then wait for its results. Once the results arrive the success
callback is fired, which replaces the contents of all elements with class result
using the data from the dbCall.php
script. An alert
call then pops a dialogue up on the screen letting you know this has happened successfully.
精彩评论