Nov 8, 2009

How To Use A Link To Call Function In Php?

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > PHP Programming

How To Use A Link To Call Function In Php?

leiaah
The title says it all, really. How do you call a function using <a href=" function name"> in PHP? I'm doing a project and I stumbled upon this problem. I don't want to use query string in the href part like <a href="?del()"> since that would mess up the other part of my code. Can anyone pleae help me? I've pasted the code below. smile.gif Thanksh.

CODE
<?php
  function display($x){
//coding goes here.
  }
?>

<html>
<body>
<p align="center">
<a href="what goes here?!">Display itmes</a>
</p>
</body>
</html>

Comment/Reply (w/o sign-up)

moldboy
I don't think you can, as PHP is rendered server side, you can with JavaScript. I can't tell if you've considered making your site go back to itself with a post var, something like index.php?function=what_you_want, then in the code only execute the function if the variable $function == "what_you_want".

Comment/Reply (w/o sign-up)

Tyssen
Wouldn't it be something like this?

CODE
<a href="<? = nameOfYourFunction(variablePassedToTheFunction) ?>">Display items</a>

Comment/Reply (w/o sign-up)

fffanatics
Ok i ran into the same problem when i was first creating my website hosted here at trap17. What you have to do is link back to the same page with either a post or a session variable now included. So your code for the website would stay the same except you would have an if statement checking to see if _SESSION[...] or _POST[...] existed. If it did then would now just run the function you wanted to run. So all you have to do is make the href="yourpage.php?login=yes"

Comment/Reply (w/o sign-up)

Tyssen
QUOTE(fffanatics @ Jan 30 2006, 08:45 AM)
What you have to do is link back to the same page with either a post or a session variable now included. So your code for the website would stay the same except you would have an if statement checking to see if _SESSION[...]  or _POST[...] existed. If it did then would now just run the function you wanted to run. So all you have to do is make the href="yourpage.php?login=yes"

Eh? Why would you have to do that? If the function is based on a session or post variable, you might have to, but if it's not you wouldn't. For instance,

CODE
<?php
 function display($x){
echo ($x - 10);
 }
?>

No need for any session variables in there.

Comment/Reply (w/o sign-up)

leiaah
QUOTE(fffanatics @ Jan 30 2006, 06:45 AM)
Ok i ran into the same problem when i was first creating my website hosted here at trap17.  What you have to do is link back to the same page with either a post or a session variable now included. So your code for the website would stay the same except you would have an if statement checking to see if _SESSION[...]  or _POST[...] existed. If it did then would now just run the function you wanted to run. So all you have to do is make the href="yourpage.php?login=yes"
*



Can you give some sample coding here? I used a function because I don't want to redirect it to another page. The action is done in the function, in the page itself and not in another page. When I click the link, it should seem like nothing happened in the page but the function does its job.

 

 

 


Comment/Reply (w/o sign-up)

Spectre
It's not possible to call a function directly from a link, per se (as has been noted, PHP is server side), but you can construct a link to use GET variables to instruct the script to execute a certain function.

For example:

CODE
if( isset($_GET['function']) ) {
 switch( $_GET['function'] ) {
    case 'dosomething':
       dosomething();
    break;
    case 'dosomethingelse':
       dosomethingelse();
    break;
 }
}


And then link to script.php?function=dosomething

Hope that helps.

Oh, and Tyssen, using the link you originally supplied would cause nameOfYourFunction() to be executed whenever the script is, not just when the user clicks the link. Anything between <?php ?> (and, depending on server configurationm, <? ?>) is going to be processed as soon as the PHP engine encounters it within the script, so at the client's end, the link would end up pointing to whatever output nameOfYourFunction() returned (if any).

Comment/Reply (w/o sign-up)

leiaah
Thanks Spectre! I've tried it out and it's exactly what I want. I wish I had thought of it before. smile.gif

Comment/Reply (w/o sign-up)

FeedBacker
Same PHP function link
How To Use A Link To Call Function In Php?

Hi, I've tried nearly everything on my script, my problem is that everytime I have the forma action (to get the isset($_get...) it reloads all my variables creating issues of course because I have two different forms. My script goes something like this (obviously I'm not applying syntax here):

Html:

Form action:process.Php method post
<input send>
<input preview>


