Re: prob with "nested java"

From: Date: Fri, 10 Nov 2000 02:53:37 +0000
Subject: Re: prob with "nested java"
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-24599@lists.php.net to get a copy of this message
Shane McBride writes: > I want to validate a text field before submission. Here's what I have: > > <form action="<? $PHP_SELF ?>" name="test"> > Name: <input type="text" name="name" size=30> > <input type="submit" value="Testing"> > > <? > if (!($name)) { > echo "<SCRIPT> alert('Enter a name'); </SCRIPT>"; > } else { > echo "This will do something in the future..."; > } > > ?> > > When I call this file in a broswer, it immediate pops up the Alert > box. How do I get past this? Secondly, it does not work in Netscape > when you do enter a name and submit it. > > Any ideas? > > Here's a link: www.rditech.net/php/validate.php > > TIA, > > Shane Because Javascript (in this sense) is client-side, and PHP is server-side. Basically, think of it this way: PHP is a program which writes web pages on the fly, sending them to the client as it goes. PHP can generate any kind of textual output: HTML, plaintext, Javascript, whatever. The Javascript, on the other hand, doesn't get executed until it's already loaded into the client's browser. By the time it gets there, PHP is no longer relevant to its execution. The practical upshot of this is that this PHP code: <? if (!($name)) { echo "<SCRIPT> alert('Enter a name'); </SCRIPT>"; } else { echo "This will do something in the future..."; } ?> ...checks to see whether $name has a false value. If it does, the text '<SCRIPT> alert('Enter a name'); </SCRIPT>' is sent to the browser. Since on initial loading (i.e. the first time the user goes to the page, before filling in the form and submitting it) $name has no value unless you've assigned it something earlier in the script. Therefore the test succeeds and the Javascript is sent to the browser, where it gets executed. The clientside Javascript interpreter never sees the if() statement. Your choices are: o Do the entire form validation in Javascript on the client's side. An example of this can be found at: http://www.xs4all.nl/~ppk/js/index.html?formex.html NOTE: if you *must* validate before submission, this is your only option (without going into Java/applets/etc). Be careful when doing this unless you know exactly what your users are using, though. For instance, if this page is meant for public consumption and not, say, a captive Intranet crowd where you know everybody has Javascript enabled, you'll have problems with some people not having Javascript enabled and thus breaking the setup. o Do the entire form validation in PHP. This means that you must submit the form data, check it, generate any necessary messages, and echo it back to the user. This time, though, you could take such steps as marking which form fields are invalid, fill them in with any already-entered valid information, etc. You could even take this opportunity to additionally echo back your alert script. Good luck, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#24599) next »