我有以下功能:
function si_ad_call_models(){
// title of the page
echo \'<h2>Add new ad call model</h2>\';
?>
<form action="" method="post">
<label for="add">Enter name of ad model: </label>
<input type="text" name="new_model_name"/>
<input type="submit" name="submit" value="Add your new ad model"/>
</form>
<?php
if(isset($_POST[\'submit\'])){
add_option(\'si_ad_call_model\', serialize($_POST[\'new_model_name\']));
}
$myopt = unserialize(get_option(\'si_ad_call_model\'));
}
变量$myopt输出我在文本字段中输入的第一个内容。问题是如何在数组中添加值,并且每次提交表单时都应该更新选项值?这可能吗?
最合适的回答,由SO网友:Charles Clarkson 整理而成
像这样的事情应该行得通。
function si_ad_call_models() {
if ( isset( $_POST[\'submit\'] ) ) {
// Checking that $_POST[\'new_model_name\'] is set is probably not enough validation.
if ( isset( $_POST[\'new_model_name\'] ) ) {
// Get the stored models.
if ( get_option( \'si_ad_call_model\' ) )
$si_ad_call_models = unserialize( get_option( \'si_ad_call_model\' ) );
else
$si_ad_call_models = array();
// Add the new model to the end of the models array.
// @TODO Clean $_POST[\'new_model_name\'] before adding it to database.
$si_ad_call_models[] = $_POST[\'new_model_name\'];
// Store the updated array of models.
if ( update_option( \'si_ad_call_model\', serialize( $si_ad_call_models ) ) ) {
// @TODO Tell user about success.
} else {
// @TODO Tell user about failure.
// @TODO Log database update failure.
}
} else {
// @TODO Add validation error code.
}
}
// Title of the page.
echo \'<h2>Add new ad call model</h2>\';
?>
<form action="" method="post">
<label for="add">Enter name of ad model: </label>
<input type="text" name="new_model_name"/>
<input type="submit" name="submit" value="Add your new ad model"/>
</form>
<?php
}
我没有完全测试此代码。