You are not logged in.

#1 2007-02-12 08:23:02

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

php and javascript

1. <script>
2. var r=confirm("Are you sure you want to delete")
3. if (r==true) {
4. alert("You selected Yes")
5. <?php $del = "yes"; ?>
6. } else {
7. alert("You selected No")
8. <?php $del = "no"; ?>
9. }
10. </script>

When PHP is included, whatever is selected (OK or Cancel), the above script will always read line 8 (php). The java lines (4 and 7) does correctly as per selection. Any solution to get php as per selection?


Markku

Offline

#2 2007-02-12 15:37:12

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: php and javascript

it's because php doesn't interact with javascript, nor the browser..
basically the php code never reaches the browser.
mayeb you could walk around it by using ajax or something, to cache the value in a session cookie. i dunno.


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#3 2007-02-12 23:41:53

Stalwart
Member
From: Latvia, Riga
Registered: 2005-10-18
Posts: 445
Website

Re: php and javascript

PHP code is executed before javascript code reaches user's browser


IRC: Stalwart @ FreeNode
Skype ID: thestalwart
WeeChat-devel nightly packages for i686

Offline

#4 2007-02-13 02:43:01

codemac
Member
From: Cliche Tech Place
Registered: 2005-05-13
Posts: 794
Website

Re: php and javascript

You could just POST the result to a controller page that then deletes the whatever, and then forwards back to the previous referring page.

Offline

#5 2007-02-13 12:00:25

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: php and javascript

codemac wrote:

You could just POST the result to a controller page...

I am not familar with the word "controller page". Can you give a sample.


Markku

Offline

#6 2007-02-13 12:38:24

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: php and javascript

head.html

<script language="JavaScript">
var r=confirm("Are you sure you want to delete")
if (r==true) {
    alert("You selected Yes")
    document.href="bounce.php?del=yes"
}
else {
    alert("You selected No")
    document.href="bounce.php?del=no"
 }
</script>

bounce.php

<?php
 // @session_start;
if (isset($_POST['del'])) {
    if ($_POST['del'] == 'yes') ) {
        // $del = 'yes';
        // $_SESSION['del'] = 'yes';
        // do_whatever ...
    }
    else {
        $del = 'no';
        // $_SESSION['del'] = 'no';
        // do whatever ... 
    }
}
@header("Location: head.html") or echo "<meta http-equiv="refresh" content="2;url=head.html">";
?>

obviously that doesn't works as-is, and neither does the concept.
it's just a small demonstration on your last question.


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#7 2007-02-13 18:24:14

[vEX]
Member
From: Sweden
Registered: 2006-11-23
Posts: 450

Re: php and javascript

You would not use POST if you put the stuff in the URL, you use GET.


PC: Antec P182B | Asus P8Z77-V PRO | Intel i5 3570k | 16GB DDR3 | GeForce 450GTS | 4TB HDD | Pioneer BDR-207D | Asus Xonar DX | Altec Lansing CS21 | Eizo EV2736W-BK | Arch Linux x86_64
HTPC: Antec NSK2480 | ASUS M3A78-EM (AMD 780G) | AMD Athlon X3 425 | 8GB DDR2 | GeForce G210 | 2TB HDD | Arch Linux x86_64
Server: Raspberry Pi (model B) | 512MB RAM | 750GB HDD | Arch Linux ARM

Offline

#8 2007-02-14 07:38:16

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: php and javascript

Thanks, noriko and vEX. I will test the solution.


Markku

Offline

#9 2007-02-14 13:35:03

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: php and javascript

ya sory i forgot which way roudn they went.. cos i usually use request...


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#10 2007-02-15 09:17:14

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: php and javascript

Couldn't get to work, here what I am trying to do. What's missing? For my information, to read the full url of "bounce.php", how to?

head.php

<script language="JavaScript">
    document.href="bounce.php"
</script>

<?php
$del=$_GET['del'];
echo "Test: $del";
?>

bounce.php

<?php
$del = "yes";
?>

Markku

Offline

#11 2007-02-15 14:38:31

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: php and javascript

what exactly are you trying to do....

btw, see my example above, you are implementing it incorrectly.

the php code u wrote needs to be in the same file.
either in head with the js, or in bounce.


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#12 2007-02-15 16:54:10

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: php and javascript

noriko wrote:

...the php code u wrote needs to be in the same file.
either in head with the js, or in bounce.

I tried in different ways both your sample and mine. I am concluding javascript don't find the bounce.php. This is way I asked how to display the url  (or path) created by document.href="bounce.php" ... to be sure the location is correct.

I am not familar with javascript. Does document.href="" do same as include() in php?


Markku

Offline

#13 2007-02-15 17:36:07

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: php and javascript

no, it changes the document's href (hypertext reference, i think) .. basically it redirects the page, much like header("Location:[*]")`php does.
what exactly happens?
does it change page? it should ...

ps:
     whatever it is ur trying to do, i'm sure it can be done with forms, don't forget js isn't enabled on every webbrowser .... i personally have all        plugins except mplayer+flash turned off in fx, and have no addons, or themes.


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#14 2007-02-17 15:08:51

aquila_deus
Member
From: Taipei
Registered: 2005-07-02
Posts: 348
Website

Re: php and javascript

rasat wrote:
1. <script>
2. var r=confirm("Are you sure you want to delete")
3. if (r==true) {
4. alert("You selected Yes")
5. <?php $del = "yes"; ?>
6. } else {
7. alert("You selected No")
8. <?php $del = "no"; ?>
9. }
10. </script>

When PHP is included, whatever is selected (OK or Cancel), the above script will always read line 8 (php). The java lines (4 and 7) does correctly as per selection. Any solution to get php as per selection?

hmmm you're confused. PHP is processed before end-user's browser get the web page and run javascript. So the line 5 and 8 don't make sense.

Your js code could then submit back for php to handle the change.

BTW have you read about MVC? It can help you to build website on a clear design, and skip a lot of error/trying steps. Most of non-mvc websites I have seen are all very messy and hard to maintain and code.

Offline

#15 2007-02-17 15:36:38

reaper
Member
Registered: 2007-02-16
Posts: 27

Re: php and javascript

i have ..
<a href="?section=gb&acc=delete" onclick="return confirm('Delete?')">Del</a> smile

Offline

#16 2007-02-23 13:24:49

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: php and javascript

reaper wrote:

.... onclick="return confirm('Delete?')">Del</a>

Thanks, reaper!! That solved the issue I was searching.


Markku

Offline

Board footer

Powered by FluxBB