0

So I'm using jQuery and the validation plugin to validate form input on the client side. Of course I need to revalidate it on the server. I want to statically put the errors tags into the HTML for all form items. So even if there's no error they'll still be there. I want to do this so that the jQuery validation and PHP can both output to the same tags.

Now my question is this - How can I force the validation plugin to output to the existing tag? Is it possible and if not, how could I accomplish what I want? Surely this is a pretty common thing to want to do, and yet I haven't been able to find any answers (maybe I just don't know what to search for).

Here's a little example of what I'm after.

<li>
   <label for="description" class='form_desc'>Description</label>
   <textarea name="description" id='description' class='required' title="A description of your listing." rows="15" ></textarea>
   <label for='description' class='error'>This is the tag I want to use.</label>
</li>
2

1 Answer 1

1

If I understand your question correctly you can use the jquery validation. It's easier to use. Take a look at the below code.

        jQuery("#description").validate({
            expression: "if(VAL) return true; else return false;",
            message: "<?php echo JText::_('DESC_ERROR_BLANK'); ?>"
        });

At the server level(PHP) you can define the DESC_ERROR_BLANK like below.

define("DESC_ERROR_BLANK", "This is the tag I want to use");

At the server level

If($_POST['description'] == '')
{
 //php validation goes here
 //use the same 'DESC_ERROR_BLANK' to output the error here
}

Hope this will solve your problem. If you have any questions please don't hesitate to ask. Thanks.

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't explain myself very well. I'm not using jquery within php at all, but I want to force jQuery (on the client) to output to the tags that php will create within the document. This will make it easier for me to display errors in a consistent manner. Basically if I have HTML code as above, is there a way to make jQuery not create a new element, but use the existing one?
Why did you accept this answer if it's not actually what you wanted?
I think it might have helped him

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.