Dynamically Add Input Fields And Submit To Database With jQuery and PHP

Dynamically Add Input Fields And Submit To Database With jQuery and PHP

In this Post We Will Explain About is Dynamically Add Input Fields And Submit To Database With jQuery and PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to dynamically add input fields and submit to database with jquery and codeigniterExample

In this post we will show you Best way to implement dynamically add input fields and submit to database with javascript and php, hear for add field dynamically to form in jquery and phpwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

input_changed

function input_changed(contrl_name)
{
	return isset($_POST['_'.contrl_name.'_changed']);
}

input_num

function input_num($postname=null, $dflt=0)
{
	if (!isset($_POST[$postname]) || $_POST[$postname] == "")
	  	return $dflt;

    return user_numeric($_POST[$postname]);
}

hidden fields

$hidden_fields = array();
function hidden(contrl_name, $data_value=null, $echo=true)
{
  	global $jqAjax, $hidden_fields;
	
	if ($data_value === null) 
		$data_value = get_post(contrl_name);
	
  	$ret = "";
	$jqAjax->addUpdate(contrl_name, contrl_name, $data_value);
	if ($echo)
			$hidden_fields[] = $ret;
	else
		return $ret;
}

submit button

function submit(contrl_name, $data_value, $echo=true, $data_title=false, $atype=false, $icon=false)
{
	global $path_to_root;

	$aspect='';
	if ($atype === null) {
		$aspect = fallback_mode() ? " aspect='fallback'" : " style='display:none;'";

	} elseif (!is_bool($atype)) { // necessary: switch uses '=='

		$aspect = " aspect='$atype' ";
		$types = explode(' ', $atype);

		foreach ($types as $type) {
			switch($type) {
				case 'selector':
					$aspect = " aspect='selector' rel = '$data_value'"; 
					$data_value = _("Select");
					if ($icon===false) $icon=ICON_SUBMIT; break;

				case 'default':
					if ($icon===false) $icon=ICON_SUBMIT; break;

				case 'cancel':
					if ($icon===false) $icon=ICON_ESCAPE; break;

				case 'nonajax':
					$atype = false;
			}
		}
	}
	$submit_str = "\n";
	if ($echo)
		echo $submit_str;
	else
		return $submit_str;
}

center_submit

function center_submit(contrl_name, $data_value, $echo=true, $data_title=false, $data_async=false, $icon=false)
{
	if ($echo) echo "
"; submit(contrl_name, $data_value, $echo, $data_title, $data_async, $icon); if ($echo) echo "
"; }

center_submit_first

function center_submit_first(contrl_name, $data_value, $data_title=false, $data_async=false, $icon=false)
{
	echo "
"; submit(contrl_name, $data_value, true, $data_title, $data_async, $icon); echo " "; }

last_submit_center

function last_submit_center(contrl_name, $data_value, $data_title=false, $data_async=false, $icon=false)
{
	echo " ";
	submit(contrl_name, $data_value, true, $data_title, $data_async, $icon);
	echo "";
}

submit_add_and_update

function submit_add_and_update($add=true, $data_title=false, $data_async=false, $clone=false)
{
	$cancel = $data_async;

	if ($data_async === 'both') {
		$data_async = 'default'; $cancel = 'cancel';
	}
	elseif ($data_async === 'upgrade') {
		$data_async = 'default nonajax process'; $cancel = 'cancel';
	}
	elseif ($data_async === 'default')
		$cancel = true;
	elseif ($data_async === 'cancel')
		$data_async = true;
	
	if ($add)
		submit('ADD_ITEM', _("Add new"), true, $data_title, $data_async);
	else {
		submit('UPDATE_ITEM', _("Update"), true, _('Submit changes'), $data_async);
		if ($clone) submit('CLONE', _("Clone"), true, 
			_('Edit new record with current data'), $data_async);
		submit('RESET', _("Cancel"), true, _('Cancel edition'), $cancel);
	}
}

submit_add_and_update_center

function submit_add_and_update_center($add=true, $data_title=false, $data_async=false, $clone=false)
{
	echo "
"; submit_add_and_update($add, $data_title, $data_async, $clone); echo "
"; }

submit_add_or_update_row

function submit_add_or_update_row($add=true, $right=true, $extra="", $data_title=false, $data_async=false, $clone = false)
{
	echo "";
	if ($right)
		echo " \n";
	echo "";
	submit_add_and_update($add, $data_title, $data_async, $clone);
	echo "\n";
}

submit_cells

function submit_cells(contrl_name, $data_value, $extra="", $data_title=false, $data_async=false)
{
	echo "";
	submit(contrl_name, $data_value, true, $data_title, $data_async);
	echo "\n";
}

submit_row

function submit_row(contrl_name, $data_value, $right=true, $extra="", $data_title=false, $data_async=false)
{
	echo "";
	if ($right)
		echo " \n";
	submit_cells(contrl_name, $data_value, $extra, $data_title, $data_async);
	echo "\n";
}

submit_return

function submit_return(contrl_name, $data_value, $data_title=false)
{
	if (@$_REQUEST['popup']) {
		submit(contrl_name, $data_value, true, $data_title, 'selector');
	}
}

submit_js_confirm

function submit_js_confirm(contrl_name, $msg, $set = true) {
	global $jqAjax;
	$js = "_validate.contrl_name=".($set ? "function(){ return confirm('"
				. strtr($msg, array("\n"=>'\\n')) . "');};"
				: 'null;');
	if (in_ajax()) {
		$jqAjax->addScript(true, $js);
	} else
		add_js_source($js);
}

button

function button(contrl_name, $data_value, $data_title=false, $icon=false,  $aspect='')
{
	$rel = '';
	if ($aspect == 'selector') {
		$rel = " rel='$data_value'";
		$data_value = _("Select");
	}
	if (user_graphic_links() && $icon)
	{
		if ($data_value == _("Delete")) // Helper during implementation
			$icon = ICON_DELETE;
		return "\n";
	}
	else
		return "'=2E', '='=>'=3D',// ' '=>'=20','['=>'=5B'
			)))
			."' value='$data_value'"
			.($data_title ? " data_title='$data_title'":'')
			. ($aspect ? " aspect='$aspect'" : '')
			. $rel
			." >\n";
}