No problems there.

Now the process.Php has several conditionals:

If(isset(send)){
SendFunction();
}

If(isset(preview)){
<input go back> this part works!
<input send> and here I need to use same function without resending everything
}

I can't use a form post in the last one because it'll process all script again. So I want to href link it to the function.

Please help!



-question by Taliaha

Comment/Reply (w/o sign-up)

(G)Lol
How To Use A Link To Call Function In Php?
How To Use A Link To Call Function In Php?

Lol.  Yeah, in case you haven't noticed, all PHP code never leaves the server.  If you expect a browser to ever be able to interpret that, well, let's just put it this way: Browsers themselves don't ever see any php code, and therefore have no parsers/runtimes for such.


Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : link, call, function, php

  1. Search Engine/ Function
    (0)
  2. PHP Function To Add Previous and Next Page Feature
    useful php function (5)
    CODE function navigationbar($start_number = 0, $items_per_page = 50, $count) {    // Creates a
    navigation bar    $current_page = $_SERVER ;     if (($start_number         $start_number = 0;
        }     $navbar = "";     $prev_navbar = "";     $next_navbar = "";     if ($count >
    $items_per_page) {         $nav_count = 0;         $page_count  = 1;         $nav_passed = false;
            while ($nav_count             // Are we at the current page position?             if
    (($start_number                 $navbar .= " ";                 $nav_passed = true;       ....
  3. Endif function?
    (6)
    As you get noticed before, I am studying PHP in examples like using the tutorials as well as books
    itself. Through my readings, I get this function CODE a lot of times. So, what do you mean
    by this function, and what does it do exactly?....
  4. Arrays Outside A Function
    Need to have arrays available to all functions. (3)
    I've got a bunch of arrays that i want to use for more then 1 function. when i declear the
    arrays outside a function i cant use it in a function. This code was originally written in
    javascript by another person but since I plan to use it and extend it with php I had to change it
    from javascript to php code. In the javascript code the arrays were decleared outside the functions
    with 'var arrayname' I read somewhere that declearing javascript variables with
    'var' gives it global access. Any ideas on how I can go about declearing 1 set of these
    arrays t....
  5. How To Check If Fsockopen Function Is Enabled?
    (3)
    Hi, I have VPS (virtual private server) and I have access to php.ini file. Is there any script that
    will show that fsockopen function is enabled or where do I have to enable it? Searched google and
    here and couldn't find anything. Thanks! ....
  6. Php Explode Function Help
    (4)
    I am having trouble creating a script, all i want to achieve is to: 1. Select the variable from my
    mysql database, which is in a format of : id|id|id|id| and so on... 2. Split them into separate
    variables by using : $songexploded = explode("|",$ttyo ); 3. Then this is the bit I'm stuck on
    trying to create a while loop from the $songexploded variables. So(this might not be correct but
    you should get the idea).. CODE $x=1; while ($songexploded ==$result) echo $songexploded
    .' '; } ....
  7. The Best Zip Function
    (1)
    hi my 6th code is very useful, you can zip your file by this: CODE class dZip{     var
    $filename;     var $overwrite;          var $zipSignature = "\x50\x4b\x03\x04"; // local file header
    signature     var $dirSignature = "\x50\x4b\x01\x02"; // central dir header signature     var
    $dirSignatureE= "\x50\x4b\x05\x06"; // end of central dir signature     var $files_count  = 0;
        var $fh;          Function dZip($filename, $overwrite=true){         $this->filename  =
    $filename;         $this->overwrite = $overwrite;     }     Function addDir($dirname,
    $fileComments=&#....
  8. Mail() Clone
    A PHP mail() function clone (5)
    A lot of free web hosts have disabled the mail() function so you cannot send emails using PHP. Does
    anybody know of a script that makes a function "like" mail but is able to be installed in a web
    accessible directory and called included into another script and called like that? Or maybe you know
    how to make such a function? I just really need to find a way around the free hosts turning of the
    mail() function. I need to figure out a way to send emails.....
  9. [php](simple) Using Functions To Combine Values In A Form
    Really simple example on how to combine values with function (2)
    I just learned this simple method on how to use functions to combine two values from a form. First
    we create ourselves a simple POST form CODE Name: Location: Now we add this php
    to that same file CODE $nick = $_POST ; $location = $_POST ; function information($nick,
    $location){     echo 'My nick is '.ucfirst($nick).' My location is '.$location;
         } information($nick, $location); ?> that code is similar to this one CODE $nick =
    $_POST ; $location = $_POST ; function information($nick, $location){     return ....
  10. [php] Header Function
    (2)
    Header function Greetings we are going to use the header() funtion to redirect start making a
    file called page.php at the top of the file add CODE ?> Example 1 After CODE
    header('Location: http://www.trap17.com'); the LOCATION means where you want it to go.
    Example 2 you also can define a file that you want to redirect to After CODE
    header('Location: index.php'); Example 3 you also can add a timer to it /laugh.gif"
    style="vertical-align:middle" emoid=":lol:" border="0" alt="laugh.gif" /> After CODE header(&#....
  11. Error With Joomla Template
    cant find function (1)
    Hello! I am working on my template in Dreamweaver and i am using joomla extensions for dreamweaver!
    When i start my page with joomla stand alone server(jsas) i get this errors on the bottom of the
    page! QUOTE Warning: mosloadcomponent(w:/www/Joomla/components/com_banner/banner.php) : failed
    to open stream: No such file or directory in w:\www\Joomla\includes\frontend.php on line 66
    Warning: mosloadcomponent(w:/www/Joomla/components/com_banner/banner.php) : failed to open stream:
    No such file or directory in w:\www\Joomla\includes\frontend.php on line 66 Warning: m....
  12. The Extract() Function
    Something I just found out (6)
    The extract() function is used in PHP to take an array and split it up into variables. MySQL
    queries can be parsed this way. Below is an example. CODE $query = mysql_query("select
    username, password from users where uid=1"); $result = mysql_fetch_array($query, mysql_assoc);
    extract($result); print "Your username is : $username"; The extract() function works for ANY
    array, including $_POST, and $_GET. Makes processing form data a LOT easier /biggrin.gif"
    style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> .....
  13. How Good Is This Data Cleaning Function?
    (2)
    Hi all, this is my first function and as part of a script and i just want to know a couple of
    things. here is the code for the function: CODE function clean($dirty_string) {
    $muddy_string = stripslashes($dirty_string); $murky_string = strip_tags($muddy_string);
    $clean_string = htmlentities($murky_string);      }; ?> So the first thing is how secure is it?
    the script this will be used in connects to a database and sends an email so it needs to stop SQL
    injections and any email abuse it might cause, also the data stored in the database will be usaed as
    par....
  14. Help Php: How To Load String From Text File (solved)
    Loading string from text file when you click on your link (9)
    I learned the way to load other files with the code posted on this forum. Now i wanted to try
    something for my side menu. I am calling this a string, --> $tekst , maybe it's called
    something else i'm not sure Now let's say i have a file called details.txt In that file i
    would like to have something like this $detailsaboutphp1 = ("details details details 1");
    $detailsaboutphp2 = ("details details details and even more details 2"); How to make a code that
    loads those $strings on click of a mouse. When we tried to load external file we used this:
    ....
  15. Explode Function Help
    need help from you programmers! (1)
    /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Hi I am robert I
    need some help with some php coding. I am stuck with a explode function. Here is the code:
    $username = $check ; $query="SELECT `buildings` FROM `authuser` WHERE `uname` =
    '$username'"; $result=mysql_query($query); $result=mysql_result($result,0);
    list($building1,$building2,$building3,$building4,$building5,$building6,$building7,$building8,$buildi
    ng9,$building10,)=explode('|',$result,'|',$position); I want to explode a mysql
    field into two seper....
  16. Regexp Function Preg_match_all()
    preg_match_all() - Help me (0)
    Hi, I got a new problem which has caused me to go mad but no solution. preg_match_all() - is the
    problem. I have something like this: CODE Test1 Test2 Test3 Test4 Test5 Some
    text.Some text.Some text. Test1 Test1 Test2 Test3 Test4 Test5 Test2 Test3
    Test4 Test5 My RegExp is the following: CODE preg_match_all("/(\ )(.*)(\ )/iUx",
    $text, $matches, PREG_SET_ORDER); Now this function gets 2 matches from the given text : Match
    1: CODE Test1 Test2 Test3 Test4 Test5 Match 2: CODE ....
  17. Question About The Mail() Function
    (2)
    Hi, Is there any way of using the mail() function with an SMTP connection? Is there any way of
    sending messages let's say for example using an email of yahoo? Any help about this woul be very
    thankfull. Thanks in advance.....
  18. How To Enable Mail() Function In Php
    (1)
    im just trying to send mail by using a very simple php function mail() but it is not working.the
    format is CODE $to = "email@example.com"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; if
    (mail($to, $subject, $body)) {   echo(" Message successfully sent! "); } else {   echo(" Message
    delivery failed... "); } I think there is something wrong with php.ini setting..maybe
    something to do with SMTP settings.. i want to send email FROM either hotmail , gmail , or yahoo or
    any other web based email because i dont know my ISP smtp setting..so can anyone please ....
  19. Mailing List Script And Link Thingy.
    (5)
    Hi yar. I am looking for a couple of scripts. I would like a very simple mailing list script which
    i can intergrate into my current website. I need it in php and all i want is subscribe, unsubcribe
    and then a sending page so i can send an email using text only. I have looked in the cpanel but the
    only one there is very complicated, too complicated. Also I would like to create a link box within
    a picture. You will understand better if you visit my website (www.net-fuse.com). The pictures at
    the top have a random script on them, and so i need something which will sele....
  20. Include File.php?id=something
    using the include() function (13)
    Well, I am making a full CMS system for my site, and want to make the index.php file to include the
    view.php?id=1 file. I tried with this code, but it didn't work: CODE This is the error
    I get: CODE Warning: main(view.php?id=1) : failed to open stream: Invalid argument in
    C:\server\xampp\htdocs\test\index.php on line 1 Warning: main() : Failed opening
    'view.php?id=1' for inclusion (include_path='.;C:\server\xampp\php\pear\') in
    C:\server\xampp\htdocs\test\index.php on line 1 So what can I do?....
  21. php header() function help needed
    automatic re-direct (4)
    hey ppl, u seem to have real gud knowledge about php, i just wanted a little help...i designed this
    website, but i want that if i click on certain page, it should open for some few seconds and then
    browser should automatically redirect me to some other page....i tried this with header() function
    but i couldnt do the wait n redirect part, ... so somebody plz help.... -thanx in advance!....
  22. Need Help With Php
    GET function with timer (2)
    I need some help on creating a timer that every thirty minutes, refreshes on a URL. I know how to
    get the page, but I have no idea how to create a timer that initiates it. Could someone point me on
    a helpful direction?....
  23. Question For The If And Echo Function
    (2)
    I'm not that good with PHP, and I tried this code: CODE if ( $_SERVER == ('/')
    )/*'/' is the domain root*/ { echo(' '); } else { echo(' '); }
    However, it doesn't work. So, basically, I want that if the request is at the root (actually
    mysubdomain.domain.com), it will show {I_ONEURL} in the image src. If the request is not at the
    root, but for instance at index.php?showpage=22, it will show {I_ANOTHERURL} in the image src. I
    hope you understand what I want, and can make a new code. And I know I'm not the best PHP pr....
  24. Need Help With The Header() Function
    I am redirecting from my old site (2)
    Over a month ago, I bought a domain name for my site, but my site is still not indexed. I did
    everything needed to get indexed, but I forgot one thing: The old site had exactly the same content
    as the new one. So I had duplicate content. Therefore, I want my old site to redirect the user to
    the new site with this script: CODE header("Location: http://www.global-rs.com" . $_SERVER );
    exit; ?> global-rs.com is my new URL. However, on my old site, which I will be placing this code
    on, there is a problem: It was a free host, not trap17 (I already had another site ....
  25. Sending Attachments Using Email Function In Php
    (2)
    I'm trying to send an attachment using the mail Php function. It gets caught by the email
    server with an error. It seems to have a problem with the separator or who knows what. The server
    says something like "invalid separator on mime type." The code is: Code: CODE  // subject
    $subject  = "Hello There "; $mime_boundary = " >>";        // headers        $headers  =
    "From: " . 'Tom' . " \r\n"; $headers .= "Return-Path: " . 'texample@aol.com' .
    "\r\n"; $headers .= "Reply-To: " . 'texample@aol.com' . "\r\n"; $headers .= "....
  26. Error When Using file_put_contents()
    failed to call to undefined function (5)
    Hey all, I decided to write a script which writes some text to a file, but I have a problem when I
    execute the script, I get a fatal error: QUOTE(homepage) Fatal error : Call to undefined
    function: file_put_contents() in /home/cmatcme/public_html/afile.php on line 55 This is the
    code I'm using to write the file: $ipfnsdoc = "/home/cmatcme/public_html/afolder/afile.txt";
    if (!is_readable($ipfnsdoc)) { echo "File cannot be read"; $stopload = 1; } if
    (!is_writable($ipfnsdoc)) { echo " \nFile cannot be written to"; $stoploa....
  27. Directory Function
    - I need some help (1)
    Hey, I'm really in a jam. Here's whats happening, I have to post over 350 streaming WMA
    files on a server. But it would be nice to not have to script it out in HTML. And I know that in PHP
    you can set a directory or folder and PHP will place all the files out there. Would somone show me a
    way to do that? and please note where I must insert filenames, folder, ect.... So, I have tons of
    files that I need to have on the page, but scrpiting it takes long. Heres the page I'm working
    on: http://www.cbf-wa.org/sermons.php thanx so much....
  28. Need Feedback On A Function
    (1)
    I'm working hard on a fully self-coded community (after tons of errors you get depressed i
    know...but i need to see what i can), so i need some feedback on a Function i made (wich is
    basically reguser.php), here it is: CODE Function regresult($uname, $pword, $cword, $rname,
    $country, $day, $month, $year, $gender, $email, $email2){ Global $db; $datab="Users";
    if(empty($uname or $pword or $rname or $country or $day or $month or $year or $gender or $email)){
    exit("Please fill in every field."); } if($pword=!$cword){ exit("Your passwords do not match."); }
    if($email....
  29. Help Importing Mysql Database Results
    Need help formatting link (3)
    I am having trouble formatting my table to display the results of a mySQL query the way I want.
    Here is the part of my code: CODE $result = mysql_query("SELECT * FROM uploads WHERE
    Category='Drama'") or die(mysql_error()); ?> Title Author File // keeps getting
    the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // echo "
    "; echo $row ; echo " "; echo $row ; echo " "; echo $row ; echo " "; } echo " "; I want to
    make 'Title' show as a hyperlink to 'File' but am at a loss as to how to d....
  30. Getting List Of Directories And Files Using Php
    PHP Function for Directory and File List (6)
    is there a php function that lists the content of some folder.... example: /New folder new.txt
    left.gif download.zip dc.exe ....so is there..? /rolleyes.gif' border='0'
    style='vertical-align:middle' alt='rolleyes.gif' /> ....

    1. Looking for link, call, function, php
Similar
Search Engine/ Function
PHP Function To Add Previous and Next Page Feature - useful php function
Endif function?
Arrays Outside A Function - Need to have arrays available to all functions.
How To Check If Fsockopen Function Is Enabled?
Php Explode Function Help
The Best Zip Function
Mail() Clone - A PHP mail() function clone
[php](simple) Using Functions To Combine Values In A Form - Really simple example on how to combine values with function
[php] Header Function
Error With Joomla Template - cant find function
The Extract() Function - Something I just found out
How Good Is This Data Cleaning Function?
Help Php: How To Load String From Text File (solved) - Loading string from text file when you click on your link
Explode Function Help - need help from you programmers!
Regexp Function Preg_match_all() - preg_match_all() - Help me
Question About The Mail() Function
How To Enable Mail() Function In Php
Mailing List Script And Link Thingy.
Include File.php?id=something - using the include() function
php header() function help needed - automatic re-direct
Need Help With Php - GET function with timer
Question For The If And Echo Function
Need Help With The Header() Function - I am redirecting from my old site
Sending Attachments Using Email Function In Php
Error When Using file_put_contents() - failed to call to undefined function
Directory Function - - I need some help
Need Feedback On A Function
Help Importing Mysql Database Results - Need help formatting link
Getting List Of Directories And Files Using Php - PHP Function for Directory and File List

Searching Video's for link, call, function, php
See Also,
advertisement


How To Use A Link To Call Function In Php?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com