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 Live24u.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!.

You can download source code and Demo from below link.




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 = "<input type=\"hidden\" name=\"contrl_name\" value=\"$data_value\">";
	$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 = "<button class=\""
	    .($atype ? 'ajaxsubmit' : 'inputsubmit')
		."\" type=\"submit\""
		.$aspect
	    ." name=\"contrl_name\"  id=\"contrl_name\" value=\"$data_value\""
	    .($data_title ? " data_title='$data_title'" : '')
	    .">"
		.($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon' height='12' alt=''>" : '')
		."<span>$data_value</span>"
		."</button>\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 "<center>";
	submit(contrl_name, $data_value, $echo, $data_title, $data_async, $icon);
	if ($echo) echo "</center>";
}

center_submit_first

function center_submit_first(contrl_name, $data_value, $data_title=false, $data_async=false, $icon=false)
{
	echo "<center>";
	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 "</center>";
}




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 "<center>";
	submit_add_and_update($add, $data_title, $data_async, $clone);
	echo "</center>";
}




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 "<tr>";
	if ($right)
		echo "<td> </td>\n";
	echo "<td $extra>";
	submit_add_and_update($add, $data_title, $data_async, $clone);
	echo "</td></tr>\n";
}

submit_cells

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

submit_row

function submit_row(contrl_name, $data_value, $right=true, $extra="", $data_title=false, $data_async=false)
{
	echo "<tr>";
	if ($right)
		echo "<td> </td>\n";
	submit_cells(contrl_name, $data_value, $extra, $data_title, $data_async);
	echo "</tr>\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 "<button type='submit' class='editbutton' name='"
			.htmlentities(strtr(contrl_name, array('.'=>'=2E', '='=>'=3D',// ' '=>'=20','['=>'=5B'
			)))
			."' value='1'" . ($data_title ? " data_title='$data_title'":" data_title='$data_value'")
			. ($aspect ? " aspect='$aspect'" : '')
			. $rel
			." >".set_icon($icon)."</button>\n";
	}
	else
		return "<input type='submit' class='editbutton' name='"
			.htmlentities(strtr(contrl_name, array('.'=>'=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 "<td align='center'>";
	echo button(contrl_name, $data_value, $data_title, $icon, $aspect);
	echo "</td>";
}

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 .= "<input"
	    .($data_value == 1 ? ' checked':'')
	    ." type='checkbox' name='contrl_name' value='1'"
	    .($change_submit_click ? " onclick='$change_submit_click'" : '')
	    .($data_title ? " data_title='$data_title'" : '')
	    ." >\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 "<td>$data_label</td>\n";
	echo "<td $argument>";
	echo check(null, contrl_name, $data_value, $change_submit_click, $data_title);
	echo "</td>";
}




check_row

function check_row($data_label, contrl_name, $data_value=null, $change_submit_click=false, $data_title=false)
{
	echo "<tr><td class='label'>$data_label</td>";
	echo check_cells(NULL, contrl_name, $data_value, $change_submit_click, $data_title);
	echo "</tr>\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 "<input type='radio' name=contrl_name value='$data_value' ".($selected ? "checked":'')
	    .($change_submit_click ? " onclick='$change_submit_click'" : '')
		.">".($data_label ? $data_label : '');
}

labelheader_cell

function labelheader_cell($data_label, $argument="")
{
	echo "<td class='tableheader' $argument>$data_label</td>\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 "<td $argument>$data_label</td>\n";

	return $data_label;
}

email_cell

function email_cell($data_label, $argument="", $id=null)
{
	cell_label("<a href='mailto:$data_label'>$data_label</a>", $argument, $id);
}

percent_cell

function percent_cell($data_label, $bold=false, $id=null)
{
	if ($bold)
		cell_label("<b>".percent_format($data_label)."</b>", "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 "<td $argument>$data_label</td>\n";
	cell_label($data_value, $argument2, $id);
}

label_row

function label_row($data_label, $data_value, $argument="", $argument2="", $leftfill=0, $id=null)
{
	echo "<tr>";
	if ($argument == "")
	{
		echo "<td class='label'>$data_label</td>";
		$data_label = null;
	}	
	cell_labels($data_label, $data_value, $argument, $argument2, $id);
	if ($leftfill!=0)
	  	echo "<td colspan=$leftfill></td>";
	echo "</tr>\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 "<input $argument type=\"text\" name=\"contrl_name\" size=\"$data_size\" max_lengthlength=\"$max_length\" value=\"$data_value\""
	    .($data_title ? " data_title='$data_title'" : '')
	    .">";
}

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 "<td>";

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

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

	echo "</td>\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 "<td>";
	$class = $change_submit_click ? 'class="searchbox"' : '';
	echo "<input $class type=\"text\" name=\"contrl_name\" size=\"$data_size\" max_lengthlength=\"$max_length\" value=\"" . $_POST[contrl_name]. "\""
	 .($data_title ? " data_title='$data_title'": '')." >";

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

	echo "</td>\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 "<tr><td class='label'>$data_label</td>";
	text_cells(null, contrl_name, $data_value, $data_size, $max_length, $data_title, $argument, $post_label);

	echo "</tr>\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 "<tr><td class='label'>$data_label</td>";
	text_cells_ex(null, contrl_name, $data_size, $max_length, $data_value, $data_title, $argument, $post_label);

	echo "</tr>\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 = "<a href='Mailto:".$_POST[contrl_name]."'>$data_label</a>";
	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 = "<a href='Mailto:".$_POST[contrl_name]."'>$data_label</a>";
	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 = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('contrl_name')[0]);\">"
		. "	<img src='$calc_image' style='vertical-align:middle;padding-bottom:4px;width:16px;height:16px;border:0;' alt='"._('Click Here to Pick up the date')."'></a>\n";
	}	
	else
		$post_label = "";

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

	echo "<td>";
	
	$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 "<input type=\"text\" name=\"contrl_name\" class=\"$class\" $aspect size=\"$data_size\" max_lengthlength=\"12\" value=\"" 
	 . $_POST[contrl_name]. "\""
	 .($data_title ? " data_title='$data_title'": '')." > $post_label";
	echo "</td>\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 "<tr><td class='label'>$data_label</td>";
	date_cells(null, contrl_name, $data_title, $check, $inc_days, $inc_months, 
		$inc_years, $argument, $change_submit_click);
	echo "</tr>\n";
}

password_row

function password_row($data_label, contrl_name, $data_value)
{
	echo "<tr><td class='label'>$data_label</td>";
	cell_label("<input type='password' name='contrl_name' size=20 max_lengthlength=20 value='$data_value' >");
	echo "</tr>\n";
}	

file_cells

function file_cells($data_label, contrl_name, $id="")
{
	if ($id != "")
		$id = "id='$id'";
	cell_labels($data_label, "<input type='file' name='contrl_name' $id >");
}	

file_row

function file_row($data_label, contrl_name, $id = "")
{
	echo "<tr><td class='label'>$data_label</td>";
	file_cells(null, contrl_name, $id);
	echo "</tr>\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 "<td $argument>$data_label</td>\n";
	if ($data_value == null)
		$data_value = (!isset($_POST[contrl_name]) ? "" : $_POST[contrl_name]);
	echo "<td><textarea name='contrl_name' cols='$cols' rows='$rows'"
	.($data_title ? " data_title='$data_title'" : '')
	.">$data_value</textarea></td>\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 "<tr><td class='label'>$data_label</td>";
	textarea_cells(null, contrl_name, $data_value, $cols, $rows, $data_title, $argument);
	echo "</tr>\n";
}

ahref

function ahref($data_label, $href, $data_target="", $onclick="") {
  echo "<a href='$href' target='$data_target' onclick='$onclick'>$data_label</a>";
}

ahref_cell

function ahref_cell($data_label, $href, $data_target="", $onclick="") {
  echo "<td align='center'>  ";
  ahref($data_label, $href, $data_target, $onclick);
  echo "  </td>";
}

You can download source code and Demo from below link.




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(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.

http://live24u.com/wp-content/uploads/2017/06/dynamically-add-input-fields-and-submit-to-database-with-jquery-and-php.pnghttp://live24u.com/wp-content/uploads/2017/06/dynamically-add-input-fields-and-submit-to-database-with-jquery-and-php-150x150.pngadminAjaxAPICSSHtmlJavaScriptjQueryJSONPHPadd field dynamically to form in jquery and php,dynamic form generation in php,dynamically add / remove input fields in php with jquery ajax,dynamically add input fields and submit to database with javascript and php,dynamically add input fields and submit to database with jquery and codeigniter,dynamically add input fields and submit to database with jquery and php,dynamically add input fields and submit to database with php,how to add textbox dynamically in phpDynamically 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 Live24u.com - Examples, The best For Learn web development Tutorials,Demo with Example! Hi...BIGGEST AND FASTEST Web Tutorials - live24u.com