cell_button

function cell_button(contrl_name, $data_value, $data_title=false, $icon=false, $aspect='')
{
	echo "";
	echo button(contrl_name, $data_value, $data_title, $icon, $aspect);
	echo "";
}

delete_cell_button

function delete_cell_button(contrl_name, $data_value, $data_title=false)
{
	cell_button(contrl_name, $data_value, $data_title, ICON_DELETE);
}

edit_cell_button

function edit_cell_button(contrl_name, $data_value, $data_title=false)
{
	cell_button(contrl_name, $data_value, $data_title, ICON_EDIT);
}

select_cell_button

function select_cell_button(contrl_name, $data_value, $data_title=false)
{
	cell_button(contrl_name, $data_value, $data_title, ICON_ADD, 'selector');
}

check_value

function check_value(contrl_name)
{
    if (!isset($_POST[contrl_name]) || ($_POST[contrl_name]+0) === 0)
		return 0;
	return 1;
}

checkbox

function checkbox($data_label, contrl_name, $data_value=null, $change_submit_click=false, $data_title=false)
{
  	global $jqAjax;

	$str = '';	

	if ($data_label)
		$str .= $data_label . "  ";
	if ($change_submit_click !== false) {
		if ($change_submit_click === true)
			$change_submit_click = 
				"JsHttpRequest.request(\"_{contrl_name}_update\", this.form);";
	}
	if ($data_value === null)
		$data_value = get_post(contrl_name,0);

	$str .= "\n";

	$jqAjax->addUpdate(contrl_name, contrl_name, $data_value);
	return $str;
}

check

function check($data_label, contrl_name, $data_value=null, $change_submit_click=false, $data_title=false)
{
	echo checkbox($data_label, contrl_name, $data_value, $change_submit_click, $data_title);
}

check_cells

function check_cells($data_label, contrl_name, $data_value=null, $change_submit_click=false, $data_title=false,
	$argument='')
{
	if ($data_label != null)
		echo "$data_label\n";
	echo "";
	echo check(null, contrl_name, $data_value, $change_submit_click, $data_title);
	echo "";
}

check_row

function check_row($data_label, contrl_name, $data_value=null, $change_submit_click=false, $data_title=false)
{
	echo "$data_label";
	echo check_cells(NULL, contrl_name, $data_value, $change_submit_click, $data_title);
	echo "\n";
}

radio

