简体   繁体   English

如何从单个文本框中获取多个值并将其存储在数组中以及如何将其存储在数据库中

[英]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.

相关问题 如何同时获得选择框的文本和值并将其存储到数据库中? - How to get both the text and value of a select box and store it into the database? 如何从文本框中获取元素并将其存储在数组中 - How to fetch elements from a text box and store them in an array 如何在数据库中存储文本框的值? - How to store the value of a text box in a database? 如何获取输入框值和选择框值以将其存储在数组中并进行计算 - How to get input box values and the select box values to store it in array and do calculation 如何从多个 url 中获取数据并将其存储在 javascript 中的单个数组中? - How to fetch data from multiple url and store it in single array in javascript? 将多个值从单个输入框存储到本地存储并提交按钮并将它们返回给其他按钮 - Store multiple values to local storage from a single input box and submit button and return them to feed other buttons 如何在(“输入”)上获取多个值并将它们存储在数组中 - How to get multiple values on("input") and store them in an array JavaScript将文本框存储中的值放入数组中以创建新数组进行排序 - JavaScript Taking in values from text box store into an array create new array to sort 如何获取和替换多个跨度元素的值(来自标记字符串)并将它们存储在数组中 - How to get and replace values of multiple span elements (from a string of markup) and store them in an array 将动态创建的文本框值按顺序存储在 javascript 数组中 - Store dynamically created text box values in an javascript array in an order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM