PHP and showing user input instantly on screen

Hi,

I want to use both PHP to call a database and I want the user input to appear on screen instantly (without reloading the page).

For example if they were buying a car

So if the users types in deposit: 1000

And the user selects: BMW car

I need the price of the BMW car (in the database) minus the deposit to reveal the amount outstanding. This is just an example of kind of use I need to achieve.

How do I access the database without refreshing the page? I have no idea how it is done. PHP is the only way to access a database but I thought the page would need refreshing to get data from the database?

Hope this is possible.

Matt.

Hi Matthew,

this could be done by using AJAX technology and an API backend on your server. AJAX can be used when the user types in a number to call your (PHP) API. Your API would then get the price from the database and return this number to the AJAX called page. The AJAX technology is very useful for this kind of stuff!

In general, on the client side a new connection is opened with Javascript. This connection is used to send data to a PHP page (your API). And the response data can be received, too. If you use AJAX, you do not need to reload a page to get new data from a server!

Thanks.

I looked up AJAX and found this site which mentions JavaScript too:

http://openenergymonitor.org/emon/node/107

Can this be done without JavaScript, as I have never written Java before?

Matt.

I don’t think so. AJAX is Asynchronous JavaScript and XHTML (or something like that) - the JavaScript on your page is what causes it to call the server-side routine to search the database. Basically you trap the “onblur” event from your selection (i.e. when the user tabs away having chosen “BMW”) to call the short server routine which then retrieves the data and displays it as required.

The JavaScript doesn’t need to be overly complicated - the actual connection code is widely available and after that it’s simply a case of hooking onto the appropriate event in your page (often losing focus from a control, or clicking a button), getting the data from the form, calling the server routine, and inserting the results into the document. I haven’t got a lot of JavaScript knowledge but have managed to get it working using some of the many tutorials kicking around the web.

Javascript also has nothing to do with Java, other than they are both programming languages. If you learn Javascript, you are learning Javascript, not Java. And, learning Javascript is something you should also do. No web developer should be without it, as it is what mostly controls browser behavior, including gathering data inconspicuously from a web server in the background, so the user doesn’t experience a page refresh.

Scott

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.