How to get only the IP address / subnet

蓝咒 提交于 2019-12-08 05:57:21

问题


I'm using a jquery plugin, it maskered my field and allow the user only to type the correct values. It is working very well, but now when I submit my form, the object IP is an Array, I need to retrieve only the IP number (10.1.25.2/30)

The result:

$ip = $_POST['ipaddress'];
print_r($ip);

//It returns to me
Array ( [abcd] => Array ( [0] => 10.1.25.2/30 ) ) 

I need implode the Array or do something on this way.. help please.!

The Whole Code:

HTML Page jQuery plugins included:

<script src="scripts/jquery.min.js" type="text/javascript">
<script src="jquery.validate/jquery.caret.js" type="text/javascript"></script>
<script src="jquery.validate/jquery.ipaddress.js" type="text/javascript"></script>

    <script>
        $(function(){
        $('#ip').ipaddress({cidr:true});
        });     
    </script>

<form name="form1" method="post" action="equipAction.php">
 <tr>
  <td>IP</td>
  <td><input name="ipaddress[abcd][]" id="ip" type="text" value="<?=$ip;?>" />
  <b>»» IP atribuído ao contrato do Cliente/Torre </b> </td>    
 </tr>
 <input type='submit' name="alt" value="Edit" class="btn" />
</form>

Now my Action php code:

 if ($_POST["alt"] == "Edit") {

    # Dados do form
    $idequip  = $_GET['id'];
    $contrato = $_POST['contrato'];
    $transmi  = $_POST['transmissor'];
    $ip       = $_POST['ipaddress'];
    $local    = $_POST['local']; //tipo_equip
    $obs      = $_POST['obs'];
    $usado    = $_POST['usado'];

    echo "<br />";
    print_r($ip);




    # Atualiza dados do equipamento mestre
    # desenvolvendo
    $res = mysql_query("UPDATE equipment SET idtorre='$transmi', ip='$ip', tipo_equip='$local', obs='$obs', usado_cliente='$usado' WHERE id='$idequip'") or die("Erro na query: atualização equipamento mestre.");
    #header("Location: equipamento_adm.php?return=3&ip=$control");
    exit;
 }

That all!


回答1:


It a simple multidimensional array, you can retrieve your value like this :

$ip = $_POST['ipaddress'];    
$justip = $ip['abcd'][0];

Now there's only the ip address in the $justip variable.

You should maybe read the documentation about arrays : http://ch.php.net/manual/en/language.types.array.php




回答2:


How many forms are on this page?

The reason that it's showing up in an array and not in $_POST['ipaddress'] is this: <input name="ipaddress[abcd][]" id="ip" type="text" value="<?=$ip;?>" /> in the form html. The [abcd][] causes it to make ipaddress an array containing an array with a key of 'abcd' of which the first available key gets populated with the value from the form. If you did <input name="ipaddress" id="ip" type="text" value="<?=$ip;?>" /> then the IP address would be available directly through $_POST['ipaddress'].

If you have multiple forms, then use name="ipaddress[]" and each form will populate a key of the $_POST['ipaddress'] array, so $_POST['ipaddress'][0] will be the first form and $_POST['ipaddress'][1] will be the second form and so on.




回答3:


By the way, if you switch from

<input name="ipaddress[abcd][]" id="ip" type="text" value="<?=$ip;?>" />

to

<input name="ipaddress[abcd]" id="ip" type="text" value="<?=$ip;?>" />

you will get rid of the array (notice the removed [] from the input's name attribute).



来源:https://stackoverflow.com/questions/5181133/how-to-get-only-the-ip-address-subnet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!