Category Archives: Jscript

add user with ajax by submiting form

<form id="ajaxform" action="ajaxform" method="post">
	<p class="status"></p>
    <label for="uname">User name:</label>
    <input type="text" name="uname" id="uname" required>
    <label for="uemail">User email:</label>
    <input type="email" name="uemail"id="uemail" required>
    <input type="submit" value="Login" name="submit">
</form>

.js file

jQuery(document).ready(function($) {
// Perform AJAX login on form submit
	$('form#ajaxform').on('submit', function(e){
	$('form#ajaxform p.status').show().text(ajax_signup_object.loadingmessage);
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: ajax_signup_object.ajaxurl,
			data: {
			'action': 'signup', //calls wp_ajax_nopriv_ajaxsignup
			'uname': $('form#ajaxform #uname').val(),
			'uemail': $('form#ajaxform #uemail').val(),
			},
			success: function(data) {
			   
			}
		});
	e.preventDefault();
	});
});

functions.php

/* Enque script for ajax */
function prefix_ajax_signup() {
wp_register_script('ajax-signup-script', get_template_directory_uri() . '/js/deafult.js', array('jquery'));
wp_enqueue_script('ajax-signup-script');
wp_localize_script('ajax-signup-script', 'ajax_signup_object', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'loadingmessage' => __('User created successfully')
));
}
add_action('wp_enqueue_scripts', 'prefix_ajax_signup');
/* End Enque script for ajax */

/* Insert user using ajax */
function insert_user() {
$uname = $_POST['uname'];
$uemail = $_POST['uemail'];
$userdata = array(
'user_login' => $uname,
'user_email' => $uemail,
);
$user_id = wp_insert_user($userdata);
}
add_action( 'wp_ajax_signup', 'insert_user' );
add_action( 'wp_ajax_nopriv_signup', 'insert_user' );
Advertisements

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>



%d bloggers like this: