Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 14th, 2006, 06:40 AM
MP3 MP3 is offline
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default [How] Using java to get a result from a php file?

first of all, i'm sorry if its in the wrong forum

i created a sample php file contain the following code:

Code:
<?php 

require_once('./global.php'); 

$a = $db->query_first("SELECT bb FROM " . TABLE_PREFIX . "aa WHERE `id`='1'"); 


if ("$a"=="3") 
{ 
        $play = "<embed src=\"anything.wav\" autostart=true hidden=true></embed>"; 
} 
else 
{ 
        $play=""; 
} 
?>
Now, i want to create a java code (AJAX) to check the php file every 5 min. when there is a result then a small popup will show to the visitor.

Can anybody help me with the JAVA code?

Many thanks to whoever can assist.

(@)
 
Old July 14th, 2006, 07:21 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BTW there's a huge difference between Java and Javascript. The "J" in AJAX stands for Javascript. Java is primarily used on the server-side, much the same way PHP is used.

As for your answer, look at the Javascript setInterval() function. Here's an example that flips through a slide show every 5 seconds: http://www.devshed.com/c/a/JavaScrip...-JavaScript/5/

You can use basically the same code to do what you're asking. Just change the interval time from 5000 (5 seconds) to "5000*60" (5 minutes). Then in the slideShow() method, which you should rename, call window.open() to create your pop-up. There's lots of web sites out there that explain window.open() -- you should be able to figure it out very fast!!

Good luck!

Jon Emerson
http://www.digg.com/users/panaceaa/profile
 
Old July 15th, 2006, 04:20 AM
MP3 MP3 is offline
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are right about J = JavaScript.
I'm civil Engineer and I don’t know a lot about using JavaScript and for that reason I asked in this forum.

Okay, I saw the code at the link you posted above, but for instance if there was no result the popup will show and what's the benefit?

I want a function that will check the PHP file if the variable "$a" equal to 3 then the popup will show with the sound "anything.wav", else nothing will happen and the popup will not show.

Can you please give me a help about this because really I spent in this code 1 month trying and trying without any result.


(@)
 
Old July 16th, 2006, 03:14 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What's the point of the pop-up? Is it just to play the sound, or is there some UI you would like to show in it too? Would you prefer the sound to just play from the parent window, if possible?

Jon Emerson
http://www.digg.com/users/panaceaa/profile
 
Old July 16th, 2006, 03:47 PM
MP3 MP3 is offline
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you a lot for your reply
When the variable $a = 3, a popup window will show and the sound will play inside this popup with the ability of stopping this sound or play it again. Also, a text will show inside this popup window say the variable a = $a (where $a will get it from the php file and equal 3)

Okay, why I chose a popup? Because some visitors maybe they muted them speakers and they can't hear the sound, so they will see the popup and they will know there is a sound.



(@)
 
Old July 16th, 2006, 04:24 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm, I believe the only way to open the pop-up conditionally is to use XMLHttpRequest to fetch the conditional from the server, and if it's true, then open the pop-up. XMLHttpRequest allows you to make an asynchronous request -- when the response is received, an event is called with the response body stored in the event object. You can parse this response with an XML parser, or simply treat it as a true/false string.

I'm explaining this pretty poorly, but there's more information here:

http://developer.apple.com/internet/...mlhttpreq.html

:)

Jon Emerson
http://www.digg.com/users/panaceaa/profile
 
Old July 17th, 2006, 12:54 PM
MP3 MP3 is offline
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i added this code to my forum header and its didnt work:

Code:
<script language="JavaScript">

function loadXMLDoc(url) {
        req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
            try {
                        req = new XMLHttpRequest();
        } catch(e) {
                        req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
               try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
              } catch(e) {
                try {
                          req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                          req = false;
                }
                }
    }
        if(req) {
                req.onreadystatechange = processReqChange;
                req.open("GET", mp3.php, true);
                req.send("");
        }
}



function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
           alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}
</script>
no Alret show in the two case!!

where is the problem?

(@)
 
Old July 17th, 2006, 06:38 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Are you calling loadXMLDoc(url) with a setInterval? Also, you don't need a url parameter in that function if you're hardcoding the location inside the function. Thirdly, req.open("GET", mp3.php, true); should be req.open("GET", "mp3.php", true);, or similar.

Jon Emerson
http://www.jonemerson.net/
 
Old July 24th, 2006, 05:01 PM
MP3 MP3 is offline
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks panacea for your great help. Every thing working fine except the last point, showing the result.
For these lines:

if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
           alert("There was a problem retrieving the XML data:\n" +
                req.statusText);

Instead of :


           alert("There was a problem retrieving the XML data:\n" +
                req.statusText);


What can I write to show the result?
The result from the php file is:

echo" <SCRIPT>
window.open('mp3.php?do=play','','width=400,height =250,resizable=0,status=0,scrollbars=0,left=450,to p=450');
    </SCRIPT>";

Regards



(@)





Similar Threads
Thread Thread Starter Forum Replies Last Post
From .NET to PHP or JAVA redafarhat General .NET 1 April 23rd, 2008 12:36 PM
saving query result to xml file p2pMember SQL Server 2005 0 January 15th, 2008 03:37 AM
distorted result while calling a html file taraprasad Pro JSP 1 January 11th, 2008 04:06 AM
Article on Java and PHP interoperability ypomonh J2EE 0 August 2nd, 2007 05:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.