View previous topic :: View next topic |
Author |
Message |
iarfhlaith Smarty Rookie
Joined: 20 Dec 2006 Posts: 13 Location: Dublin, Ireland
|
Posted: Wed Jan 02, 2008 11:08 am Post subject: AJAX and SmartyValidate |
|
|
Hi All,
I've been using SmartyValidate now for over a year, and I love it. It's made the form validation side of writing apps, much easier.
I've been trying to find the best way to use it with jQuery's ajax features and I keep coming up with the same problem.
Here's some background info the the issue:
- When the form loads initially I connect to SmartyValidate and register the validators.
- Then, when the form is submitted I serialize the data and post it all up to a page that handles the actual form validation (the submission is done via jQuery's AJAX and not posted in the traditional way).
- When I connect to SmartyValidate from the validating ajax page the registered validators have been reset.
So, my question is: How can I post form information to a new ajax page that hasn't been used to register the validators and at the same time use the original page to display any form error notices that need to be shown?
Here's some code to give you an idea of what I'm talking about:
The Form:
Code: |
<div id='notice'></div>
<form name='reminder' id='reminder'>
<input type='text' id='email' name='email' value='' />
{validate id='notice' message="Please enter in a valid email address"}
<input type='submit' id='submit' name='submit' value='Submit' />
</form>
|
The PHP Code:
Code: |
SmartyValidate::connect($smarty, true);
SmartyValidate::register_validator('notice', 'email', 'isEmail', false);
// Display in Template
$smarty->display('reminder.tpl');
|
The jQuery AJAX Code
Code: |
$(document).ready(function()
{
$('#email').focus();
$('#reminder').submit(function (){
var formVars = $('form').serialize();
$.ajax
({
type: 'POST',
url: '/app/ajax/reminder.php',
data: formVars,
dataType: 'json',
success: function(data)
{
if(data['response']['result'] == '0')
{
// form was valid
}
else
{
// form was NOT valid
}
},
error:function(data)
{
// there was a comm error
}
});
return(false);
});
});
|
the JSON response (if the form wasn't valid)
Code: |
{'response':{'result':'1','text':'Please complete the form'}}
|
And The PHP Form Validator submitted via AJAX
Code: |
$text = 'no info was submitted';
$result = 1;
if(!empty($_POST))
{
SmartyValidate::connect($smarty);
if(SmartyValidate::is_valid($_POST))
{
SmartyValidate::disconnect();
// The form was valid...
$text = 'success';
$result = 0;
}
}
$smarty->assign('text' , $text);
$smarty->assign('result', $result);
// Display in Template
$smarty->display('json.tpl');
|
If anyone has ever tried to use jQuery and SmartyValidate together and have any suggesions, or if someone can point me in the right direction from here I'd really appreciate it.
Oh and a happy new year to everyone as well!
Iarfhlaith |
|
Back to top |
|
iarfhlaith Smarty Rookie
Joined: 20 Dec 2006 Posts: 13 Location: Dublin, Ireland
|
Posted: Thu Jan 03, 2008 9:42 am Post subject: |
|
|
OK, I think I'm over complicating the question. Here's what I really need to know:
Is there a way to have a php page register the validators, then another to check if they're valid and then have the original page display any error messages that might need to be shown?
So for example:
PHP Script 1: Connects to SmartyValidate & Registers Validators
PHP Template 1: Contains the Form and the {validate} tags
PHP Script 2: (accessed via AJAX) The form variables are processed here and SmartyValidate is instantiated again, without resetting and it's checked to see if the form was valid.
It then responds to PHP Script 1 and returns a message in JSON format.
Then PHP Template 1 displays any validation errors.
----------------------------------------------------------------
So far, I've had problems when reconnecting to SmartyValidate in PHP Script 2. It isn't remembering that I've registered it already.
If you need a code sample, please see above.
Thanks a million for any help anyone can give me on this as it's really something I want to get sorted before progressing anymore on this particular project.
Many thanks in advance,
Iarfhlaith |
|
Back to top |
|
louenlax Smarty n00b
Joined: 24 Oct 2009 Posts: 1
|
Posted: Thu Oct 29, 2009 7:24 am Post subject: |
|
|
Are there any IDE's available, to develop website using Ajax? I am having a project, in which I have to develop a website using Ajax, and hardcoding ajax is very difficult and time consuming. So are there any Ajax IDE's available which can help in rapid development of web-site?
___________________
yahoo keyword tool ~ overture ~ traffic estimator ~ adwords traffic estimator
Last edited by louenlax on Fri Oct 30, 2009 10:45 am; edited 1 time in total |
|
Back to top |
|
iarfhlaith Smarty Rookie
Joined: 20 Dec 2006 Posts: 13 Location: Dublin, Ireland
|
Posted: Thu Oct 29, 2009 9:43 am Post subject: Use a Framework |
|
|
@louenlax I think the best thing for you here is to use a js framework rather then trying to find an editor that will write code for you. There are some excellent frameworks out there, my own favourite being jQuery, but Prototype is very capable also.
If you're really set on finding an IDE that has Ajax script support, then try using Aptana. It's a spin off of Eclipse and focuses on web development. We use it at Webstrong and it's great.
I hope you find what you're looking for in a js framework. One of their best features is that they extract the developer away from writing the nitty gritty ajax code needed to make it work in all browsers. Instead they use standard library calls, which makes life much easier. |
|
Back to top |
|
djkee Smarty n00b
Joined: 23 Nov 2009 Posts: 3
|
Posted: Mon Nov 23, 2009 4:00 am Post subject: Give a try |
|
|
I should give this a try
I have some major trouble with ajax and smarty |
|
Back to top |
|
ardienne Smarty n00b
Joined: 19 Jan 2010 Posts: 2
|
Posted: Sun Jan 24, 2010 7:56 am Post subject: |
|
|
Can you hide you javascript through loading it through ajax? I am no expert but i think ajax loads your js from a server and injects it into the page at run-time is there any way to load your js but hide it at the same time, that woulnd't realy make sense though thinking about it. Basically can you run your js from server side using ajax or some other method and hence hide it from client side users?
______
asian matrimonial |
|
Back to top |
|
BbErSeRkK Smarty Rookie
Joined: 04 Jan 2008 Posts: 14
|
Posted: Mon Mar 01, 2010 3:26 am Post subject: |
|
|
yes if i understand what you are asking, it is possible.
you can use an ajax call to prime smarty validate, then a second call to submit the data.
if your validation fails on the second ajax call you will need to do an execute on the template in order to get the smartyvalidate error messages in, then pass the results of that (using json)
do you still need help with this? |
|
Back to top |
|
elizas Smarty n00b
Joined: 25 Mar 2010 Posts: 2
|
Posted: Thu Mar 25, 2010 12:35 pm Post subject: Reset or clear the form data when form is submitted by AJAX |
|
|
When a form is submitted through submit button click the form data is subsequently cleared or reset . But when we submit a form by AJAX then we have to clear the form data by picking every element and setting the value as blank. If a form contains more than 100 input fields then it is a tedious job to clear or reset the form data.
But jQuery can help you avoid this problem, itoffers many different ways to clear form data more easily. I have written some functions based upon different requirements.
Hope this tip is useful |
|
Back to top |
|
|