passing form data to an array

Experts,

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?

THanks,

Rootman103
rootman103Asked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
Are you thinking of a textarea? You could let them put the ip addresses in a comma separated list and then explode it

example:

<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.

for($no=0;$no<count($ips);$no++){

   passthru("ping -c $times ".$ips[$no]);
}
0
 
BatalfCommented:
Try to name your form fields ips[]

Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>My page</title>
</head>
<body>
<?
if(isset($_POST['sendValues'])){
    $times = "5";
    for($no=0;$no<count($_POST['ips']);$no++){
        $ips = $_POST['ips'][$no];
        passthru("ping -c $times $ips");
       
    }


   
}

?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<?
for($no=0;$no<10;$no++){
    echo "<input type=\"text\" name=\"ips[]\"><br>\n";
}
?>
<input type="submit" name="sendValues">
</form>
<div id="myDiv">This is the content of this div</div>
</body>
</html>
0
 
rootman103Author Commented:
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?
0
 
rootman103Author Commented:
Awesome.

This works perfectly.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.