How to stop form submission after validation has suceeded - php

I was wondering if there was a way I could stop the form from being submitted normally after Jquery validation has actually succeeded.
I would like some other function to take care of submitting the form. I'd like the form to be validated but not submitted. Hope anyone can help
I'm using the VarienForm validation class.
here'a an example below:
var newform = new VarienForm('newform', true);
Thanks in advance.
here's my form:
<form id="newform" >
<div class="fieldset">
<h2 class="legend">User Details</h2>
<ul class="form-list">
<li class="fields">
<div class="field">
<label for="firstname" class="required"><em>*</em>First Name</label>
<div class="input-box">
<input name="firstname" id="firstname" title="First Name of Staff" value="<?php if (isset($user->firstname)){ echo $user->firstname; } ?>" class="input-text required-entry" type="text" tabindex="1"/>
</div>
</div>
<div class="field">
<label for="lastname" class="required"><em>*</em>Last Name</label>
<div class="input-box">
<input name="lastname" id="lastname" title="Last Name of Staff" value="<?php if (isset($user->lastname)){ echo $user->lastname; } ?>" class="input-text required-entry" type="text" tabindex="2"/>
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="othername" class="required"><em>*</em>Other Name (s)</label>
<div class="input-box">
<input name="othername" id="othernames" title="Other Name(s)" value="<?php if (isset($user->othername)){ echo $user->othername; } ?>" class="input-text" type="text" tabindex="3"/>
</div>
</div>
<div class="field">
<label for="phone" class="required"><em>*</em>Phone Number</label>
<div class="input-box">
<input name="phone" id="phone" title="Phone Number" value="<?php if (isset($user->phone)){ echo $user->phone; } ?>" class="input-text validate-number" type="text" tabindex="4"/>
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="username" class="required"><em>*</em>User Name</label>
<div class="input-box">
<input name="username" id="username" title="User Name" value="<?php if (isset($user->username)){ echo $user->username; } ?>" class="input-text required-entry" type="text" tabindex="5" />
</div>
</div>
<div class="field">
<label for="email" class="required"><em>*</em>Email</label>
<div class="input-box">
<input name="email" id="email" title="User Email" value="<?php if (isset($user->email)){ echo $user->email; } ?>" class="input-text validate-email" type="text" tabindex="6" />
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="password" class="required"><em>*</em> Password</label>
<div class="input-box">
<input name="password" id="password" title="Password" value="<?php // if (isset($user->password2)){ echo $user->password2d; } ?>" class="input-text validate-password required-entry" type="password" tabindex="7"/>
</div>
</div>
<div class="field">
<label for="password2" class="required"><em>*</em>Confirm Password</label>
<div class="input-box">
<input name="password2" id="password" title="Confirm Password" value="<?php // if (isset($user->password2)){ echo $user->password2; } ?>" class="input-text validate-password required-entry validate-cpassword" type="password" tabindex="8"/>
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="Role" class="required"><em>*</em>Role</label>
<div class="input-box">
<?php $roles = Role::find_all(); if ($roles){ ?>
<select name="role" id="role" class="required-entry" tabindex="9" value="">
<option value="" selected="selected">SELECT ROLE</option>
<?php foreach ($roles as $role) : ?>
<option value="<?php echo $role->id; ?>"><?php echo $role->name ?></option>
<?php Endforeach; ?>
</select>
<?php } else { echo "No Roles Found! Add Role"; } ?>
</div>
</div>
<div class="field">
<label for="Status" class="required"><em>*</em>Status</label>
<div class="input-box">
<select name="status" id="status" class="required-entry" tabindex="10" value="">
<option value="1" selected="selected">Active</option>
<option value="0">Inactive</option>
</select>
</div>
</div>
</li>
</ul>
<div class="buttons-set">
<input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
<input type="submit" name="submit" value="SAVE" class="buttons-set" />
</div>
</div>
</form>

