简化使用dabase数据和错误检查预填充的PHP表单,因此主动使用$ _POST数据覆盖 [英] Simplifying PHP forms that pre-populate with dabase data and error check, so actively overwrite with $_POST data

查看:275
本文介绍了简化使用dabase数据和错误检查预填充的PHP表单,因此主动使用$ _POST数据覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以这是一种常见的情况。



您有一个涉及编辑信息的HTML表单。原始信息来自数据库。当您发布表单时,它可能不会立即保存信息,因为在完成数据检查时可能需要修复,可能有一个必填字段留空。因此,您要重新显示表单字段,但如果有发布数据,则显示发布数据,如果没有,则显示数据库中的原始数据。



所以我创建了一个函数检查post,然后默认一些任意数据(在这种情况下从数据库)。



但总的来说,方法感觉不起眼,POST数据被隐藏在函数内部的一个全局的,但如果我传递的数据在我不得不传递它在每个函数调用,它几乎一样冗长,只是用手每次,所以具体我看对于这种方法的替代品,通常我会喜欢建议更好的方式来处理这种形式场景,我处理每一次我编辑html表单。

  //从post或get,或者使用数据,例如从数据库,填充表单。 
function in_or_data($ index,$ data,$ trim = false){
return $ _POST [$ index]? ($ trim?trim($ _ POST [$ index]):$ _POST [$ index]):$ data [$ index];
}

<?php
$ item_name = in_or_data('item_name',$ data_from_database_somewhere); // pull post data,with defaults comes from the
?>

// .....稍后,一些示例html只是转义&回显数据。 .....
< td id ='item-name'>< input name =item_nametype =textid =item_namevalue =<?php echo escape item_name);?> size =47maxlength =100tabindex =9>< / td>

如何改进处理表单的方式,从数据库中获取数据,

$

 < input type =text

name =abcvalue =<?php array_key_exists('abc',$ _REQUEST)?$ _REQUEST ['abc']:默认值在这里? />

一个更优雅的解决方案,虽然工作量很大,但会涉及使用ajax )在实际提交之前对表单执行服务器端验证。


Ok, so this is a common scenario.

You have an html form that involves editing information. The original information comes from the database. When you post the form, it may not save the information immediately, because something may need fixing when the data-checking is done, maybe one of the required fields is left blank. As a result, you want to redisplay the form field, but if there was post data, display the post data, if not, display the original data from the database.

So I created a function to check post, then default to some arbitrary data (in this case from the database).

But overall, the approach feels inelegant, the POST data is being pulled invisibly inside the function from a global, but if I pass the post data in I have to pass it in for every function call, and it's almost as verbose as just doing it by hand each time, so specifically I'm looking for alternatives to this approach, and generally I'd love advice on better ways to deal with this form scenario that I deal with every single time I edit html forms.

// Pull from post or get, or else use data, e.g. from the database, to populate a form.
function in_or_data($index, $data, $trim=false){
    return $_POST[$index]? ($trim ? trim($_POST[$index]) : $_POST[$index]) : $data[$index];
}

<?php
$item_name = in_or_data('item_name', $data_from_database_somewhere); // Pull post data, with defaults coming from the 
?>

// ..... Later, some example html that just escapes & echoes out the data. .....
<td id='item-name'><input name="item_name" type="text" id="item_name" value="<?php echo escape($item_name); ?>" size="47" maxlength="100" tabindex="9"></td>

How can I improve dealing with forms that get their data either from the database initially, or from post after some kind of submission is being done?

解决方案

<input type="text" name="abc" value="<?php array_key_exists('abc', $_REQUEST) ? $_REQUEST['abc'] : "default value goes here" ?>" />

A more elegant solution, though a serious amount of work, would involve using ajax (jquery, etc.) to perform server-side validation on the form BEFORE actually submitting.

这篇关于简化使用dabase数据和错误检查预填充的PHP表单,因此主动使用$ _POST数据覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