On CodeIgniter, how to repopulate a form only with correct values? - forms

Imagine a form like this:
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?=set_value('firstname')?>" />
<input type="text" name="lastname" value="<?=set_value('lastname')?>" />
<input type="text" name="email" value="<?=set_value('email')?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
If I submit an incorrect email, the CodeIgniter function will write "the field is not valid" and populate the invalid field with the wrong value.
I would like to keep the error message but not the wrong value (I prefer having an empty value). But I also want to keep the re-populating function for correct values.
Here is what I get:
Here is what I want:
[EDIT] SOLUTION FOUND (thanks to Herr Kaleun and Matt Moore)
Example with the email field:
<input type="text" name="email" id="email" value="<?=!form_error('email')?set_value('email'):''?>" />

You can check the fields individually and have a logic like this.
If the error for a specific field is present, you can build a logic on top of that.
if ( form_error('email') != '' )
{
$data['email_value'] = '';
}
else
{
$data['email_value'] = set_value('email');
}
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?=set_value('firstname')?>" />
<input type="text" name="lastname" value="<?=set_value('lastname')?>" />
<input type="text" name="email" value="<?= $email_value; ?>" />
<input type="submit" name="submit_form" value="OK" />
</form>

The Form_error is set when a form value errors out based on the validation. Judging by your use of the set_value function I assume you're using the built in form validation. You can check to see if the message for that field is set if(form_error('fieldname') !=NULL) and set the value based on that.
You may have to setup error messages for each field, which you should be doing anyway. Here is the guide that covers it: http://codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform
<form action="test.php" method="post">
<input type="text" name="firstname" value="<?if(form_error('firstname') != NULL){echo set_value('firstname');}?>" />
<input type="text" name="lastname" value="<?if(form_error('lastname') != NULL){ echo set_value('lastname');}?>" />
<input type="text" name="email" value="<?if(form_error('email') !=NULL){ echo set_value('email');}?>" />
<input type="submit" name="submit_form" value="OK" />
</form>
*NOTE THIS CODE HAS NOT BEEN TESTED *

Related

Shopify Omnisend Custom HTML Signup Form

I am using omnisend app for Shopify subscribe list. I am using a custom form to integrate the input data.
For the ominsend form, they have own class to capture data.
Like this:
<form class="my-form omnisend-subscribe-form" action="/subscribe" method="post">
<input class="omnisend-subscribe-input-email" type="text" placeholder="Enter your email address" />
<input class="omnisend-subscribe-input-first-name" type="text" placeholder="Enter your first name" />
<input class="omnisend-subscribe-input-last-name" type="text" placeholder="Enter your last name" />
<input type="submit" value="Subscribe!">
</form>
First Name: omnisend-subscribe-input-first-name
Last Name: omnisend-subscribe-input-last-name
Email: omnisend-subscribe-input-email
But for the birthday input data, I am unable to find the class.
How can I dynamic the birthday input data by class?
<form class="my-form omnisend-subscribe-form" action="/subscribe" method="post">
<input class="omnisend-subscribe-input-email" type="text" placeholder="Enter your email address" />
<input class="omnisend-subscribe-input-first-name" type="text" placeholder="Enter your first name" />
<input class="omnisend-subscribe-input-last-name" type="text" placeholder="Enter your last name" />
<input class="omnisend-subscribe-input-?" type="text" placeholder="MM" />
<input class="omnisend-subscribe-input-?" type="text" placeholder="DD" />
<input class="omnisend-subscribe-input-?" type="text" placeholder="YYYY" />
<input type="submit" value="Subscribe!">
</form>
Actually, there is no way to collect the birthday form the omnisend form. This form is not support to collect birthday.

Modx FormIt, got 2 forms, formit send both

This is my code, I can't understand why formit sends both the forms. It means I have 2 messages instead of one.
I use two templates and different &prefix:
[[!FormIt?
&hooks=`spam,email,emailUser`
&emailTo=`sitename#gmail.com`
&emailFromName=`[[+name]]`
&emailTpl=`ContactTpl`
&prefix="fi"
]]
<div>[[+fi.error.error_message]]</div>
<form action="[[~[[*id]]]]" method="post" id="recall">
<input type="hidden" name="nospam:blank" value="" />
<input type="text" name="name" class="right" id="recall_name" required="required" value="[[+fi.name]]">
<input type="number" name="number" class="right" id="recall_number"value="[[+fi.number]]" >
<textarea name="message:required:stripTags" value="[[+fi.message]]"></textarea>
<input type="submit">
</form>
[[!FormItRetriever]]
[[!FormIt?
&hooks=`spam,email,emailUser`
&emailTo=`sitename#gmail.com`
&prefix="cs"
&emailFromName=`[[+sup_name]]`
&emailTpl=`SupportTpl`
]]
<div>[[+fi.error.error_message]]</div>
<form action="[[~[[*id]]]]" method="post" id="support">
<input type="hidden" name="nospam:blank" value="" />
<label for="name" class="right" >Имя:</label>
<input type="text" name="name" class="right" id="support_name" required="required" value="[[+cs.sup_name]]">
<input type="number" name="number" value="[[+cs.sup_number]]">
<textarea name="message:required:stripTags"value="[[+cs.sup_message]]"></textarea>
<input type="submit">
</form>
take a look at the submitVar attribute: http://rtfm.modx.com/extras/revo/formit
your formit calls don't know which form they should be submitting. Set a name on each form submit control and set the submitVar for each in your formit calls.