Since you tagged jQuery, I'm going to give you a jquery solution:
$("#newform").submit(function() {
return false;
});
Similarly, you can use this variation (actually this is the right way to do it)
$(document).ready(function() {
// I'm not familiar with VarienForm
// but the statement below runs once all DOM elements
// become available.
var newform = new VarienForm('newform', true);
// The block of code below only runs after you click
// the submit button in your form with id="newForm"
$("#newform").submit(function(e) {
// you can put a condition here, for example:
// if your form validation is correct return true;
// otherwise run e.preventDefault()
e.preventDefault();
});
});

You need to return false in your "onSubmit" method.
More details are explained in this similar post: How to prevent buttons from submitting forms

You can give the form an onsubmit="function" and in the function do an return false, before you return the false run another function.

First take a look at code i wrote below :
var x=true;
$('#newform').submit(function(e){
if(x) e.preventDefault();
myfun($(this));
});
function myfun(p) {
//lots of work...(simulating with timeout it could be anything ie. 2*2*2...)
setTimeout(function(){
x=false;
p.submit();
},1000);
}
Link to demo: >>> http://jsfiddle.net/techsin/QwWfb/
Main idea stop execution the time you want it to stop...but when ready to submit after doing whatever, let the stopping function know that it's not needed anymore. So when you call submit it actually goes through.
Control x to control submitting.

Related

php pass single product to cart with hidden field

I am trying to redirect a form to a shopping cart with one product in it at the same price. There is a redirect in place that if the cart it empty it goes to another page an I would like that to not happen. Here is the form code:
<form method="post" onSubmit="return varifyForm()">
<?php
if( $this->session->flashdata('error_msg') != ''){
?>
<p style="font-weight:bold; color:#F00;"><?php echo $this->session->flashdata('error_msg'); ?></p>
<?php
}
?>
<?php
if( $this->session->flashdata('success_msg') != ''){
?>
<p style="font-weight:bold; color:#0F0;"><?php echo $this->session->flashdata('success_msg'); ?></p>
<?php
}
?>
<div class="form-group">
<label><strong>Name*</strong></label>
<div class="row">
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="First" style="margin-bottom:5px;" id="" name="terms[first_name]" value="" required>
</div>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Last" style="margin-bottom:5px;" id="" name="terms[last_name]" value="" required>
</div>
</div>
</div>
<div class="form-group">
<label><strong>Password*</strong></label>
<div class="row">
<div class="col-sm-6">
<input type="password" class="form-control" id="" name="password" value="" placeholder="Enter Password" style="margin-bottom:5px;" required>
</div>
<div class="col-sm-6">
<input type="password" class="form-control" placeholder="Confirm Password" id="" name="confirm_password" value="" style="margin-bottom:5px;" required>
</div>
</div>
</div>
<div class="form-group">
<label><strong>Email*</strong></label>
<input type="email" class="form-control" id="" name="terms[email]" value="" placeholder="Email" required>
</div>
<div class="form-group">
<label for="sel1"><strong>User Role*</strong></label>
<select class="form-control" id="user_role" name="terms[user_role]">
<option value="Member">Member</option>
</select>
</div>
<div class="form-group">
<label for="sel1"><strong>Your Cause*</strong></label>
<select class="form-control" id="cause" name="terms[cause]">
<option value="<?php echo $causes['user_id']; ?>"><?php _e($causes['nonprofit_name']); ?></option>
</select>
</div>
<div class="form-group">
<label><strong>Zip Code*</strong></label>
<input type="text" class="form-control" id="" name="terms[zip]" value="" required>
</div>
<div class="form-group">
<label><strong>Terms and Conditions*</strong></label>
<div class="checkbox-inline full">
<input type="checkbox" id="terms">
<strong>I agree to the</strong> <strong>Terms and Conditions</strong>
</div>
</div><br>
<button type="submit" name="btnSignUp" value="signup" class="btn btn-primary"><p style="margin:0;">NEXT</p></button>
</form>
I have been stuck fixing someone else's work and appreciate any help that can be given. Let me know if any other information is needed.
Thank you!
Hi as you mentioned the below sample code may helpful to you. If you require additional help please tell me.
if(!empty($_POST['cart'])){//what ever the cart item name please replace with it.
$cart["{item_id}"] = ["quantity"=>"{qty}","price"=>"{fixed _price}"];
$_SESSION['cart']=$cart;
header('Location: redirect.php'); //replace with your actual redirect page to redirect.php
}else{
header('Location: redirect_other.php'); //replace with your actual other redirect page to redirect_other.php
}

