Hi guys,
I'm trying to write code where an ajax function, at a specified interval, calls php script to query a table and open a certian window according to the response(variable) received. After that, the ajax function will continue to call the same php script but will open another window only if the response or variable is different from the previous request(s). What kind of method should I use to achieve this?

The part about being different from before, can't be (easily) done through php, you'll need to do it through javascript (shouldn't be too hard). (HTTP is stateless).

I'd also just have PHP generate a window open string, which you can put in a window.open() statement if an response is received.

You also could eval() the response, and respond with javascript.

Another option, would be to use a modal window (thickbox), and have it pull up the data to show right then (find the url, fopen (local only!), curl it into the modal window, or offer a redirect for the window).

The part about being different from before, can't be (easily) done through php, you'll need to do it through javascript (shouldn't be too hard). (HTTP is stateless).

I'd also just have PHP generate a window open string, which you can put in a window.open() statement if an response is received.

You also could eval() the response, and respond with javascript.

Another option, would be to use a modal window (thickbox), and have it pull up the data to show right then (find the url, fopen (local only!), curl it into the modal window, or offer a redirect for the window).

Thanks, Codejoust

I tried the eval() function that you recommended to execute an open() function in the form of php string received as a response from ajax request but I couldn't get it to work. I wondered if it is because of the eval() function that I used call for a string inside a DOM like this eval("document.getElementById('txt').innerHTML");. Any ideas?

Anatta,

As I see it ......

The question you need to answer is : Where are you going to store the responses such that you can avoid repeats?

To help answer that, you need first to ask what scope should the "response-store" have? My main choices would be :

  • Scope=page - store client-side in Javascript.
  • Scope=session - store server-side in php session or database.
  • Scope=multi-session - store client-side in cookie or server-side in database.

It's then up to you exactly what you choose to store.

You shouldn't need to use eval(), which is pretty well frowned on in JS circles. An AJAX response should contain data not code. There may be exceptional circumstances but I don't think this is one.

Airshow

Thanks, Airshow

Your suggestion tells me that I need to read up more on these subjects. In the mean time, could you please point me to the right direction on what are the things I need to learn and where the resources are in order to build an intrasite one-on-one instant messaging system where logged in members can send and receive sms. I have been searching for tutorials about this for quite sometimes but most of them are chat type which is not exactly what I have in mind. I am also aware that there are many IM scripts available for download but I want to build it by myself. And, It's been very frustrating because my php knowledge is very limited and even more so with javascript/ajax. So, suggestions on specific techniques or methods required to get the job done would be really helpful.

OK, you've described something that I have never attempted, though I can envisage the various elements you would need - but only for a thin client (browser) interface. My experience outside thin-cliient is limited.

Firstly, you will need the agreement and cooperation of your IT Department at a high level because your application will (a) require the installation of some non-standard software on your company's server(s) and (b) a commercial agreement with a telecom provider (or a third party intermediary) for the gateway to a public voice/SMS network. Both are likely to involve some cost - probably not a lot but you must go into this with your eyes open.

Next, you will need to choose a server-side language and you will be probably governed by what is already available on your intranet server(s). For INTERnet, the natural choice would be PHP, main reasons being that php is capable, well documented and a plethora of third-party php libs exist (some free, some at a price) for all sorts of special functionality (including send/receive SMS). PHP hosting is very competitive, though for intranet that should not be an issue. Other contenders would be ASP, JSP, Perl - and my comments on PHP apply to these to a greater or lesser extent.

Then, for client-side programming the choice is automatic - Javascript - no point considering anything else. You could program everything in raw Javascript but a library/toolkit/framework such as JQuery, Prototype or Yahoo's YUI would make life easier in the long term. With these libs, cross-browser compatibility is near 100% and the amount of code you need to write is significantly less. On the downside the few lines of code you do write will be far from simple; you would need to invest some time in learning the chosen lib's syntax. JQuery, Prototype and YUI are all very well documented - as are several other offerings. Search the web for "javascript libraries compared" for several good comparisons of these libs and others.

There are many other ways tyo go. I'm sure others will offer different advice - maybe better?.

Good luck.

Airshow

Thanks again, Airshow

I will start doing research on the subjects you recommended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.