Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    161
    Thanks
    71
    Thanked 1 Time in 1 Post

    Passing specific data

    Hey. I have a html form and at the moment the inputs are to, from and depart 4 times e.g.
    Code:
        <fieldset class="login">
            <legend>Sector 1</legend>
            <div>
                <label for="from">From</label> <input type="text" id="from" name="from">
            </div>
            <div>
                <label for="to">To</label> <input type="text" id="to" name="to">
            </div>
            <div>
                <label for="depart">Depart</label> <input type="text" name="depart" id="depart" />
            </div>
        </fieldset>
        <fieldset class="login">
            <legend>Sector 2 </legend>
            <div>
                <label for="from2">From</label> <input type="text" id="from2" name="from2">
            </div>
            <div>
                <label for="to2">To</label> <input type="text" id="to2" name="to2">
            </div>
            <div>
                <label for="depart2">Depart</label> <input type="text" name="depart2" id="depart2" />
            </div>
        </fieldset>
    ...
    Now each of these segments should be treated like a group, all three inputs need to be set in order for the group to be valid. Now in my php class for the submit, I create an array for each segment e.g.
    Code:
    $seg1 = array();
    I then proceed to check if they have been set e.g.
    Code:
    if (isset($_POST['from'], $_POST['to'], $_POST['depart'])) {
        $seg1[] = $_POST["from"];
        $seg1[] = $_POST["to"];
        $seg1[] = $_POST["depart"];
    }
    Now I need to pass the segments to a function if they have been properly set. So I have my function call
    Code:
    getListingsViaSoap();
    If $seg1 is set, I need to pass it that. If $seg1 and $seg2 are set, I need to pass both of them to the function etc. Whats the best way to do this and is my approach so far ok?

    Thanks
    Last edited by nick2price; 10-27-2013 at 09:38 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,296
    Thanks
    13
    Thanked 345 Times in 341 Posts
    I’d modify the HTML to pre-group them (way easier to handle later):
    PHP Code:
        <fieldset class="login">
            <
    legend>Sector 1</legend>
            <
    div>
                <
    label for="from">From</label> <input type="text" id="from" name="sector[0][from]">
            </
    div>
            <
    div>
                <
    label for="to">To</label> <input type="text" id="to" name="sector[0][to]">
            </
    div>
            <
    div>
                <
    label for="depart">Depart</label> <input type="text" name="sector[0][depart]" id="depart" />
            </
    div>
        </
    fieldset>
    // use index 1 for the next group 
    then in PHP you have it already structured.

    PS. since all inputs are text fields, they will always be passed (even with an empty string). so you should test with empty() or filter_input() rather than isset().
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    nick2price (10-29-2013)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Although definitely valid to the question, I believe the OP actually has two questions without knowing it. The first is that there appears to require multiple input, but the second is how to dynamically call it.
    Although PHP itself is based on vararg, the problem is the receiving end of that function needs to be able to differentiate its input parameters to filter out the null. Since I'm guessing its a remote function call given the name, the best way is to provide it with an array and use a call func:
    PHP Code:
    $aInput = array(); // store the non-nulls in order here
    call_user_func_array('getListingsViaSoap'$aInput); 
    Simple as that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    nick2price (10-29-2013)

  • #4
    New Coder
    Join Date
    Jun 2013
    Location
    The Republic of Texas
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    To put it all together, I would use Dormilich's naming name="data[0][from]", etc.. and then Fou-Lu's call:

    PHP Code:
    if(!empty($data array_filter($_POST['data']))) {
        
    $result call_user_func_array('getListingsViaSoap'$data);


  • Users who have thanked AbraCadaver for this post:

    nick2price (10-29-2013)


  •  

    Posting Permissions

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