[英]How to get only the IP address / subnet
我正在使用一個jquery插件,它掩蓋了我的字段,只允許用戶鍵入正確的值。 它工作得很好,但是現在當我提交表單時,對象IP是一個數組,我只需要檢索IP號(10.1.25.2/30)
結果:
$ip = $_POST['ipaddress'];
print_r($ip);
//It returns to me
Array ( [abcd] => Array ( [0] => 10.1.25.2/30 ) )
我需要內爆Array或以這種方式做一些事情。
整個代碼:
HTML頁面jQuery插件包括:
<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>
現在我的Action PHP代碼:
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;
}
就這些!
它是一個簡單的多維數組,您可以像這樣檢索值:
$ip = $_POST['ipaddress'];
$justip = $ip['abcd'][0];
現在, $justip
變量中只有IP地址。
您也許應該閱讀有關數組的文檔: http : //ch.php.net/manual/en/language.types.array.php
此頁面上有多少個表格?
它顯示在數組中而不是$ _POST ['ipaddress']中的原因是: <input name="ipaddress[abcd][]" id="ip" type="text" value="<?=$ip;?>" />
格式的html。 [abcd][]
使它使ipaddress成為一個包含鍵為'abcd'的數組的數組,該鍵的第一個可用鍵將填充表格中的值。 如果您做了<input name="ipaddress" id="ip" type="text" value="<?=$ip;?>" />
則IP地址可以直接通過$_POST['ipaddress']
。
如果您有多種形式,請使用name="ipaddress[]"
,每種形式都會填充$_POST['ipaddress']
數組的鍵,因此$_POST['ipaddress'][0]
將是第一種形式, $_POST['ipaddress'][1]
將是第二種形式,依此類推。
順便說一句,如果您從
<input name="ipaddress[abcd][]" id="ip" type="text" value="<?=$ip;?>" />
至
<input name="ipaddress[abcd]" id="ip" type="text" value="<?=$ip;?>" />
您將擺脫數組(注意從輸入的name屬性中刪除的[]
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.