tags:

views:

52

answers:

2

I have an issue going on here. I am using PHP to get values from a database in a php script. What I would ideally like to do is test to see if there is any data to display server side and if there is, pop up a javascript window with the values.

I have this working right now with javascript. Currently, the user can look up data and presses a submit button with an onclick event attached to it that opens the javascript window. I'm also using getemementbyID to grab the value posted in the parent window that gets passed to the child window. Basically, the php script is getting bypassed so I can't really do any checks of the data. Bottom line... I want to check to see if data is present BEFORE the window opens, if possible.

Any ideas?

A: 

your best option is to either make a request via ajax to check for data or make the request on the initial page load so you know already and can pass this to your click handler.

edit - This is an example using prototype (untested):

$('button').observe("click",clickCheck);    

function clickCheck(){
 new Ajax.Request("/remote/url", {
  method: 'get',
  onSuccess: function(transport) {
    if (transport.responseText == 'results returned'){
      // launch popup
    }else{
      // dont launch popup
    }
  }
 });
}
seengee
Thanks seengee. I have tried this a dozen ways. I also had this idea and is what will work best for me but I have no idea how to pass to the click handler. Basically, I want to test the return value from the database and then if there is a result, pop the window. If nothing exists then show a message. Can you maybe provide an example of your second solution?
Thanks seengee. This gives me a starting point for sure. I'll credit you for the effort. :)
A: 

I suggest using jQuery for making an ajax request. Something like:

$("#my-button").click(function(){
  $.get('page_with_the_data.php', function(data, textStatus){
    if (data) {
      // Open the pop-up
      // ..or just: alert(data)
    } else {
      // empty page
      alert("No data returned");
    }
  })
})
redShadow