View Full Version : Link with two functions?

08-01-2012, 11:24 PM
Hey guys,

Im trying to make the following work..

I have this piece of code as a link:

<a href="'.$_SERVER['PHP_SELF'].'?mp3='.urlencode(trim(strstr($converter->GetSongFileName(), '/'), '/')).'">Download your File.</a>

and I have this piece of javascript:

<a href="#" onClick="javascript:initGateway(); return false;">Click here</a>

I need to blend these to together into one single <a href=""> so that when you click the link, both functions will be activated.

Your help would be much appreciated. So far I haven't been able to pull it off..

I tried the following but without result :

<a href="'.$_SERVER['PHP_SELF'].'?mp3='.urlencode(trim(strstr($converter->GetSongFileName(), '/'), '/')).';initGateway(); return false;">Download your MP3 file.</a>

08-02-2012, 12:03 AM
You can't just quote PHP in the middle of HTML, it needs to be enclosed in <?php and ?> tags. I think this is correct, technically,

<a href="<?php echo $_SERVER['PHP_SELF'] . '?mp3=' .
urlencode(trim(strstr($converter->GetSongFileName(), '/'), '/')); ?>"
onclick="initGateway();return false;">Download your MP3 file.</a>

But it won't work: clicking the link will cause the href to be followed and the click event will never happen.

08-02-2012, 12:25 AM
just put the link in the onclick command

08-02-2012, 12:45 AM
Its not really a link, it calls a piece of javascript..

I'll explain further what I try to accomplish..

The visitor clicks on a download button to get their mp3 file and when they do, the page freezes and an offer from our sponsor will appear on the screen asking them if they are interested in the offer. If they are not interested, they can close it to download their file.
The offer is triggered with a piece of javascript.
The javascript itself is placed below the <body>

This is the php part:

// On form submission...
if ($_POST['submit'])
// Print "please wait" message and preview image
echo '<div id="preview" style="display:block"><p>...Please wait while I try to convert:</p>';
echo '<p><img src="http://img.youtube.com/vi/'.$converter->ExtractVideoId(trim($_POST['youtubeURL'])).'/1.jpg" alt="preview image" /></p>';
echo '<p>'.$converter->ExtractSongTrackName(trim($_POST['youtubeURL']), 'url').'</p>';
echo '<div id="progress-bar"><div id="progress">0%</div></div></div>';

// Main Program Execution
if ($converter->DownloadVideo(trim($_POST['youtubeURL'])))
echo ($converter->GenerateMP3($_POST['quality'])) ? '<p>Success!</p><p><a href="'.$_SERVER['PHP_SELF'].'?mp3='.urlencode(trim(strstr($converter->GetSongFileName(), '/'), '/')).'">Download your File.</a></p>' : '<p>Error generating File!</p>';
echo '<p>Error downloading video!</p>';

Understanding that I am dealing with two different worlds (server vs client) I'm not sure how to go about this one..

08-02-2012, 01:07 AM
What does initgateway do? can it trigger the other script from within itself?

08-02-2012, 01:16 AM
"initgateway" is supposed to trigger this script (

<script type="text/javascript">var gwloaded = false;</script>
<script src="URL" type="text/javascript"></script>
<script type="text/javascript">if (gwloaded==false){window.location = "#";}</script>
<noscript><meta http-equiv="refresh" content="0;url=URL" /></noscript>

*offtopic, I took the actual links out for security reasons but this is the javascript.)

08-02-2012, 02:06 AM
That is three separate scripts, none of which mention "initgateway" :confused:

*offtopic 'noscript' is deprecated, as is the use of 'javascript:'.

08-02-2012, 11:52 AM
i'd modify your popup script to get the file when they close it