Input elements not being posted with HTML5

I'm working on a test HTML5 login form and have the form set up like so:
<form id="login" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<h1>Log In</h1>
<fieldset id="inputs">
<input id="username" type="text" placeholder="Username" autofocus required>
<input id="password" type="password" placeholder="Password" required>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" name="submit_data" value="Log in">
</fieldset>
</form>
When clicking Submit, the form is posted back to the same page. When I print the array of POSTed elements, I'm only seeing one for 'submit_data'. 'username' and 'password' are not coming through.
Where am I going wrong?
You haven't specified names for your inputs, e.g.
<form id="login" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<h1>Log In</h1>
<fieldset id="inputs">
<input id="username" type="text" name="username" placeholder="Username" autofocus required>
<input id="password" name="password" type="password" placeholder="Password" required>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" name="submit_data" value="Log in">
</fieldset>
</form>
That might fix this problem.
Can you just perform a check for isset($_POST['username']) and isset($_POST['password']) instead of the print_r (which I assume you are using)?
<input type="..." NAME="username" ..> You haven't set var name.
Also instead of placeholder, set value="Username" and value="Password". There may not be any value passed if just using placeholder. See this test: http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_input_placeholder
As you submit without anything, no value is passed. Once you type something in, value is passed.

html / iphone - getting a form alert when attempting to refresh the page

I'm building a site which has a form but for some reason, even if a user doesn't enter info into the form (even if the form hasn't been presented yet - it starts out hidden) - if I refresh the page, iOS gives me a browser alert stating:
are you sure you want to submit this form again?
Any idea why this happens or how to suppress that?
This is my form code:
<div id="vehicle_form">
<form method="post" name="emailForm">
<input class="dField" id="dfEmail" type="email" name="email" value="Email"/><br/>
<input class="dField" id="dfName" type="text" name="firstname" value="First Name"/><br/>
<input class="dField" id="dfLast" type="text" name="lastname" value="Last Name"/><br/>
<input class="dField" id="dfZip" type="text" maxlength="5" name="zip" value="Zip Code"/><br/>
<button id="dFormBack">Back</button><button type="submit" id="dSubmit">Submit</button>
</form>
<div id="dErrors"></div>
</div>
I also have this javascript acting on the form fields:
$j('.dField').focus(function(){
clearInput($j(this)[0]); //The [0] seems to be necessary to retrieve the element at the DOM object level
}).blur(function(){
restoreInput($j(this)[0]);
});
as well as some other form related javascript.
Not sure if this is exactly what you're looking for, but try this:
<form method="post" name="emailForm">
<input type="text" name="email" value="Email" size="30" onfocus="value=''"><br>
<input type="text" name="firstname" value="First Name" size="30" onfocus="value=''"><br>
<input type="text" name="lastname" value="Last Name" size="30" onfocus="value=''"><br>
<input type="text" name="zipcode" value="Zip Code" size="30" onfocus="value=''"><br>
<button id="dFormBack">Back</button><button type="submit" id="dSubmit">Submit</button>
</form>

Select input value from two different forms in one page using mootools

I have two forms with different values in one page.
I want get input value of the form that user clicks on it's submit button, using mootools code.
Mootools code most work for two forms, as same:
<form class="cart_form" >
<input type="hidden" name="order_code" value="KWL-hgy" />
<input type="hidden" name="price" value="40000" />
<input type="hidden" name="name" value="modeling" />
<label>KWL-hgy: <input class="center" type="text" name="qty" value="2" size="3" ></label>
<input type="submit" name="submit" value="Add to cart" />
</form>
<form class="cart_form" >
<input type="hidden" name="order_code" value="KWL-JFE" />
<input type="hidden" name="price" value="12000" />
<input type="hidden" name="name" value="php" />
<label>KWL-JFE: <input class="center" type="text" name="qty" value="1" size="3" ></label>
<input type="submit" name="submit" value="Add to cart" />
</form>
you could do it somehow like this:
$$('form.cart_form').addEvent('submit', function(e){
this.getChildren().each(function(el){
alert(el.get('name') + ': ' + el.get('value'));
});
});
this will fire an event as soon as a form with the class chart_form is submited and loop through all the form's children.