Jump to content

Link with two functions?


WilliamGrant

Recommended Posts

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>

 

Thanks a lot

Link to comment
Share on other sites

What you are asking doesn't make sense. PHP executes server-side and JavaScript executes client-side. There is no way to make them both "activate" at the same time.

 

But, let's break down what those are doing to see how they might fit together.

 

- The PHP code is simply dynamically creating the href value for the link dynamically.

- The JavaScript onClick event is calling a function and then returning false. That means if there was an href property assigned it would not run.

 

So, if we put those together only the JavaScript event would fire. Unless the user has JS disabled then only the href would be called. If you want both actions to work you would do a return true, or remove the return completely. Then when the user clicks the link the JS event will first run, then the browser will be redirected to the href.

 

I personally hate writing a lot of code on one line where it makes it hard to read, so I would create the href value separately - then create the actual link. Give this a try:

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

Link to comment
Share on other sites

Hi,

 

Your help is definitely much appreciated.

I have been trying to implement your idea and what appears to happen is that I get a blank page in my browser.

If you would not mind I will share with you the full php part.

I am trying to implement your idea into this part of the script.

 

 

	<?php
	// 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>';
		flush();

		// 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>';
		}
		else
		{
			echo '<p>Error downloading video!</p>';
		}
	}
?>

 

 

Link to comment
Share on other sites

I have no idea what initGateway() is supposed to do or how you expect that to work when redirecting to the href URL. This really isn't a PHP issue. Get it to work how you want by hard-coding it. Then figure out how to write them dynamically using PHP. I think you are just wanting a download link that doesn't open a new page? If so, there are plenty of resources out there. Just do a search for "PHP force download"

Link to comment
Share on other sites

To explain the function of the script..

 

Basically,

 

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 tag>

 

I suppose there are ways to make javascript work within php, I'll have to figure it out.

Link to comment
Share on other sites

I suppose there are ways to make javascript work within php, I'll have to figure it out.

There is no such thing as "javascript work[ing] within php". I guess that is where the confusion is coming from. You are using PHP to dynamically create the href value. That is all done on the server. Then, the completed page (including JavaScript) is sent to the user. There is no more PHP in the page the user receives. The JavaScript is then executed on the client-side.

 

That is why I stated you should hard-code the link to work as you want it to and then figure out how to dynamically create it using PHP. This really isn't a PHP issue, but a JavaScript issue. What I provided should work depending on what you are really doing in that JavaScript code. My guess is that the JS code is doing something that is preventing the link from being processed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.