javascript and php

hi, how can i send a confirmation dialog of javascript in php and get the respond of ok or cancel from the dialog??
thanks alot ...
[140 byte] By [ayumi] at [2007-11-20 11:03:34]
# 1 Re: javascript and php
You will have to pass it through a parameter or use AJAX.

<script type="text/javascript">
var okcancel = confirm();
var theResponse = (okcancel) ? 'yes' : 'no';
window.location = 'test.php?response=' + theResponse
</script>
PeejAvery at 2007-11-8 0:43:10 >
# 2 Re: javascript and php
!!! thank you very much :)
got it to works hehe
ayumi at 2007-11-8 0:44:07 >
# 3 Re: javascript and php
You're welcome. Good luck with the rest! :wave:
PeejAvery at 2007-11-8 0:45:09 >
# 4 Re: javascript and php
erm.. could i continue to ask ... after i do a window.location...
how can i grab the variable from the url and set it to the window.location??

for example when the confirmation dialog is invoke the url is
test.php?abc=3&ddd=4

how can i retrieve the abc=3 and add it to the window.location?

thanks
ayumi at 2007-11-8 0:46:14 >
# 5 Re: javascript and php
Just use $_GET.

<?php
$abc = $_GET['abc']; // value will equal 3
?>

<script type="text/javascript">
window.location = 'test.php?abc=<?php echo $abc; ?>';
</script>
PeejAvery at 2007-11-8 0:47:19 >
# 6 Re: javascript and php
my javascript is a .js file... and the function is invoke onclick of a button..
could i just add the php tag inside the .js file??
ayumi at 2007-11-8 0:48:18 >
# 7 Re: javascript and php
No. The JavaScript file is interpreted on the client side. Therefore the PHP, being server-side, cannot read it.

If you need to access the variable using JavaScript, here is a function I just made up that should help you.

function retrieveURLVariable(name){
var vURL = window.location.search.substr(1);
var vParts = vURL.split(name + "=");
vParts = vParts[1].split('&');
return vParts[0];
}

var test = retrieveURLVariable('abc');
alert(test);
PeejAvery at 2007-11-8 0:49:17 >
# 8 Re: javascript and php
oh .. thats a good function~

if for php say i have a <a href="..test.php?abc=3>

after i do the processing how can i set back all the variable that has been passed over?
i know i could do a redirection and set it back 1 by 1 ... but i have lots of variable to do ...
is there any more efficient method?

thanks in advance ~~
ayumi at 2007-11-8 0:50:23 >
# 9 Re: javascript and php
Well, in PHP you can grab the whole query string using $_SERVER['QUERY_STRING'].
PeejAvery at 2007-11-8 0:51:22 >
# 10 Re: javascript and php
>.< thanks alot for your help ... appreciate it. haha have just learn php for 3wks

will try to continue my work and post if face any more problem :) thankS!
ayumi at 2007-11-8 0:52:21 >
# 11 Re: javascript and php
You're welcome. :wave:

If you have more problems, create a new thread. Each thread should be a different problem.
PeejAvery at 2007-11-8 0:53:24 >