Custom contact form: Unable to submit your request. Please, try again later

I created a new contact form in:
app/design/frontend/base/default/template/contacts
Named form2.phtml
I am getting the error: Unable to submit your request. Please, try again later.
Here's my code:
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>
<h2>Questions? Comments?<br>
Contact us here and we’ll get back to you shortly.</h2>
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm2" class="footer-form" method="post">
<div class="fieldset">
<ul class="form-list">
<div class="form-row">
<li>
<div class="input-box">
<input placeholder="First Name *" name="first name" id="name" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('First Name')) ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
</div>
</li>
<li>
<div class="input-box">
<input placeholder="Last Name *" name="last name" id="name" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Last Name')) ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
</div>
</li>
</div>
<div class="form-row">
<li>
<div class="input-box">
<input placeholder="Email *" name="email" id="email" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Email')) ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
</div>
</li>
<li>
<div class="input-box">
<input placeholder="Telephone" name="telephone" id="telephone" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Telephone')) ?>" value="" class="input-text" type="text" />
</div>
</li>
</div>
<li class="wide">
<div class="input-box">
<textarea placeholder="Message *" name="comment" id="comment" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Comment')) ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
</div>
</li>
</ul>
</div>
<div class="buttons-set">
<input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
<button type="submit" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('contacts')->__('Submit')) ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('SEND') ?></span></span></button>
</div>
</form>
<script type="text/javascript">
//<![CDATA[
var contactForm = new VarienForm('contactForm', true);
//]]>
</script>
Any ideas what would be causing this? The default form works fine when I switch it back to it. Did I miss a step somewhere?
Thanks
Since you have the "hideit" input added, commented out
if (Zend_Validate::is(trim($post[’hideit’]), ‘NotEmpty’)) {
$error = true;
}
inside app/code/core/Mage/contacts/controllers/indexController.php

undefined index in php using post but it can echo the value using request on update.php

I have these two files and i am updating a table but on the update.php file i get error saying Notice: Undefined index: pname , $pcode...... $pname = $_POST["pname"]; here i am storing pname value in pname variable.but it shows error
<address>
<div class="control-group">
<label class="control-label">Product Name:</label>
<div class="controls">
<input id="pname" name="pname" type="text" value="<?php echo $PName; ?>"
class="input-xlarge">
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<label class="control-label">Product Code:</label>
<div class="controls">
<input id="code" name="pcode" type="text" value="<?php echo $PCode; ?>"
class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label">Product Price:</label>
<div class="controls">
<input id="price" name="pprice" type="text" value="<?php echo $PPrice; ?>"
class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label">Availability:</label>
<div class="controls">
<select id="stock" name="stock" class="input-xlarge">
<option value="1" selected="selected">Available</option>
<option value="0">Not Available</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Product Description:</label>
<div class="controls">
<textarea rows="4" cols="50" name="description" > <?php echo $PDescription; ?> </textarea>
</div>
</div>
<div class="control-group">
Picture 1 :
<div class="controls">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<input type="file" name="pic1"
class="input-xlarge">
</div>
</div>
<div class="control-group">
Picture 2 :
<div class="controls">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<input type="file" name="pic2"
class="input-xlarge">
</div>
</div>
<div class="control-group">
Picture 3 :
<div class="controls">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<input type="file" name="pic3"
class="input-xlarge">
</div>
</div>
<div class="control-group">
<div class="controls">
<input type='hidden' name='id' value="<?php echo $ID; ?>"><br/>
<input type="submit" value="UPDATE" class="btn-large" />
</div>
</div>
</form>
Do you check for form submit?
if (isset($_POST["pname"])) {
// do all your form processing here.
}
$pname= (isset($_POST["pname"]) ? $_POST["pname"] : "");
this will check if pname exists in $_POST and then sets it.
you receive that error because you dont have set $_POST["pname"]