function radio($data_label, contrl_name, $data_value, $selected=null, $change_submit_click=false)
{
	if (!isset($selected))
		$selected = get_post(contrl_name) === (string)$data_value;

	if ($change_submit_click === true)
		$change_submit_click = 
			"JsHttpRequest.request(\"_{contrl_name}_update\", this.form);";

	return "".($data_label ? $data_label : '');
}

labelheader_cell

function labelheader_cell($data_label, $argument="")
{
	echo "$data_label\n";
}

cell_label

function cell_label($data_label, $argument="", $id=null)
{
    global $jqAjax;

	if(isset($id))
	{
	    $argument .= " id='$id'";
	    $jqAjax->addUpdate($id, $id, $data_label);
	}
	echo "$data_label\n";

	return $data_label;
}

email_cell

function email_cell($data_label, $argument="", $id=null)
{
	cell_label("$data_label", $argument, $id);
}

percent_cell

function percent_cell($data_label, $bold=false, $id=null)
{
	if ($bold)
		cell_label("".percent_format($data_label)."", "nowrap align=right", $id);
	else
		cell_label(percent_format($data_label), "nowrap align=right", $id);
}

cell_labels

function cell_labels($data_label, $data_value, $argument="", $argument2="", $id=null)
{
	if ($data_label != null)
		echo "$data_label\n";
	cell_label($data_value, $argument2, $id);
}

label_row

function label_row($data_label, $data_value, $argument="", $argument2="", $leftfill=0, $id=null)
{
	echo "";
	if ($argument == "")
	{
		echo "$data_label";
		$data_label = null;
	}	
	cell_labels($data_label, $data_value, $argument, $argument2, $id);
	if ($leftfill!=0)
	  	echo "";
	echo "\n";
}

text_input

function text_input(contrl_name, $data_value=null, $data_size='', $max_length='', $data_title='', $argument='')
{
	if ($data_value === null)
		$data_value = get_post(contrl_name);

	return "";
}

text_cells

function text_cells($data_label, contrl_name, $data_value=null, $data_size="", $max_length="", $data_title=false, 
	$labargument="", $post_label="", $inargument="")
{
  	global $jqAjax;

	foucus_control(contrl_name);
	if ($data_label != null)
		cell_label($data_label, $labargument);
	echo "";

	echo text_input(contrl_name, $data_value, $data_size, $max_length, $data_title, $inargument);

	if ($post_label != "")
		echo " " . $post_label;

	echo "\n";
	$jqAjax->addUpdate(contrl_name, contrl_name, $data_value);
}

text_cells_ex

function text_cells_ex($data_label, contrl_name, $data_size, $max_length=null, $init=null, $data_title=null,
	$labargument=null, $post_label=null, $change_submit_click=false)
{
  	global $jqAjax;

	foucus_control(contrl_name);
	if (!isset($_POST[contrl_name]) || $_POST[contrl_name] == "")
	{
		if ($init)
			$_POST[contrl_name] = $init;
		else
			$_POST[contrl_name] = "";
	}
	if ($data_label != null)
		cell_label($data_label, $labargument);

	if (!isset($max_length))
		$max_length = $data_size;

	echo "";
	$class = $change_submit_click ? 'class="searchbox"' : '';
	echo "";

	if ($post_label)
		echo " " . $post_label;

	echo "\n";
	$jqAjax->addUpdate(contrl_name, contrl_name, $_POST[contrl_name]);
}

text_row

function text_row($data_label, contrl_name, $data_value, $data_size, $max_length, $data_title=null, $argument="", $post_label="")
{
	echo "$data_label";
	text_cells(null, contrl_name, $data_value, $data_size, $max_length, $data_title, $argument, $post_label);

	echo "\n";
}

text_row_ex

function text_row_ex($data_label, contrl_name, $data_size, $max_length=null, $data_title=null, $data_value=null, $argument=null, $post_label=null)
{
	echo "$data_label";
	text_cells_ex(null, contrl_name, $data_size, $max_length, $data_value, $data_title, $argument, $post_label);

	echo "\n";
}

email_row

function email_row($data_label, contrl_name, $data_value, $data_size, $max_length, $data_title=null, $argument="", $post_label="")
{
	if (get_post(contrl_name)) 
		$data_label = "$data_label";
	text_row($data_label, contrl_name, $data_value, $data_size, $max_length, $data_title, $argument, $post_label);
}

email_row_ex

