SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to figure out how to loop through a array for a forms action. Say the user has a number of choices, those choices are put into an array and then loop through that array giving the form a different action for each choice, so that the variables are passed to a script pertinent to their choice or choices.

    form input:
    <input name="the_array[]" value="script_one">one
    <input name="the_array[]" value="script_two">two
    <input name="the_array[]" value="script_three">three
    ect...

    And in the form statment,
    <form action="<?php echo ($array) ?>">

    Choices would be put into an array, counted then looped through the array until all are done. The big question is, would this work with only hitting the submit button once, if they chose more than one? Or how could it be done to work that way? Can any one point me in the direction of an example or where to start?
    Thank you.

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ronnie,

    for those reading, this is a continuation of this thread http://www.sitepointforums.com/showt...threadid=19463

    your form tag should look something like this...

    <form name="formName" method="post" action="urlOfPhpScript.php">

    Then when the user hits the SUBMIT button in the form the whole array is posted to the script identified in the form tag.

    Now you can loop through the array in a manner similar to what was discussed in the previous thread.

    Does this help?

  3. #3
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your right Freaky, I should have left this in the last thread. I was thinking it was a different question, thats the reason for the new thread..

    The reason I was asking this question is, that I did'nt think the code you gave would work, not that I'm an expert. I was thinking wrong in the fact, that I was not trying to pass to custom functions, but rather to actual mini seperate scripts.
    Not the first time I thought wrong..

    Since I was trying to run scripts, depending on what the user chose, I did'nt think it would work with your code, so a friend suggested the above set-up.

    Of course AFTER starting this thread, I came apon the "exec" function. So I was thinking this code might work, from what you gave me:

    foreach ($petArray as $pet) {
    switch ($pet) {
    case "dog" :
    exec ("woof.php");
    break;
    case "cat" :
    exec ("meow.php");
    break;
    case "fish" :
    exec ("swish.php");
    break;
    }
    }

    What I have to find out though is, if the form input will be passed to the script(s), if called for. Also if the exec runs each script(s) one at a time until all are done. And also if the "exec" function outputs, say script(s) echo's to the screen, for say, results, errors, ect.

    From reading the php manual, it should do as I need. Or am I incorrect? Any feedback would be appreciated!
    Thanks.

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the "exec" function did not work as I had hoped. I did however find that the "include" function seems to work.
    Also I found a easier way to do what I wanted, for anyone that might have a similar situation.

    $cnt = count($petArray);
    for ($id = 0; $id < $cnt; ++$id){
    include ("$petArray[$id]" . ".php");
    }

    Does the same as Freaky suggested, but a little less code. Unless some one can point out any pitfalls, or problems I could have. I've tested it and it works great.

    It's a great use for a form that you want multiple actions, or passing variables to script(s) given a users choice(s).

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •