Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Writing to a file when a link is clicked
Old 05-30-2007, 04:38 PM Writing to a file when a link is clicked
pea
Average Talker

Posts: 28
Trades: 0
I'm trying to gather the most popular links on my database, so i need to write to a file whenever a link is clicked.

I thought this might work, but it leaves the page loading on ff and completely crashes on opera.

Code:
<script type="text/javascript">
function execute_php() { document.write("<?php 

$datafilearray = file("file.php");

$fd = fopen ("file.php", "w");

$all = count($datafilearray)+1;

for($i = 0; $i < $all; $i++) {
    fwrite ($fd, $datafilearray[$i]);
}

fwrite ($fd, "test\n");

fclose($fd);

 ?>") }
</script>

<a href="http://www.google.com" onclick="javascript:execute_php();" target="_blank">Link</a>
Thanks, Peter
__________________

Please login or register to view this content. Registration is FREE
pea is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-30-2007, 05:07 PM Re: Writing to a file when a link is clicked
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You cannot operate that way.

PHP being a server side language, and javascript a client side, you will just write php code in the local user browser, but it won't get interpreted.

To do what you are trying to do, you must:

1) create a page on your server that will get the datas you need (either passed from a form, or fetched from a db)
2) Create a link to that page, giving the necessary parameters in the call

When the user will land on the page, the server will create the file via PHP.
From there, you can do everything you want with your file.
Get it via FTP, or create another link that will allow the user to download it via his browser, store it as backup...
It's up to you.

Edit:
The page being still loading is because when you use javascript to write in a newly created window, you need to do a document.close(); at the end of your script to explicitly tell the browser that you have finished writing in it.
When doing document.write in a physically existing page, there is no need for it, as the browser already have closed it. You are adding more content, not creating it from scratch.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 05-30-2007 at 05:10 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-30-2007, 05:15 PM Re: Writing to a file when a link is clicked
pea
Average Talker

Posts: 28
Trades: 0
ok then. Thanks
__________________

Please login or register to view this content. Registration is FREE
pea is offline
Reply With Quote
View Public Profile
 
Old 06-01-2007, 03:52 PM Re: Writing to a file when a link is clicked
pea
Average Talker

Posts: 28
Trades: 0
I just came up with this, it doesn't stop search engines from skipping the link but it still redirects to the writing script:

Code:
<a href="link.html" target="_blank" onclick="window.location = 'write_to_datafile.php?link=link.com'">link</a>
Code:
<?php
$datafilearray = file("links_visited.php");

$fd = fopen ("links_visited.php", "w");

$all = count($datafilearray)+1;

for($i = 0; $i < $all; $i++) {
    fwrite ($fd, $datafilearray[$i]);
}

fwrite ($fd, "".$_GET['link']."\n");

fclose($fd);

header("location: $HTTP_REFERER");
?>
__________________

Please login or register to view this content. Registration is FREE
pea is offline
Reply With Quote
View Public Profile
 
Old 06-01-2007, 04:25 PM Re: Writing to a file when a link is clicked
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Yes, the rule is:

The onclick is executed before the href.
If there is javascript which return "false", then the href is not followed.
Otherwise, the href is followed after the onclick execution.

I personnaly used this on a link exchange page, to record the outgoing click without breaking the browsing via an intermediate page.
I send an AJAX request with he onclick to record the link out.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-01-2007, 04:53 PM Re: Writing to a file when a link is clicked
pea
Average Talker

Posts: 28
Trades: 0
Oh, so i need to move the onclick? Like:

<a onclick="window.location = 'write_to_datafile.php?link=link.com'"
href="link.html" target="_blank">link</a>
__________________

Please login or register to view this content. Registration is FREE
pea is offline
Reply With Quote
View Public Profile
 
Old 06-01-2007, 05:46 PM Re: Writing to a file when a link is clicked
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
The order of the onclick and href have no importance.
You can put them in any order you want.

Both of your syntaxes are valid.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-01-2007, 06:52 PM Re: Writing to a file when a link is clicked
pea
Average Talker

Posts: 28
Trades: 0
Oh you mean the javascript is executed before..yeh
__________________

Please login or register to view this content. Registration is FREE
pea is offline
Reply With Quote
View Public Profile
 
Old 06-01-2007, 07:18 PM Re: Writing to a file when a link is clicked
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
yep. It's always the onclick before the href, no matter the order they are written.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-20-2007, 04:06 PM Re: Writing to a file when a link is clicked
pea
Average Talker

Posts: 28
Trades: 0
HAHAHA HA! HAHA!. ha. bargain
__________________

Please login or register to view this content. Registration is FREE
pea is offline
Reply With Quote
View Public Profile
 
Old 06-20-2007, 04:09 PM Re: Writing to a file when a link is clicked
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
This forum isn't here to sell your services to people. If you don't want to give help, then don't respond.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Writing to a file when a link is clicked
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31831 seconds with 12 queries