Reset button using JavaScript is not working after validating the form using Ajax

I have a "create user"form with reset and create button. This reset button is not working for some drop-downs in the form.
In addition, reset button is not clearing all the values in the form after validation. It only clears the values entered after validation.
Here is my code:
<div class="form-action clearfix">
<button type="button" onClick="clearForm()" > Reset </button>
<button class="button" type="button" id="button" onclick="doAjax('index.php/users/createUser', convertFormJson('#createUserForm'));">Create</button>
and
function clearForm() {
document.getElementById('createUserForm').reset();
}
Form:
<?php echo validation_errors(); ?>
<div class="container_12 clearfix leading" >
<div class="grid_12">
<!--<form class= "form has-validation">-->
<?php
$attributes = array('class' => 'form has-validation', 'id' => 'createUserForm');
echo form_open('/users/createUser', $attributes);
?>
<div class="clearfix">
<label for="form-name" class="form-label">First Name <em>*</em></label>
<div class="form-input">
<input type="text" id="form-name" name="firstname" required="required" placeholder="Enter the first name" value="<?php echo set_value('firstname'); ?>" />
</div>
</div>
<div class="clearfix">
<label for="form-name" class="form-label">Last Name <em>*</em></label>
<div class="form-input">
<input type="text" id="form-name" name="lastname" required="required" placeholder="Enter the last name" value="<?php echo set_value('lastname'); ?>"/>
</div>
</div>
<div class="clearfix">
<label for="form-email" class="form-label">Email <em>*</em></label>
<div class="form-input"><input type="email" id="form-email" name="email" required="required" placeholder="A valid email address" value="<?php echo set_value('email'); ?>"/></div>
</div>
<!--
<div class="clearfix">
<label for="form-birthday" class="form-label">Birthdate</label>
<div class="form-input"><input type="date" id="form-birthday" name="date" placeholder="mm/dd/yyyy" value="<?php //echo set_value('date'); ?>" class="date"/></div>
</div>
-->
<div class="clearfix">
<label for="form-username" class="form-label">Username <em>*</em></label>
<div class="form-input">
<?php echo form_error('username'); ?>
<input type="text" id="form-username" name="username" value="<?php echo set_value('username'); ?>" required="required" maxlength="30" placeholder="Alphanumeric (max 30 char.)" />
</div>
</div>
<div class="clearfix">
<label for="form-password" class="form-label">Password <em>*</em></label>
<div class="form-input">
<?php echo form_error('password'); ?>
<input type="password" id="form-password" name="password" value="<?php echo set_value('password'); ?>" maxlength="30" placeholder="Min 8 char. containing a capital, number and/or symbol" />
</div>
</div>
<div class="clearfix">
<label for="form-password-check" class="form-label">Password check <em>*</em></label>
<div class="form-input">
<?php echo form_error('passconf'); ?>
<input type="password" id="form-password-check" name="passconf" value="<?php echo set_value('passconf'); ?>" data-equals="password" maxlength="30" placeholder="Re-enter your password" />
</div>
</div>
<div class="clearfix">
<label for="form-group" class="form-label">Group <em>*</em></label>
<div class="form-input">
<?php
$groupsData[' '] = "--";
$groupJs = 'id="form-group"';
echo form_dropdown('groupId', $groupsData,' ',$groupJs);
?>
</div>
</div>
<div class="clearfix">
<label for="form-language" class="form-label" >Language <em>*</em></label>
<div class="form-input">
<select id="form-language" name="language">
<option value ="" > -- </option>
<option value ="1">English</option>
<option value ="2">French</option>
</select>
</div>
</div>
<div class="clearfix">
<label for="form-timezone" class="form-label" >Timezone <em>*</em></label>
<div class="form-input">
<?php echo timezone_menu('UM5', 'form-timezone'); ?>
</div>
</div>
Please help me out.

