Tag Archives: form

auto fill form field

name: <input id="de-name" type="text">
recomendation:	<input id="de-name" type="text">

/* auto field form field*/
jQuery(document).ready(function(){
	jQuery( "#de-name" ).keyup(function() {
		jQuery('#de-rec').val(jQuery('#de-name').val());
	});
});

Avoid form auto submit on page refreshing


<form>

......

</form>

if(isset($_POST['submit'])) {

......

?><script>
window.location = document.URL;    // 'document.url' is get current url and 'windo.location' isredirect on it.
</script><?php

}

All types of form validation



<form name="f1" onsubmit="return vali()">
  name
  <input type="text" name="fname" id="fname">
  <br>
  Email
  <input type="text" name="email" id="email">
  <br>
  Number
  <input type="text" name="number" id="number">
  <br>
  Strong pass
  <input type="text" name="strongpass" id="strongpass">
  <br>
  password
  <input type="text" name="pass" id="pass">
  <br>
  conform pass
  <input type="text" name="cpass" id="cpass">
  <br>
  city
  <select name="city" id="city">
    <option></option>
    <option value="junagadh">junagaadh</option>
  </select>
  <br>
  Gender
  <input type="radio" name="gender" id="gender" />
  Male
  <input type="radio" name="gender" id="gender" />
  Female<br>
  Post
  <input type="checkbox" name="developer" id="developer" />
  Developer
  <input type="checkbox" name="designer" id="designer" />
  Designer<br>
  <input type="submit" name="submit">
  <script language="javascript">
	
	function vali()
	{
		var x = document.forms["f1"]["fname"].value;
		if(x == null || x == "")
		{
			alert("name");
			fname.focus();
			return false;
		}
		
		var x = document.forms["f1"]["email"].value;
		if(!x.match(/^[a-zA-Z0-9._]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/))
		{
			alert("email");
			email.focus();
			return false;
		}
		var x = document.forms["f1"]["number"].value;
		if(!x.match(/^[0-9]{10}$/))
		{
			alert("number");
			number.focus();
			return false;
		}
		var x = document.forms["f1"]["strongpass"].value;
		if(!x.match(/^[0-9A-Za-z._-]{6,10}$/))
		{
			alert("pass musst be 10 digit");
			strongpass.focus();
			return false;
		}
		var x = document.forms["f1"]["pass"].value;
		if(x == null || x == "")
		{
			alert("password");
			pass.focus();
			return false;
		}
		var x = document.forms["f1"]["pass"].value;
		var y = document.forms["f1"]["cpass"].value;
		if(x != y) // (!(x==y)) also use this
		{
			alert("pass not match");
			cpass.focus();
			return false;
		}
		var x = document.forms["f1"]["city"].value;
		if(x == null || x == "")
		{
			alert("city");
			city.focus();
			return false;
		}
		if(document.f1.gender[0].checked == false && gender[1].checked == false)
		{
			alert("gensder");
			gender[0].focus();
			return false
		}
		if(document.f1.developer.checked == false && designer.checked == false)
		{
			alert("job");
			developer.focus();
			return false;
		}
		
		alert("Thank you for submitting data");
		window.location.href="validation.php";
		
		return true;
	}
	
</script>
</form>



Search filter by form

Demo
implimented in “http://estategrid.com/find-agent/
This is form on submit button

<form method=”post” action=””  name=”agent” enctype=”multipart/form-data”>
<?php
/* sidebar for agent form*/

//list each role and each user with that role
global $wp_roles;
?><select name=”myagent”><option value=””>Select Agent</option><?php
foreach( $wp_roles->role_names as $role => $name ) {
$name = translate_with_context($name);
if($name == ‘Agent’){
$this_role = “‘[[:<:]]”.$role.”[[:>:]]'”;
$query = “SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = ‘wp_capabilities’ AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000″;
$users_of_this_role = $wpdb->get_results($query);
if ($users_of_this_role) {
foreach($users_of_this_role as $user) {
$curuser = get_userdata($user->ID);
$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
$agent = $curuser->user_nicename ;
echo ‘<option value=”‘.$agent.'”>’.ucfirst($agent).'</option>’;
}
}
}
}
?>
</select>
<?php
global $wpdb;

$argsa =array( ‘role’ => ‘aamrole_53673234ba574 ‘ );
$a = get_users( $argsa );
$argsb =array( ‘role’ => ‘ aamrole_5329589cf0296 ‘ );
$b = get_users( $argsb );
$agentusers = array_merge( $a, $b );
#print_r($agentusers);

foreach($agentusers as $agentuser)
{
$user_id = $agentuser->ID;
$agentlocation = get_user_meta($user_id,’location’,true);
//$agentlocation = $wpdb->get_var(“SELECT meta_value FROM “.$wpdb->usermeta.” WHERE meta_key = ‘location’ and user_id=$user_id”);

if(!empty($agentlocation))
$location_array[]=strtolower($agentlocation);
}
$mylocation_array= array_unique($location_array);
if(!empty($mylocation_array)){
?><select name=”mylocation”>
<option value=””>Select Location</option>
<?php
foreach($mylocation_array as $mylocation=>$value)    {
echo ‘<option value=”‘.$value.'”>’.ucfirst($value).'</option>’;
}?>
</select><?php
}
?>
<input type=”submit” value=”Refine Results” name=”agent”>
<?php
/* end sidebar for widget form*/
?>
</form>

