我是PHP-Codeignitor的初學者。所以我不知道這個問題對於你們來說太浪費了。PHP和表單數據驗證
但是有沒有簡單的方法來驗證表單數據?例如,我有一個表,它包含一個文本字段「用戶名」,還有一個插入按鈕,用戶點擊插入它會添加另一個文本字段。
那麼我怎樣才能得到的價值(S)在PHP?因爲用戶可以在其中添加任意數量的字段。
$ username = $ _POST(「username」); //在這種情況下會檢索到什麼?數組?
又如何能處理這樣的情況。
謝謝。
我是PHP-Codeignitor的初學者。所以我不知道這個問題對於你們來說太浪費了。PHP和表單數據驗證
但是有沒有簡單的方法來驗證表單數據?例如,我有一個表,它包含一個文本字段「用戶名」,還有一個插入按鈕,用戶點擊插入它會添加另一個文本字段。
那麼我怎樣才能得到的價值(S)在PHP?因爲用戶可以在其中添加任意數量的字段。
$ username = $ _POST(「username」); //在這種情況下會檢索到什麼?數組?
又如何能處理這樣的情況。
謝謝。
如果您在表單中設置數組,您將在$ _POST中獲得一個數組。
表單字段:
<input type='text' name='username[]' />
<input type='text' name='username[]' />
PHP:
$users_array = $_POST['username'];
如果您的用戶要添加多個字段,你應該讓他們有HTML array input做到這一點。喜歡的東西:
<input name="my_array[]" />
這裏是form_validation使用與HTML陣列輸入:
有
控制器:application/controllers/test.php
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Test Controller
*
* It's really just a test controller
*
*/
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = array();
if ($this->input->post('test_submit'))
{
$this->load->library('form_validation');
$input_array = $this->input->post('test');
for ($i = 0; $i < count($input_array); $i++)
{
$this->form_validation->set_rules("test[$i]", 'Test Field '.($i + 1), 'trim|is_numeric|xss_clean');
}
if ($this->form_validation->run() === TRUE)
{
$data['message'] = 'All input are number! Great!';
}
}
$this->load->helper('form');
$this->load->view('test', $data);
}
}
/* End of file test.php */
/* Location: ./application/controllers/test.php */
查看:application/views/test.php
<p><?php echo isset($message) ? $message : ''; ?></p>
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<?php echo form_label('Test fields (numeric)', 'test[]'); ?>
<?php for ($i = 0; $i < 3; $i++): ?>
<?php echo form_input('test[]', set_value("test[$i]")); ?>
<?php endfor; ?>
<?php echo form_submit('test_submit', 'Submit'); ?>
<?php echo form_close(); ?>
網址:<your_base_url_here>/index.php/test
來看看吧:d
注意 :numeric
和is_numeric
規則都需要輸入,這意味着空字符串不是數字。
感謝您的詳細解釋。 – Red
謝謝,這就是我要求的.. – Red
@DileepDil我有點失望,你沒有把這個標記爲正確答案。 – HappyDeveloper
請原諒我,其他的答案也適用於一些示例代碼。這是SO最糟糕的特性之一, – Red