• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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
0
rootman103
Asked:
rootman103
  • 2
  • 2
1 Solution
 
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
 
BatalfCommented:
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
 
rootman103Author Commented:
Awesome.

This works perfectly.

Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now