add data using ajax and codeigniter

I'm new in codeigniter
I try to add data in table users using ajax when I send data I have now problem and all data send good but no data stored in data base
and this is my code
controller
public function addusersajax()
{
if($this->input->post("action") =="addusersintable")
{
$this->load->helper('date');
$data=array(
"fullname"=> $this->input->post("fullname"),
"username"=> $this->input->post("username"),
"password" => md5($this->input->post("password")),
"email"=>$this->input->post("email"),
"groubid"=>$this->input->post("groubid"),
"date"=>mdate('%Y-%m-%d ', now()),
"time"=>date(" H:i:s")
);
$this->load->model("usersmodel");
if($this->usersmodel->adduserbyajax($data)){
echo "done";
}else{
echo 'failed';
}
}
}
this is function for view form
public function view()
{
$data['pagetitle']="xx";
$this->load->view("template/admin/header",$data);
$this->load->view("users/ss",$data);
$this->load->view("template/admin/footer");
}
this is my view
<div class="container">
<div id="container">
<div class="col-lg-8">
<form id="insercodegn" method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-name" class="control-label">Full Name :</label>
<input type="text" class="form-control" name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">UserName :</label>
<input type="text" class="form-control" name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="message-text" class="control-label">Password:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
<i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email :</label>
<input type="text" class="form-control" name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">User Type :</label>
<select class="form-control" name="groubid" id="groubid">
<option value="1">Administartor</option>
<option value="0">User</option>
<option value="2">Maker</option>
<option value="3">cheker</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputFile">Image :</label>
</div>
<input type="submit" name="action" value="adduserssss">
<button type="submit" name="action" value="addusersintable" class="btn btn-primary">addcc</button>
</form>
</div>
</div>
</div>
****this is my script inside ss viewpage****
<script>
$(document).ready(function () {
$(function () {
$("#insercodegn").on('submit', function (e) {
e.preventDefault();
$.ajax({
url:'<?php echo base_url()?>Users/addusersajax',
//url:"<?php echo base_url() .'Users/addusersajax'?>",
method:'post',
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success:function(data) {
alert(data);
}
})
})
})
})
</script>
my form is
<div class="container">
<div id="container">
<div class="col-lg-8">
<form id="insercodegn" method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-name" class="control-label">Full Name :</label>
<input type="text" class="form-control" name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">UserName :</label>
<input type="text" class="form-control" name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="message-text" class="control-label">Password:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
<i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email :</label>
<input type="text" class="form-control" name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">User Type :</label>
<select class="form-control" name="groubid" id="groubid">
<option value="1">Administartor</option>
<option value="0">User</option>
<option value="2">Maker</option>
<option value="3">cheker</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputFile">Image :</label>
</div>
<button type="submit" name="action" value="adduserssss" class="btn btn-primary">add</button>
</form>
</div>
</div>
</div>
Change this:
<input type="submit" name="action" value="adduserssss">
<button type="submit" name="action" value="addusersintable" class="btn btn-primary">addcc</button>
To this:
<input type="hidden" name="action" value="addusersintable">
<button type="submit" class="btn btn-primary">addcc</button>
Buttons don't have $_POST data thus: if($this->input->post("action") =="addusersintable") is always evaluating to false and never hitting your model. A hidden input is what you are looking for.
1) Use lower case url in your javascript file, try "users/addusersajax" instead of "Users/addusersajax".
2) Use var_dump($_POST) or print_r($_POST) in your controller. in network tab of Chrome or Firefox check to see result of your request. you must see what is posted. if it is all good you must check your model
3) In your model it is best to use transactions. see transactions documentations in Codeigniter user guide. also you can echo $this->db->last_query() to see what query is executed. copy the executed query and run it from phpMyAdmin or any other MySQL client you use and see if you get any error.

Categories