Need help writing own functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dariusz

    #1

    Need help writing own functions

    I would like to try writing my own functions, so as to keep some code
    within the same PHP file rather than make multiple PHP files.

    What I am having a problem is is how to call a function when a php
    generated HTML form is "submitted" and to put the variables back into
    another function within the same PHP file.

    I have figured how to pass variables to a basic test function I wrote, but
    not how to call a function within the same PHP file AND from a form.

    Can't seem to find any good examples online.

    Any help appreciated.

    Dariusz
  • Jedi121

    #2
    Re: Need help writing own functions

    I'm not sure you understood the "Server side" of the PHP.
    What you can do is a HTML Form that points to the script itself (use
    $_SERVER['REQUEST_URI'] in the action param of the form)
    and add some hidden var if needed to know where you come from.

    You can not call a PHP function with a 'Submit' button whitout sending
    the form.
    If you need such behaviour you should try JavaScript instead.


    Comment

    • Dariusz

      #3
      Re: Need help writing own functions

      In article <mesnews.bd3f7d 42.3acfac5b.568 .2689@free.fr.R emovethis>, Jedi121 <jedi121news@fr ee.fr.Removethi s> wrote:[color=blue]
      >
      >You can not call a PHP function with a 'Submit' button whitout sending
      >the form.
      >If you need such behaviour you should try JavaScript instead.[/color]

      I don't know if I explained it properly, so I give it another shot.

      If you have a HTML form, you can call a PHP file and post or get all the
      variables from the form into the PHP file to do with as you please. What
      I'd like to know is is it possible to call and execute a function within
      the PHP file, and pass all the variables at the same time?

      Trying to stay clear of Java / Javascript and anything like that. If
      anything, I just keep the PHP files seperate instead of having one big file
      with functions (I should learn how to do functions - and this script
      should suit).

      Dariusz

      Comment

      • Chung Leong

        #4
        Re: Need help writing own functions

        Not really. A request always goes to a file. It's up the code in the global
        scope in the file to call the functions.

        In general, it's better to associate one "web-page" with one file. Take a
        look at the source of www.php.net. Just scroll down and click the "show
        source" link. Very good example of how to structure a site in a straight
        forward manner.

        Uzytkownik "Dariusz" <ng@lycaus.plus YOURSHIT.com> napisal w wiadomosci
        news:G5v_b.9693 $h44.1085803@st ones.force9.net ...[color=blue]
        > In article <mesnews.bd3f7d 42.3acfac5b.568 .2689@free.fr.R emovethis>,[/color]
        Jedi121 <jedi121news@fr ee.fr.Removethi s> wrote:[color=blue][color=green]
        > >
        > >You can not call a PHP function with a 'Submit' button whitout sending
        > >the form.
        > >If you need such behaviour you should try JavaScript instead.[/color]
        >
        > I don't know if I explained it properly, so I give it another shot.
        >
        > If you have a HTML form, you can call a PHP file and post or get all the
        > variables from the form into the PHP file to do with as you please. What
        > I'd like to know is is it possible to call and execute a function within
        > the PHP file, and pass all the variables at the same time?
        >
        > Trying to stay clear of Java / Javascript and anything like that. If
        > anything, I just keep the PHP files seperate instead of having one big[/color]
        file[color=blue]
        > with functions (I should learn how to do functions - and this script
        > should suit).
        >
        > Dariusz[/color]


        Comment

        • Fred H

          #5
          Re: Need help writing own functions

          > If you have a HTML form, you can call a PHP file and post or get all the[color=blue]
          > variables from the form into the PHP file to do with as you please. What
          > I'd like to know is is it possible to call and execute a function within
          > the PHP file, and pass all the variables at the same time?[/color]

          When you parse the arrays containing the POST and GET vars, you can make
          your own array, containing all the variables you want to keep, like this:

          $input = array();
          foreach(incommi ng_vars as $key => $val) {
          $input[$key] = $val
          }

          The above code is of course just pseudo code, and you'd have to either
          find or write your own function that actually parses the input and stores
          it like I've tried to describe. (The "parse_incoming ()" function that the
          Invision Board crew has made, is really good.)

          Now that you've got all incoming vars in an array, you can either pass that
          array to your fu like this:

          $retval = myfu($input);

          Or you could simply tell your fu to use the global var $input:

          function myfu() {
          global $input; //Tells the fu that it can use the global var $input
          return $input['a'] + $input['b']; //Or something else...
          }

          To get all incoming vars and store them in an array is easy.
          The trick is to find a way of doing it that secures that no
          "bad" variables has snuk in there...

          --
          Fred H

          void FredH::Contact( ) {
          TextToSpeach.sa y("frode at age dee dee dot en oh");
          }

          Comment

          Working...