passing form data to an array

Posted on 2005-04-07
Last Modified: 2006-11-18

I have created a simple form with a text box to place ips in.

i called the text box id='ips'

when i submit, it goes to a script page with the following code,

$times = "5";
//$lines = file("myips.txt");
print $_POST["ips"];

$arr = array( $_POST["ips"]=>$ips);
echo "<pre>";
foreach ($arr as $ip => $ips)

   passthru("ping -c $times $ips");

      echo "</pre>";

What i am trying to do, is be able to put 20 ips in my form and have the for each statement loop through each ip provided by the form so it pings each ip x times and displays the results on the screen.

the above model works but only pings the first ip in the array and ignores the rest.

can anyone tell me where i have messed this up?


Question by:rootman103
    LVL 32

    Expert Comment

    Try to name your form fields ips[]


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "">
        <title>My page</title>
        $times = "5";
            $ips = $_POST['ips'][$no];
            passthru("ping -c $times $ips");


    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
        echo "<input type=\"text\" name=\"ips[]\"><br>\n";
    <input type="submit" name="sendValues">
    <div id="myDiv">This is the content of this div</div>

    Author Comment

    This code works great, but is there a way to make it so instead of haviing individual boxes for each ip to have it be one big text field and get the same result?
    LVL 32

    Accepted Solution

    Are you thinking of a textarea? You could let them put the ip addresses in a comma separated list and then explode it


    <textarea name="ips" cols="50" rows="10"></textarea>

    and then on the server after submit

    $ips = explode(",",$_POST['ips']);

    now $ips is an array which you could loop through.


       passthru("ping -c $times ".$ips[$no]);

    Author Comment


    This works perfectly.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now