Jump to content


Photo

Onclick PHP script


  • Please log in to reply
9 replies to this topic

#1 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 09 August 2006 - 03:47 PM

I searched every where I cant figure this out.  How can you run a php script on a user click?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 03:50 PM

If you want to run a PHP scriupt when you click a link/button/image without the user being sent to the PHP script itseft you'll want to use AJAX. AJAX is javascript, but it allows you to called server side scripts in the background without having to reload the page. Check out this introduction tutorial over at ajaxfreaks.com

#3 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 09 August 2006 - 04:47 PM

I appreciate the link and help.  I submitted help into the forums, and no one seems to respond.  Not nearly as many user as this forum.  I looked at many examples and tutorials and cant seem to find what I need.  Perhpas you could give me some pointers  if you do not mind.  Thankyou much!

#4 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 09 August 2006 - 06:57 PM

Alright check this out, I am racking my brain here and this is a very difficult concept to understand and accomplish in my opinion.  Let me re-explain.

The only way I know how to send client side information to a server is through a form.  This is slow and clumsy to me, I want to use links that are created when a page loads through a php script.  These links are dynamic so I cant just create these myself, they must be created through a script.
  When you click a link, I need to know which link was clicked so I can send that data to a php script.  Using Ajax, only allows you to run php script to send Ajax data without a refresh, not the other way around.  You cannot send a php script data from Ajax, or any javascript that I am aware of.

Am I truely limited to using a form or is there a way that I am not aware of using either Ajax or any other method? 



#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 07:13 PM

Do you mean do something likje this:
<?php

if(isset($_GET['test']))
{
    echo $_GET['test'];
}

?>
<a href="test.php?test=hello">Test</a>


#6 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 09 August 2006 - 07:32 PM

What do you want the PHP script to actually do? Is it going to be echoing anything to the browser, or is it just updating a database? As, depending on what you want to do there is another solution, a little like Ajax but easier to do.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#7 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 09 August 2006 - 08:23 PM

Output:

  Link1
  Link2
  Link3


User:

Click Link1 -> Send the data that tells the php script that link1 was clicked.


PHP:

Sees link1 was clicked therefore do this;


Its like choosing from a dynamic drop down menu in a form.  But instead I want to use links.  Without using a form, I dont know how to send what was chosen by the user to the php script.  What it does is irrelevant, what it does depends on what link was chosen.  Again the links are dynamic so I cant predefine what it does, it must create it all on its own.




#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 09 August 2006 - 08:26 PM

Take a closer look at WildTeen88's post and explain why that isn't the solution.
Legend has it that reading the manual never killed anyone.
My site

#9 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 09 August 2006 - 08:31 PM

What I'm trying to establish is whether you want the browser to relocate to the script being clicked on. If so, that's easy: just add a query string on the end of the URL, e.g.
<a href="myscript.php?var1=1&var2=10&var3=20">My link</a>

and you then get the values in the PHP script in the $_GET array, just like you get them from a form using the $_POST array.

If you don't want to relocate the browser to the script, but just want to call it, there is a way to do it with Javascript (or even without the javascript but in a less useful way).
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#10 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 10 August 2006 - 03:16 PM

You guys are brilliant, I appologize to Wildteen88, I just didnt understand it in your example, but you were right.  I never thought about sending it through the URL like that, I am so stupid.  Still learning.  Thankyou for your help everyone. 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users