After submit button

<?php /* after submit form */
if (isset($_POST[‘agent’]))
{
$args = “”;
$myagent = $_POST[‘myagent’];
$mylocation = $_POST[‘mylocation’];
if(!empty($myagent) && !empty($mylocation))
{
$args = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’     => ‘nickname’,
‘value’   => $myagent,
‘compare’ => ‘LIKE’
),
array(
‘key’     => ‘location’,
‘value’   => $mylocation,
‘compare’ => ‘LIKE’
),
),

);

}else if(!empty($myagent) && empty($mylocation))
{
$args = array(
‘meta_query’ => array(

array(
‘key’     => ‘nickname’,
‘value’   => $myagent,
‘compare’ => ‘LIKE’
),

),

);
}
else if(empty($myagent) && !empty($mylocation))
{
$args = array(
‘meta_query’ => array(

array(
‘key’     => ‘location’,
‘value’   => $mylocation,
‘compare’ => ‘LIKE’
),

),

);
}
if(!empty($args))
{
$users = new WP_User_Query( $args);
$users = $users->get_results();

foreach($users as $user)
{
?>
<div class=”agent-detail”>
<div class=”box”>
<div class=””><?php echo  get_avatar( $user->user_email); ?></div>
<h6 class=”service-title”>Name: <?php echo $user->nickname; ?></h6>
<h6 class=”service-title”>Location: <?php echo $user->location; ?></h6>
<h6 class=”service-title”>Email: <?php echo $user->user_email; ?></h6>
<h6 class=”service-title”>Mobile Phone: <?php echo $user->phone_mobile; ?></h6>
<h6 class=”service-title” style=”margin-bottom: 7px;”>About this Agent:</h6>
<p><?php echo $user->description; ?></p>
</div>
</div>
<?php
}
}
}
else
{
global $wpdb;
$argsa =array( ‘role’ => ‘aamrole_53673234ba574 ‘ );
$a = get_users( $argsa );
$argsb =array( ‘role’ => ‘ aamrole_5329589cf0296 ‘ );
$b = get_users( $argsb );
$agentusers = array_merge( $a, $b );
#print_r($agentusers);
foreach($agentusers as $agentuser)
{
?>
<div class=”agent-detail”>
<div class=”box”>
<div class=”box-img” style=”width:20%; float:left;”>
<?php echo  get_avatar( $agentuser->user_email); ?>
</div>
<div class=”box-data”>
<table border=1>
<tr><td>Name:</td><td><?php echo $agentuser->nickname; ?></td></tr>
<tr><td>Location:</td><td><?php echo $agentuser->location; ?></td></tr>
<tr><td>Email:</td><td><?php echo $agentuser->user_email; ?></td></tr>
<tr><td>Mobile Phone:</td><td><?php echo $agentuser->phone_mobile; ?></td></tr>
</table>
</div>
<div class=”box-desc”>
<br>About this Agent:
<p><?php echo $agentuser->description; ?></p>
</div>
</div>
</div>
<?php
}
} ?>

<?php
/* end submit form*/?>

Add custom form fields in user profile

<?php
function hack_add_custom_user_profile_fields(){
    global $pagenow;

    # do this only in page user-new.php
    if($pagenow !== 'user-new.php')
        return;

    # do this only if you can
    if(!current_user_can('manage_options'))
        return false;

?>
<table id="table_my_custom_field" style="display:none;">
<!-- My Custom Code { -->
    <tr>
        <th><label for="my_custom_field">My Custom Field</label></th>
        <td><input type="text" name="my_custom_field" id="my_custom_field" /></td>
    </tr>
<!-- } -->
</table>
<script>
jQuery(function($){
    //Move my HTML code below user's role
    $('#table_my_custom_field tr').insertAfter($('#role').parentsUntil('tr').parent());
});
</script>
<?php
}
add_action('admin_footer_text', 'hack_add_custom_user_profile_fields');


function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    update_usermeta($user_id, 'my_custom_field', $_POST['my_custom_field']);
}
add_action('user_register', 'save_custom_user_profile_fields');

Insert data from form

<?php
if(isset($_POST[‘submit’])){
$wpdb->insert(
‘wp_form’,
array(
‘fname’ => $_POST[‘fname’],
’email’ => $_POST[’email’],
‘message’ => $_POST[‘message’]
),
array(
‘%s’,
‘%s’,
‘%s’,
));
}
?>
<form id=”appointment” name=”appointment” method=”post”>
<fieldset>
<div class=”form-group”>
<input type=”text” placeholder=”Name” name=”fname”  />
</div>
<div class=”form-group”>
<input type=”text” placeholder=”E-mail” name=”email” />
</div>
<div class=”form-group”>
<input type=”text” placeholder=”message” name=”message”  />
</div>
<input type=”submit” name=”submit” class=”btn-subscribe” value=”Submit” />
</fieldset>
</form>

%d bloggers like this: