I have two fields in a form (ip:port) seperately. I want to add them together to make one string eg 127.0.0.1:11111 to enter into a database. At the moment I have this form.
<ul id="textlist">
<li>IP: <input type="text" maxlength="15" size="15" name="ip[]" value="" />:<input type="text" name="port[]" maxlength="5" size="5" value="27015" /></li>
</ul>
Which submits to this for parsing.
$ip = array();
foreach ($_POST['ip'] as &$value) {
if ($value != "") {
if (preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}/", $value)) {
$ip[] = addslashes(htmlentities($value));
} else {
$error = 'Invalid IP Address. Please go back and try again.';
}
}
}
$port = array();
foreach ($_POST['port'] as &$value) {
if ($value != "") {
$port[] = addslashes(htmlentities($value));
}
}
}
I then end up with 2 arrays of ip’s and ports (up to 5 of each) which I want to join together in pairs before adding to a database.
Thanks for any help!
this will work as long as your arrays are perfectly parallel. you might consider adding numeric indexes to your HTML form instead of depending on ordering.