Some PHP function help

Discussion in 'PHP' started by blueparukia, Oct 29, 2007.

  1. #1
    At the moment my code is something like this:
    
    <form action="" method="post">
    Form Stuff
    </form>
    
    <?php
    function name()
    {
    echo "Stuff"
    }
    PHP:
    OK, so I want it so when I submit the form, it executes name(). What should I specify for form action? I tried ?action=name, but I feel I am missing something here,

    thanks,

    BP
     
    blueparukia, Oct 29, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    This is a common mistake.

    You can not mix PHP with Javascript like this. PHP is on your server, while javascript is on the client's browser. You will have to submit the form to the server so PHP can validate it (or to execute PHP functions in general).

    Either you do that, or you ask in the javascript section if whatever you're trying to do can be accomplished with javascript, and not PHP.

    (Note that is you want to validate the form, you HAVE to use PHP because javascipt is not secure)
     
    nico_swd, Oct 29, 2007 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    I'm not using any Javascript, but I understand where you are coming from.

    So how do I execute functions like on any CMS software by using ?functionname?

    For example the sNews CMS function goes: index.php?login for the login page and vBulletin uses showthread.php?t=threadID.

    So how can I use it like that, would it involve .htaccess? I want to keep the amount of files in my script as minimal as possible,

    BP
     
    blueparukia, Oct 29, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Sorry, I misunderstood your question.

    The action attribute tells the browser where to send the data to. (file name or URL of the page).

    To execute the function when the form is sent, do something like:
    
    if (isset($_POST))
    {
        name();
    }
    
    PHP:
     
    nico_swd, Oct 29, 2007 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    Thank you :D Rep will be given,

    Cheers,

    BP
     
    blueparukia, Oct 29, 2007 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    Sorry for double posting.

    This does work, but I also want to be able to recognise wat for its being posted from. Say I have three forms in different files, and each one has a different result when I submit it. I tried giving the form a name and the using ['formname'] after isset($_POST)


    Cheers,

    BP
     
    blueparukia, Oct 30, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Add a hidden field to the forms:
    
    <input type="hidden" name="formname" value="My form 1" />
    
    HTML:
    And in your script receive the value with:
    
    $form_name = $_POST['formname'];
    
    PHP:
     
    nico_swd, Oct 30, 2007 IP
  8. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #8
    Thank you :)

    BP
     
    blueparukia, Oct 30, 2007 IP