function email_row_ex($data_label, contrl_name, $data_size, $max_length=null, $data_title=null, $data_value=null, $argument=null, $post_label=null)
{
	if (get_post(contrl_name)) 
		$data_label = "$data_label";
	text_row_ex($data_label, contrl_name, $data_size, $max_length, $data_title, $data_value, $argument, $post_label);
}

date_cells

function date_cells($data_label, contrl_name, $data_title = null, $check=null, $inc_days=0, 
	$inc_months=0, $inc_years=0, $argument=null, $change_submit_click=false)
{
	global $path_to_root, $jqAjax;

	if (!isset($_POST[contrl_name]) || $_POST[contrl_name] == "")
	{
		if ($inc_years == 1001)
			$_POST[contrl_name] = null;
		else
		{
			$dd = Today();
			if ($inc_days != 0)
				$dd = add_days($dd, $inc_days);
			if ($inc_months != 0)
				$dd = add_months($dd, $inc_months);
			if ($inc_years != 0)
				$dd = add_years($dd, $inc_years);
			$_POST[contrl_name] = $dd;
		}
	}
	if (user_use_date_picker())
	{
		$calc_image = (file_exists("$path_to_root/themes/".user_theme()."/images/cal.gif")) ? 
			"$path_to_root/themes/".user_theme()."/images/cal.gif" : "$path_to_root/themes/default/images/cal.gif";
		$post_label = ""
		. "	"._(\n";
	}	
	else
		$post_label = "";

	if ($data_label != null)
		cell_label($data_label, $argument);

	echo "";
	
	$class = $change_submit_click ? 'date active' : 'date';

	$aspect = $check ? 'aspect="cdate"' : '';
	if ($check && (get_post(contrl_name) != Today()))
		$aspect .= ' style="color:#FF0000"';

	foucus_control(contrl_name);
	$data_size = (user_date_format()>3)?11:10; 
	echo " $post_label";
	echo "\n";
	$jqAjax->addUpdate(contrl_name, contrl_name, $_POST[contrl_name]);
}

date_row

function date_row($data_label, contrl_name, $data_title=null, $check=null, $inc_days=0, $inc_months=0, 
	$inc_years=0, $argument=null, $change_submit_click=false)
{
	echo "$data_label";
	date_cells(null, contrl_name, $data_title, $check, $inc_days, $inc_months, 
		$inc_years, $argument, $change_submit_click);
	echo "\n";
}

password_row

function password_row($data_label, contrl_name, $data_value)
{
	echo "$data_label";
	cell_label("");
	echo "\n";
}	

file_cells

function file_cells($data_label, contrl_name, $id="")
{
	if ($id != "")
		$id = "id='$id'";
	cell_labels($data_label, "");
}	

file_row

function file_row($data_label, contrl_name, $id = "")
{
	echo "$data_label";
	file_cells(null, contrl_name, $id);
	echo "\n";
}	

percent_row

function percent_row($data_label, contrl_name, $init=null)
{

	if (!isset($_POST[contrl_name]) || $_POST[contrl_name]=="")
	{
		$_POST[contrl_name] = $init == null ? '' : $init;
	}

	small_amount_row($data_label, contrl_name, $_POST[contrl_name], null, "%", user_percent_dec());
}

textarea_cells

function textarea_cells($data_label, contrl_name, $data_value, $cols, $rows, $data_title = null, $argument="")
{
  	global $jqAjax;

	foucus_control(contrl_name);
	if ($data_label != null)
		echo "$data_label\n";
	if ($data_value == null)
		$data_value = (!isset($_POST[contrl_name]) ? "" : $_POST[contrl_name]);
	echo "\n";
	$jqAjax->addUpdate(contrl_name, contrl_name, $data_value);
}

data_textarea_row

function data_textarea_row($data_label, contrl_name, $data_value, $cols, $rows, $data_title=null, $argument="")
{
	echo "$data_label";
	textarea_cells(null, contrl_name, $data_value, $cols, $rows, $data_title, $argument);
	echo "\n";
}

ahref

function ahref($data_label, $href, $data_target="", $onclick="") {
  echo "$data_label";
}

ahref_cell

function ahref_cell($data_label, $href, $data_target="", $onclick="") {
  echo "  ";
  ahref($data_label, $href, $data_target, $onclick);
  echo "  ";
}

Example

I hope you have Got What is how to add textbox dynamically in php And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment