Solved

php echo and jquery

Posted on 2012-04-03
2
408 Views
Last Modified: 2012-04-08
Hello Experts,

I have a very small form to email (php script).
The form checks for missing fields such as name and email.

like so

if ($name == "") {
      echo "please fill in your name";
      exit;
      };

Open in new window


I created a div with an absolute position where i would like these messages to appear.
I trying to understand how to integrate jquery to help me show the relevant messages in the absolute positioned div.

can someone show me a quick example please?
0
Comment
Question by:Refael
2 Comments
 
LVL 6

Accepted Solution

by:
uzzidesign earned 500 total points
ID: 37803495
I presume that you want to use jQuery to control the behavior of the message, more than just the position? jQuery in this case would be good to use for hiding the validation messages until they are needed. The best thing to do is to use PHP or jQuery to add or remove classes on the div, and to use those classes as hooks to show/hide and style it with CSS depending on its state. You should do as much as possible with CSS and style on the div itself. You could store different types of messages (e.g., div.error, div.success) in PHP variables and have styles predefined for each class.

One simple method would be to have the div hidden with a display:none until the message appears:
 
if ($('div').is(':empty') {
	$(this).hide();   // toggles display:none
} else {
	$(this).show();   // toggles display:block
}

Open in new window

or a better practice:
if ($('div').is(':empty') {
	$(this).addClass('hidden');	
} else {
	$(this).removeClass('hidden');	
}

Open in new window


But you can get fancier with the jQueryUI library with stuff like dialog popups: http://jqueryui.com/demos/dialog/

Here's an example: http://jsfiddle.net/ChXbr/5/
0
 

Author Closing Comment

by:Refael
ID: 37821868
Thank you!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now