Tuesday, May 14, 2013

Simple Form Submission and store value in Database with Display, Delete..


Module -> phpfox_sample -> Template -> Default -> Controller
formsubmit.html.php

<form name="form2" method="post" action="{url link='product.formsubmit'}"
enctype="multipart/form-data">
Name : <input type="text" name="val[name]"> <br>
User Name : <input type="text" name="val[user]"><br>
Password : <input type="password" name="val[pass]"></br>
E-mail : <input type="text" name="val[email]"><br>
<input type="submit" name="val[submit]" > </br>
</form>
<table style="border: 1px solid #0088CC; width: 500px; ">
<tr style="border: 1px solid #0088CC;"">
<td style="border-right: 1px solid #0088CC;">Name</td>
<td style="border-right: 1px solid #0088CC;">User</td>
<td style="border-right: 1px solid #0088CC;">E-mail</td>
<td></td>
</tr>
{foreach from=$details name=detail item=detailValue}
<tr id="delete_{$detailValue.id}" style="border: 1px solid #0088CC;">
<td style="border-right: 1px solid #0088CC;">{$detailValue.name}</td>
<td style="border-right: 1px solid #0088CC;">{$detailValue.user}</td>
<td style="border-right: 1px solid #0088CC;">{$detailValue.email}</td>
<td> <a href="#" onclick = "$.ajaxCall('product.delete', 'id={$detailValue.id}');"
> DELETE </a> </td>
</tr>
{/foreach}
</table>

module -> phpfox_sample -> Include -> Component -> Controller
formsubmit.class.php

class Phpfoxsample_Component_Controller_Forminsert extends Phpfox_Component
{
/**

* @author Arun George <arun@arun-g.in>
* @since 19-11-2012

*/
public function process()
{
//$this->template()->assign(array('members'=>$aMembers,'id'=>$id,'aPages'=>
$aPage,'currentUser'=>$currentUser));
if($aVals = $this->request()->get('val')){
unset($aVals['submit']);
//d($aVals); exit();
//Phpfox::getService('forminsertservice')->test($aVals);
Phpfox::getService('phpfoxsample.forminsertservice')->insert($aVals);
}
$details = Phpfox::getService('phpfoxsample.forminsertservice')->get();
$this->template()->setTitle('My First Form Posting');
if($details)
$this->template()->assign(array('details'=>$details));
}
}

Module -> phpfox_sample -> Include -> Service
formsubmit.class.php

class Product_Service_Formsubmit extends Phpfox_Service
{
/**
*function to insert value to database
* @param type $argument
* @return type id
* @author Arun George <arun@arun-g.in>
* @since 19-11-2012
*
*/
function insert($argument)
{
//d($argument);
//exit();
$id = $this->database()->insert(Phpfox::getT('formsubmit'), $argument);
return $id ;
}
/**
*Function to get rows from table
* @return type
* @author Arun George <arun@arun-g.in>
* @since 19-11-2012
*/
function get()
{
$row = $this->database()->select('*')
->from(Phpfox::getT('formsubmit'))
->execute('getSlaveRows');
// d($row);
return $row;
}
/**
* function to delete the entry from database
* @param type $id
* @author Arun George <arun@arun-g.in>
* @since 19-11-2012
*
*/
function delete($id)
{
$this->database()->delete(Phpfox::getT('formsubmit'), ' id = ' . (int) $id);
return true;
}
}

module -> phpfox_sample -> Include -> Component -> Ajax
ajax.class.php

/**
* [PHPFOX_HEADER]
*/
defined('PHPFOX') or exit('NO DICE!');
class Product_Component_Ajax_Ajax extends Phpfox_Ajax
{
/**
* Function to delete value using Ajax
* @author Arun George <arun@arun-g.in>
* @since 19-11-2012
*/
function delete()
{
$id = $this->get('id');
$return = Phpfox::getService('product.formsubmit')->delete($id);
$this->hide("#delete_".$id);
$this->alert('deleted');
}
}

No comments:

Post a Comment