<form action="form.php" id="form">
<input type="text" name="name_1"><input type="text" name="city_1"><input type="text" name="country_1">
<input type="text" name="name_2"><input type="text" name="city_2"><input type="text" name="country_2">
<input type="text" name="name_3"><input type="text" name="city_3"><input type="text" name="country_3">
<input type="submit">
</form>
How is the best method for get this data in form.php file? This is generated with jQuery. Can be 3 (as now) or 30.
In form.php i have:
$data = new Data();
$data->name = $_POST['name_1'];
$data->city = $_POST['city_1'];
$data->country = $_POST['country_1'];
$data->save();
$data = new Data();
$data->name = $_POST['name_2'];
$data->city = $_POST['city_2'];
$data->country = $_POST['country_2'];
$data->save();
$data = new Data();
$data->name = $_POST['name_3'];
$data->city = $_POST['city_3'];
$data->country = $_POST['country_3'];
$data->save();
But if there is over 3? I would like use foreach , but how? How can i generated input or get this data?
Use name[]
, city[]
and country[]
as your input names. Then they'll come through to PHP as arrays which you can iterate with foreach
.
You can use a for loop:
for ($i = 1; $i <= 3; $i++) {
$data = new Data();
$data->name = $_POST['name_'.$i];
$data->city = $_POST['city_'.$i];
$data->country = $_POST['country_'.$i];
$data->save();
}
But if you could change your html like:
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
You will not need to know the count.( name_1,name_2
would be better to be the id but not name)
foreach ($_POST['name'] as $index => $name) {
$data = new Data();
$data->name = $name;
$data->city = $_POST['city'][$index];
$data->country = $_POST['country'][$index];
$data->save();
}
您可以将每个带有按时构建的字符串(例如“ name _。$ i”)用于$ i作为计数器
If you can change the names of the input elements you could do it like that:
<input type="text" name="items[1][name]"><input type="text" name="items[1][city]"><input type="text" name="items[1][country]">
<input type="text" name="items[2][name]"><input type="text" name="items[2][city]"><input type="text" name="items[2][country]">
<input type="text" name="items[3][name]"><input type="text" name="items[3][city]"><input type="text" name="items[3][country]">
The numbers don't have to be adjacent but the have to be unique. To convert the $_POST
-data to your data-model would then look like that:
$items = $_POST['items'];
$dataList = array();
foreach($items as $item) {
$data = new Data();
$data->name = $item['name'];
$data->city = $item['city'];
$data->country = $item['country'];
$dataList[] = $data;
}
Of course you can add as many items as you want.
If you can't change the names of the input elements then you could use one of the other solutions. I would only modify them so that the number of elements is not hard coded like:
$dataList = array()
for($i = 1; isset($_POST["name_$i"]); ++$i) {
$data = new Data();
$data->name = $_POST["name_$i"];
// ... and so on
$dataList[] = $data;
}
Of course, with that solution the numbers must be continuous/adjacent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.