Connecting Tech Pros Worldwide Forums | Help | Site Map

Javascript alert question

bonehead
Guest
 
Posts: n/a
#1: Oct 17 '05
Greetings,

I'm working on an e-mail form (btw many thanks to Philip Ronan for the
very cool email address format tester function, best I've seen so far).

I've been trying, with limited success, to produce a javascript alert
box which shows the user a validation error message. The error message
itself is dynamic, i.e., it's contained within a php variable.

I seem to be running into two issues:

1. I can raise the alert box, but I can't pass the string contained in
the $myerrormessage variable. Clearly I don't quite have the syntax just
right.

2. When the alert box does appear, the content of the web page behind
the alert box disappears, i.e., the page goes blank. When the user
clicks the OK button on the alert box, the blank web page remains, and
the user must click the Back button to return to the form.

The html that generates the form is wrapped inside a php function (the
file itself is php). The 'action' attribute of the form is "<?php echo
$PHP_SELF ?>". The form also contains a hidden field called 'action'
whose value is 'send_mail'.

When the user clicks the submit button, the script calls itself
recursively, and the $action variable's value, 'send_mail', is sent to a
switch. The switch calls a send_mail function, which performs all the
validation checks and generates an html error message if $myerrormessage
is not empty. This script was based on models provided in this book:

http://www.wrox.com/WileyCDA/WroxTit...764557440.html

Currently I'm rendering the error message via another php function which
produces the html containing $myerrormessage. This works just fine, of
course, but I'm afraid some wiseguy web developer out there will look at
it and proceed to tell me all about how lame it is.

Can I add an onclick attribute to the form, to call a javascript
function inside the <body> tag of the html which renders the form? Does
anyone have an example of what I'm looking for?



Colin McKinnon
Guest
 
Posts: n/a
#2: Oct 17 '05

re: Javascript alert question


bonehead wrote:
[color=blue]
>
> I've been trying, with limited success, to produce a javascript alert
> box which shows the user a validation error message. The error message
> itself is dynamic, i.e., it's contained within a php variable.
>
> I seem to be running into two issues:
>
> 1. I can raise the alert box, but I can't pass the string contained in
> the $myerrormessage variable. Clearly I don't quite have the syntax just
> right.
>[/color]

Bearing in mind that you may be getting OT....

What javascript error are you getting? What does the HTML from your script
say (just the bit around the alert()). You seem to be using Firefox - take
a look at the javascript console - or better yet install Venkman.
[color=blue]
> 2. When the alert box does appear, the content of the web page behind
> the alert box disappears, i.e., the page goes blank. When the user
> clicks the OK button on the alert box, the blank web page remains, and
> the user must click the Back button to return to the form.
>[/color]

erm...because they had to submit the page to get the PHP to run, and your
PHP didn't write a new page...or the PHP died trying to execute your
code....or you found one of the many bugs in internet explorer which
produce this kind of behaviour.
[color=blue]
> The html that generates the form is wrapped inside a php function (the
> file itself is php). The 'action' attribute of the form is "<?php echo
> $PHP_SELF ?>". The form also contains a hidden field called 'action'
> whose value is 'send_mail'.
>
> When the user clicks the submit button, the script calls itself
> recursively,[/color]

I sincerely hope you don't really mean that.
[color=blue]
> and the $action variable's value, 'send_mail', is sent to a
> switch. The switch calls a send_mail function, which performs all the
> validation checks and generates an html error message if $myerrormessage
> is not empty. This script was based on models provided in this book:
>
> http://www.wrox.com/WileyCDA/WroxTit...764557440.html
>
> Currently I'm rendering the error message via another php function which
> produces the html containing $myerrormessage. This works just fine, of
> course, but I'm afraid some wiseguy web developer out there will look at
> it and proceed to tell me all about how lame it is.
>
> Can I add an onclick attribute to the form,[/color]

No - forms don't have an onclick method. Buttons and other things do though.
[color=blue]
> to call a javascript
> function inside the <body> tag of the html which renders the form?[/color]

So you want the function to generate the form - the form which didn't
previously exist - when you click on it.
[color=blue]
> Does
> anyone have an example of what I'm looking for?[/color]

No I've given up the psychotropics.

C.
bonehead
Guest
 
Posts: n/a
#3: Oct 17 '05

re: Javascript alert question


Colin McKinnon wrote:[color=blue][color=green]
>>Does
>>anyone have an example of what I'm looking for?[/color]
>
> No I've given up the psychotropics.[/color]

Okay, fine, just show me whatever you consider to be a good example of a
php e-mail web form with validation and error handling. That's all I'm
really looking for. It doesn't have to have any javascript in it at all.

