/    Sign up×
Community /Pin to ProfileBookmark

how to make a javascript function execute some code at php?

i have this code in delete.php

[code]ask(“Do you want to delete the relations?”);[/code]

it’ll execute this function :

[code=php]
function ask($answer) {
global $connection;
$return .= “<script language=”javascript”>n”;
$return .= “var x=confirm(‘$answer’);”;
$return .= “if(x==true){“;
$return .= “var go = ‘yep’ ;”;
$return .= “alert(‘Proceed’); }”;
$return .= “else{“;
$return .= “alert(‘Terminated’); }”;
.
.
.
[/code]

with ask function, i want to execute this code in the same file if i answer ‘OK’ (if x==true)

[code=php]

for($idCounter = 0;$idCounter<=9999;$idCounter++){
if($_GET[$idCounter] != “”){
$id[$i] = $_GET[$idCounter];
jdebug($id[$i]);
//delete address table
$query02 = mysql_query(“DELETE FROM `address`
WHERE `id` IN (SELECT `address_id` FROM `address_relation`
WHERE `relation_id` = ‘$id[$i]'”);
.
.
.
[/code]

how to make this happen?
thx

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@PhalonApr 03.2006 — Well now this is tricky because php is server side and javascript is client side, so as far as i know your going to have to compramise. Either go to another page or reload the same page while sending the data to the php script.
Copy linkTweet thisAlerts:
@intriviousApr 03.2006 — Search for AJAX. It will allow you to call PHP functions via javascript.
Copy linkTweet thisAlerts:
@chazzyApr 03.2006 — while the solution you are looking for does require asynchronous javascript, it's not merely It will allow you to call PHP functions via javascript.[/quote]

AJAX allows you to send data back to the server, wait for a response, and manage the response text. You need to work with the concept from the ground up, it's not a quick implementation.
Copy linkTweet thisAlerts:
@sitehatcheryApr 03.2006 — Do this in Javascript:
[code=php]
function ask(answer) {
var x=confirm('answer');
if(x==true){
var go = 'yep' ;
alert('Proceed');
window.open("http://www.thisaddress.com?yep=1")
}
else{
alert('Terminated');
}
.
.
.
}
[/code]


You will pass into it from PHP like this:
[code=php]
<script type="text/javascript">
ask(<?php echo $answer ?>);
</script>
[/code]



[code=php]
<?php
if($_GET['yep']==1){
...
do your business here
...
}
?>
[/code]


I'm off lunch now, so didn't have time to test it. But, it's worth a try. I finally got PHP to interact with Javascript just yesterday. I set up an array in PHP and displayed the results. Then, javascript iterated through the PHP array and did dynamic stuff. It may take a few tries, but I think you WILL be able to do this with PHP and JavaScript.
Copy linkTweet thisAlerts:
@intriviousApr 03.2006 — while the solution you are looking for does require asynchronous javascript, it's not merely

AJAX allows you to send data back to the server, wait for a response, and manage the response text. You need to work with the concept from the ground up, it's not a quick implementation.[/QUOTE]

Yes, you are correct in what you say. I dislike the term AJAX, and I was a little sarcastic in what I said. But, I am sure if anyone who is in need of asynchronous javascript will more than likely want to use 'AJAX' once they are introduced to it. ? Good distinction between the terms though!
Copy linkTweet thisAlerts:
@cmon1011authorApr 04.2006 — thx ?

i try it but i now i put the function at the file.tpl and put the code that i want to do in php, but it doesnt work -_-"

sitehatchery : m.. doesn't work, maybe i guess that's a good idea, maybe i miss something ?

now i try like this :

i put this function at file.tpl
[code=php]
function ask() {
var x=confirm("delete?");
if (x == true) {
alert("Proceed");
var kk='yep';
}
else {
alert("Terminated");
}
}
[/code]

then i make this in the same file :
[code=php]
<input type="submit" name="action" value="delete" onClick="ask()">
[/code]

after that, i put this code in the file.php
[code=php]
if($_GET['action'] == "delete"){
jdebug($_GET['kk']);
jdebug($_GET['yep']);
jdebug($_GET['x']);
[/code]

every debug doesn't show anything..

i guess i ask a bit different question, sry if i ask the same question, coz now i use 2 different file..

how to make the function ask() at file.tpl execute something at file.php?

or how to make the file.php know/notice about this code var kk='yep';
or.. i got another idea.. how to make the ask function to stop if the user click cancel? so the page is still the same/still stay at the same page..

i've try some of this code, but it doesnt work

return history.go(-1);
history.go(-1);
return;
return false/true;
die();

i try using google to find the code, but i haven't find it -_-"

thx!
Copy linkTweet thisAlerts:
@cmon1011authorApr 04.2006 — finally i have a solution

file.tpl :
[code=php]
function ask(id) {
var x=confirm("Do you want to delete the relation?");
if (x == true) {
alert("Proceed");
var kk='yep';
return true;
}
else {
alert("Terminated");
return false;
}
}
[/code]

[code=php]
<input type="submit" name="action" value="delete" onClick="if(!ask()) { return false; }">
[/code]


file.php :
[code=php]
if($_GET['action'] == "delete"){
for($idCounter = 0;$idCounter<=9999;$idCounter++){
if($_GET[$idCounter] != ""){
.
.
.
.
[/code]


thx anyway for everyone even if i still don't understand what is ajax ?
×

Success!

Help @cmon1011 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...