![](/img/trans.png)
[英]How to get both the text and value of a select box and store it into the database?
[英]how to get multiple values from single text box and store it in array and also store it in database
我可以使用php從一個輸入文本框中獲取多個值並將其存儲在數組中嗎? 我想從用戶那里讀取多個整數並將其存儲在數組中,例如,我添加了一個輸入文本和一個提交:
您可以通過這種方式進行。
在HTML中制作一個具有輸入的表格,該輸入應采用逗號分隔的整數值,如下所示:
<form action="url" method="POST">
<input id="dataInput" type="text" name="data" value="12,34,56">
<button id="submitBtn">Submit</button>
</form>
現在提交表單時,您應該使用PHP接收數據,並且可以使用php的explode
方法,如下所示:
<?php
$data = $_POST['data'];
$data_arr = explode(',', $data); // <----- explode the string here from commas
// Now you can store these values into the database
因此, $data_arr
的輸出為:
$data_arr = array(
0 => '12',
1 => '34',
2 => '56'
);
希望這可以幫助!
使用explode ()函數,然后使用array_walk ()函數以確保獲得NUMERIC值
<?php
// Assuming your method is POST
// and assuming the delimiter (entered by user) is UNITED as a COMMA ","
$_POST['input_name'] = explode(",", $_POST['input_name']);
$_POST['input_name'] = array_walk($_POST['input_name'], 'floatval');
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.