Meanwhile I'll send a message to the authors of the book I cited telling
them that you'll shortly be explaining exactly what's wrong with using
$PHP_SELF as the action argument of a form, and providing a much better
example.
Malcolm Dew-Jones
Guest
 
Posts: n/a
#4: Oct 17 '05

re: Javascript alert question


bonehead (sendmenospam@here.net) wrote:
: Colin McKinnon wrote:
: >>Does
: >>anyone have an example of what I'm looking for?
: >
: > No I've given up the psychotropics.

: Okay, fine, just show me whatever you consider to be a good example of a
: php e-mail web form with validation and error handling. That's all I'm
: really looking for. It doesn't have to have any javascript in it at all.

Hum, if that's what you want then why didn't you just say so.

PhpBB seems to handle sending mail just fine, perhaps that application
could provide suitable examples for you of getting and using email
addresses.

There are also loads of html and cgi books that discuss validating form
input.

There have been lots of threads here (i.e. googling time) about what sort
of validation is needed by am "email form". Key item is that the email
addresses must be controlled serverside by the php script during the
invocation when it actually sends the mail. (I.e. hidden fields in forms,
or javascript validation of email addresses is useless.)


: Meanwhile I'll send a message to the authors of the book I cited telling
: them that you'll shortly be explaining exactly what's wrong with using
: $PHP_SELF as the action argument of a form, and providing a much better
: example.

Hum, I didn't notice him saying he was going to do any such thing. Are
you trying to be sarcastic? If so, I'm afraid you put your foot in your
mouth since what he questioned was your claim of "recursive" calls using
that variable. I think it highly doubtful the wrox example recursively
calls itself, which is not at all the same thing as using it as the action
of a form - though from your verbose, technically loose description, I am
not sure you would quite understand that (yet).

Michael Winter
Guest
 
Posts: n/a
#5: Oct 17 '05

re: Javascript alert question


On 17/10/2005 21:49, Colin McKinnon wrote:
[color=blue]
> bonehead wrote:[/color]

[snip]
[color=blue][color=green]
>> Can I add an onclick attribute to the form,[/color]
>
> No - forms don't have an onclick method. Buttons and other things do
> though.[/color]

Incorrect. FORM elements do dispatch click events and fire click
listeners. In fact, most elements do.

Search a DTD for occurrences of the %events; parameter entity reference
(including any other entities, like %attr;, that use it). You'll find
that it comes up quite frequently.

The %events; parameter entity definition also defines other event types,
including those for mouse and keyboard interaction. The remaining types
- blur, change, load, submit etc. - are limited to certain specific
elements.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Wayne
Guest
 
Posts: n/a
#6: Oct 18 '05

re: Javascript alert question


On Mon, 17 Oct 2005 10:21:27 -0700, bonehead <sendmenospam@here.net>
wrote:
[color=blue]
>1. I can raise the alert box, but I can't pass the string contained in
>the $myerrormessage variable. Clearly I don't quite have the syntax just
>right.[/color]

You have to output your error messages as proper Javascript code; this
might involve escaping various characters that have meaning in
Javascript or HTML.
[color=blue]
>2. When the alert box does appear, the content of the web page behind
>the alert box disappears, i.e., the page goes blank.[/color]

This can happen depending on how your javascript is being called. If
you have something like this:

<a href="javascrpt:callSomeFunction();">Click Me</a>

Then this will cause the browser to redirect to a blank page.
[color=blue]
>Currently I'm rendering the error message via another php function which
>produces the html containing $myerrormessage. This works just fine, of
>course, but I'm afraid some wiseguy web developer out there will look at
>it and proceed to tell me all about how lame it is.[/color]

It doesn't sound too bad.
[color=blue]
>Can I add an onclick attribute to the form, to call a javascript
>function inside the <body> tag of the html which renders the form? Does
>anyone have an example of what I'm looking for?[/color]

Try hooking into the onsubmit event of the form. You can return false
to cancel the submit or return true to allow it. In that function,
you can you validation and show any alert boxes.

Later,

Meiao
Guest
 
Posts: n/a
#7: Oct 18 '05

re: Javascript alert question


I'd take a look at PEAR's QuickForm
it would be something like this:

<?php
require_once('HTML/QuickForm.php');

$form = new HTML_QuickForm('emailform', 'POST');

$form->addElement('text', 'email', 'Whats your email?');

$form->addRule('email', 'Please, type your email', 'required');

//not completely sure about this one
$form->addRule('email', 'Please, type your REAL email', 'regex',
'/[^@]+@.*/');

if($form->validate()){
$form->freeze();
$form->process('processf', false);
}
else{
$form->display();
}
?>

You must also provide de processf function;

function processf($values){
//this will show you the form values
print_r($values);
}

Closed Thread