php Simulation asp. net webFrom Button Submit Event Instance

  • 2021-07-21 07:59:14
  • OfStack

Because the company needs php project development, php is just getting started. In the process of writing button submission, the button event in asp. net is better. Look at the following code first,


<?
require_once '../inc/EventHelper.php';

function Page_Load()
{
echo ' Will run at any time <br>';

if(!Page::IsPostBack())
{
echo ' Load product classification <br>';
if($_GET['cmd']=='edit') 
{
echo ' Modify and load the product information to be modified <br>';
}
}
}

function bAdd_Click()
{
//Comm::CheckQX(' Product management _ Add ');
echo "bAdd_Click<br>";
}

function bEdit_Click()
{
//Comm::CheckQX(' Product management _ Modify ');
echo 'proID='.$_GET['proID'].'<br>';
echo "bEdit_Click<br>";
}

function sdfsdfdsf_Click()
{
echo "e44444444444444444444<br>";
}

?>

<form name="aa" method="post" action="?<?=Comm::GetParam()?>">
<input type="submit" name="bAdd" value=" Add " />
<input type="submit" name="bedit" value=" Modify " />
<input type="submit" name="sdfsdfdsf" value="ewrewrewr" />
</form>

Those who have developed asp. net should be familiar with the above code, such as Page_Load, Page.IsPostback, bAdd_Click, which are very similar to asp. net events.

The above code runs the bAdd_Click function when the "Add" button is clicked (for the time being, function seems to mean a function). Similarly, clicking the "Modify" button automatically runs the bEdit_Click event. Do not need too many parameter changes, also do not need too many files, if the page function is not very complex, with this mode can be developed quickly.

Let's take a look at the code for the file EventHelper. php:


<?
class Page
{
// Whether to post back data, 1: Yes 
public static function IsPostBack()
{
global $SYSRunEventName;
return !empty($SYSRunEventName);
}

// Load and execute events 
function EventLoad()
{
global $SYSRunEventName;

$arrEvent=get_defined_functions();
$arrEventUser=$arrEvent['user'];

$arr=array_keys($_POST);
foreach($arr as $row)
{
$name=strtolower($row);
foreach($arrEventUser as $row1)
{
$name1=str_ireplace('_click','',$row1);
if($name==$name1)
{
$SYSRunEventName=$row1;
break;
}
}

if(!empty($SYSRunEventName))
{
break; 
}
}

if(function_exists('Page_Load')) 
Page_Load();

$SYSRunEventRunName=strtolower($SYSRunEventName);

if(Page::IsPostBack())
{
$SYSRunEventName();
}
}
}

class Comm
{
public static function GetParam($params=array(),$cmd='addoverride')
{
$allParam=array();

if($cmd=='addoverride')
{
$arrKeys=array_keys($params);
foreach($arrKeys as $row)
{
if(!in_array($row,array_keys($allParam))) 
$allParam[$row]=$params[$row];
}
}
else if($cmd=='del')
{
foreach($params as $row)
{
unset($_GET[$row]); 
}
}


$arrKeys=array_keys($_GET);
foreach($arrKeys as $row)
{
if(!in_array($row,array_keys($allParam)))
$allParam[$row]=$_GET[$row];
}

$p='';
$arrKeys=array_keys($allParam);
foreach($arrKeys as $row)
{
$p.=$row.'='.$allParam[$row].'&';
}
return rtrim($p,'&');
}
}

Page::EventLoad();
?>

The above functions can be tested, in my php5.4 can run successfully, but in terms of security has not been considered too much, read some articles php will be possible to execute php code through the client, because php has many practical features.

As for Comm:: GetParam, it is often necessary to obtain the parameters of get mode or modify the parameters, such as keeping all url parameters and modifying only the paging parameters (such as page=5), so I wrote some codes myself.

Mainly utilize the following characteristics of php:

function_exists
get_defined_functions

As well as using the commonly used form submission principle and submit submission principle to realize the function.

Due to the rush of time, it is too late to explain the specific principle, please forgive me, the code can be understood by everyone.


Related articles: