[英]how to get multiple values from single text box and store it in array and also store it in database
Can I use php to get multiple values from one input text box and store it in array? 我可以使用php从一个输入文本框中获取多个值并将其存储在数组中吗? I want to read multi integers from the user and store it in array For example, I add one input text and one submit:
我想从用户那里读取多个整数并将其存储在数组中,例如,我添加了一个输入文本和一个提交:
You could do it in this way. 您可以通过这种方式进行。
Make an form in HTML having an input which should take comma seperated integer values like this: 在HTML中制作一个具有输入的表格,该输入应采用逗号分隔的整数值,如下所示:
<form action="url" method="POST">
<input id="dataInput" type="text" name="data" value="12,34,56">
<button id="submitBtn">Submit</button>
</form>
Now on submitting the form, you should receive the data in PHP, and you can use php's explode
method like this: 现在提交表单时,您应该使用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
So the output for the $data_arr
would be: 因此,
$data_arr
的输出为:
$data_arr = array(
0 => '12',
1 => '34',
2 => '56'
);
Hope this helps! 希望这可以帮助!
Use explode () function Then array_walk () function to make sure you get the NUMERIC values 使用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.