473,387 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Javascript alert question

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?
Oct 17 '05 #1
6 3118
bonehead wrote:

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.

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.
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.

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.
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,
I sincerely hope you don't really mean that.
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,
No - forms don't have an onclick method. Buttons and other things do though.
to call a javascript
function inside the <body> tag of the html which renders the form?
So you want the function to generate the form - the form which didn't
previously exist - when you click on it.
Does
anyone have an example of what I'm looking for?


No I've given up the psychotropics.

C.
Oct 17 '05 #2
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.

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.
Oct 17 '05 #3
bonehead (se**********@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).

Oct 17 '05 #4
On 17/10/2005 21:49, Colin McKinnon wrote:
bonehead wrote:


[snip]
Can I add an onclick attribute to the form,


No - forms don't have an onclick method. Buttons and other things do
though.


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.
Oct 17 '05 #5
On Mon, 17 Oct 2005 10:21:27 -0700, bonehead <se**********@here.net>
wrote:
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.
You have to output your error messages as proper Javascript code; this
might involve escaping various characters that have meaning in
Javascript or HTML.
2. When the alert box does appear, the content of the web page behind
the alert box disappears, i.e., the page goes blank.
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.
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.
It doesn't sound too bad.
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?


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,

Oct 18 '05 #6
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);
}

Oct 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
Is it possible to put XSL instructions inside JavaScript? For example, pop up a message box with the xml value: alert(value in xml); Approach #1: <script type="text/javascript">...
14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
15
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
12
by: Joe | last post by:
Hello All: I'm sure that one of you can quickly spot the error in my script. I am using a small javascript function to retrieve the text of a hyperlink in a datagrid column. Here is my...
13
by: amykimber | last post by:
Hi all, I know I'm doign something really daft, but I can't get this to work... I have a form with a bunch of inputs called ship_owner - why the ? Because I'm submitting this page though php...
6
by: den 2005 | last post by:
Hi everybody, Question 1: How do you set the values from server-side to a client-side control or how do you execute a javascript function without a button click event? Question 2: How do you...
14
by: maidane | last post by:
Hi, I got the following situation using the AC_Quicktime library. Apple recommend to use a Javascript call to generate the OBJECT tag (http://developer.apple.com/internet/ieembedprep.html). I...
1
by: Morten Snedker | last post by:
If my question is off this group, please point me in the right direction. Here goes: My scipt fails between alert-2 and alert-3 with "Object doesn't support this property or method" (freely...
16
by: Tony Girgenti | last post by:
Hello. I just finished reading two articles about javascript here: http://msdn2.microsoft.com/en-us/library/bb332123(VS.80).aspx I tried some javascript testing in VS2005, SP1, VB , .NET 2.0,...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.