 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
04-04-2009, 05:03 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: May 2008
Location: Oporto-Portugal
Posts: 24
Thanks: 11
|
passing a java variable to php
Hello dear colleagues from the TalkPHP forum,
I often use the "alert" java function in the middle of PHP programs in order to display variables on screen as shown below.
PHP Code:
echo("<script>window.alert('var= ".$var."')</script>");
Now I wanted to use the "confirm" javascript function inside of a "for" cycle, so that if I click the OK the cycle should keep going on otherwise if I would click on the cancel there would be an exit from the "for" cycle (like the php break; function).
PHP Code:
echo("<script>window.confirm('var= ".$var."')</script>");
Can someone tell me how I can achieve this ?
Thanks in advance to all the help,
pipesportugal
|
|
|
|
04-04-2009, 05:39 PM
|
#2 (permalink)
|
|
The Contributor
Join Date: Feb 2009
Posts: 65
Thanks: 1
|
1. What you are looking for should be coded in Javascript and not PHP, since the code runs on the client and not the server. For this just make the prompt-box calling a function which calls up the prompt-box again (when clicked on ok).
2. Anyhow, you could do it with PHP (and Javascript). Simply call the site again when clicking on ok - and just give a $_GET variable with. Then make php writing the confirm box again. So it would pop up untill the user klicks on cancel. But thats some dump and useless way to do it 
3. Java != javascript
|
|
|
|
04-04-2009, 07:24 PM
|
#3 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,053
Thanks: 115
|
That's not Java.
That's Javascript. Totally different.
__________________
|
|
|
|
04-05-2009, 06:27 AM
|
#4 (permalink)
|
|
The Contributor
Join Date: Sep 2008
Posts: 36
Thanks: 2
|
as said above, JavaScript ≠ Java in any way shape or form.
Anyway, the only way to pass any user information (JavaScript data is considered user data in this case, since all JavaScript is processed client side, or on the browser) to the server is through either a page reload with the content in a $_GET variable, or through an xmlHTTPrequest
The first solution
Code:
<script type="text/javascript" languge="JavaScript">
window.location.href = "yourwebpage.php?variable=passedData";
</script>
From there, you can simply grab the variable in PHP through $_GET["variable"] (after being properly sterilized, of course)
The second solution is a little more in depth, but will look better. It will open a live connection with the processing page, send the information, and return the data... all without reloading your current page. This means it will be a real-time change to the user, making less click though and a (normally) more pleasurable visit to your site.
Code:
<script type="text/javascript" charset="utf-8">
var response=null;
var param="your passed data"
function ajaxRequest(param) {
var request = false;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
if (request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
try {request = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {
try {request = new ActiveXObject("Microsoft.XMLHTTP");}
catch (e) {}
}
}
if (!request) {
alert('There was an error in creating an XMLHTTP instance, please update your browser!');
return false;
} else {
request.onreadystatechange = function() { statusCheck(request); };
request.open('POST', 'yourProcessingPage.php', false);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", param.length);
request.setRequestHeader("Connection", "close");
request.send(param);
}
}
function statusCheck(request) {
if (request.readyState == 4) {
if (request.status == 200) {
window.response=request.responseText;
} else {
alert('There was a problem with the request.');
}
}
}
</script>
If you choose to go this way, it will send as $_POST, so adjust accordingly. If you want to show a response somewhere on the page, just call a function at the end of statusCheck that will display window.response.
This should get you started, there is a lot of documentation online about this stuff, and finding information on your own can help learn this stuff faster. Good Luck!
__________________
Jason Corradino
Applications Developer, Interactive Support - Tribune Technology
J2EE Development, Script Tinkering - Develop, Support, and Maintain Tribune websites.
|
|
|
|
|
The Following User Says Thank You to jcorradino For This Useful Post:
|
|
06-07-2009, 10:32 AM
|
#5 (permalink)
|
|
The Wanderer
Join Date: May 2008
Location: Oporto-Portugal
Posts: 24
Thanks: 11
|
Hi everyone,
Thank You to those that gave me their positive contribution.
I acknowledge the fact that java is different from javascript, thank You all for that.
Regarding Jason's suggestion, I dare at this point to ask him for a short "line by line" explanation of the example code that was posted here.
Thanks in advance,
pipesportugal
|
|
|
|
06-07-2009, 04:03 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 322
Thanks: 2
|
Jason's suggestion if an example of AJAX
Code:
var request = false;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
if (request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
try {request = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {
try {request = new ActiveXObject("Microsoft.XMLHTTP");}
catch (e) {}
}
}
This chunk simply attempts to create a new XMLHttpRequest object which is use for asynchronous request.
Code:
if (!request) {
alert('There was an error in creating an XMLHTTP instance, please update your browser!');
return false;
} else {
request.onreadystatechange = function() { statusCheck(request); };
request.open('POST', 'yourProcessingPage.php', false);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", param.length);
request.setRequestHeader("Connection", "close");
request.send(param);
}
}
This chunk does the work. Setting proper headers and sending the actual request. From the onreadystate property we can see that statusCheck has been set to verify results.
Code:
function statusCheck(request) {
if (request.readyState == 4) {
if (request.status == 200) {
window.response=request.responseText;
} else {
alert('There was a problem with the request.');
}
}
}
This is statusCheck function which was set to check response and verify all went well if it did than use the returned data and if not alert the user.
|
|
|
|
06-07-2009, 04:06 PM
|
#7 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,042
Thanks: 193
|
Aww man, I came in here to find javascript, not java. Would have been interesting to see what this could have been. 
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
06-07-2009, 04:08 PM
|
#8 (permalink)
|
|
The Contributor
Join Date: Sep 2008
Posts: 36
Thanks: 2
|
HTML Code:
<script type="text/javascript" charset="utf-8">
var response=null;
//This is to be the data you are passing through the AJAX call, replace "your passed data"
var param="your passed data"
//This function will actually make the AJAX call
function ajaxRequest(param) {
var request = false;
//Checks to see if a request can be made (non MS browser)
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
if (request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
//Checks to see if a request can be made (MS browser)
} else if (window.ActiveXObject) {
try {request = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {
try {request = new ActiveXObject("Microsoft.XMLHTTP");}
catch (e) {}
}
}
//Checks to see if the request from above was able to be made, if not, will display error
if (!request) {
alert('There was an error in creating an XMLHTTP instance, please update your browser!');
return false;
} else {
//Checks to see if there is any returned status
request.onreadystatechange = function() { statusCheck(request); };
//Opens a connection with another page, using (in this case): Post instead of Get, the page yourProcessingPage.php, and false meaning it will stall the function to wait for the response, true rarely works
request.open('POST', 'yourProcessingPage.php', false);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", param.length);
request.setRequestHeader("Connection", "close");
//Sends data
request.send(param);
}
}
//checks the status of the request
function statusCheck(request) {
//Check if the request is complete
if (request.readyState == 4) {
//HTTP status of 200 means ok (500 means server error and 404 means page not found)
if (request.status == 200) {
//Sets response text to javascript variable window.response
window.response=request.responseText;
//something fubared, displays error
} else {
alert('There was a problem with the request.');
}
}
}
</script>
hope this helped :)
__________________
Jason Corradino
Applications Developer, Interactive Support - Tribune Technology
J2EE Development, Script Tinkering - Develop, Support, and Maintain Tribune websites.
|
|
|
|
06-07-2009, 04:19 PM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
|
You want to pass a JavaScript variable to PHP?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
08-27-2009, 07:08 PM
|
#10 (permalink)
|
|
The Wanderer
Join Date: May 2008
Location: Oporto-Portugal
Posts: 24
Thanks: 11
|
Hi everyone,
I will rephrase my question.
I normally use the javascript function window.alert to stop the system and show me some variable values, this is a kind of my personal debugger.
It stops, and then I look at the variabe value then click on OK and the program goes on...
If I use the window.confirm will show me the OK and CANCEL buttons.
After clcking the OK button the program will go on, and if I click the CANCEL button the program does the same.
How can I find out on my php program, of which button has been clicked ?
Thanks in advance for all the help,
pipesportugal
|
|
|
|
08-27-2009, 08:42 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Sep 2008
Posts: 36
Thanks: 2
|
If you are looking to do what I think you want to do, you cant. Once the javascript is running, the php is already completed its job, and cannot be accessed again unless you use an AJAX call or refresh the page..
__________________
Jason Corradino
Applications Developer, Interactive Support - Tribune Technology
J2EE Development, Script Tinkering - Develop, Support, and Maintain Tribune websites.
|
|
|
|
|
The Following User Says Thank You to jcorradino For This Useful Post:
|
|
08-27-2009, 10:49 PM
|
#12 (permalink)
|
|
The Wanderer
Join Date: May 2008
Location: Oporto-Portugal
Posts: 24
Thanks: 11
|
ok thank You so much Jason.
pipesportugal
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|