Welcome to the Geeks & God Static Archive. Read more »

Is the ONBLUR event limited to Javascript ?

Joined: 11/28/2008

Hi,

As the subject title suggests; what are the limitations of the ONBLUR event ? Recently BobbyMac supplied some the following:

CODE
<INPUT TYPE="TEXT" NAME="text1" SIZE="40" onBlur="alert('The text box lost focus!');">

Is ONBLUR restricted to executing a JS function within the same file, or a function within a (JS) file defined in the "[script][/script]" definition ? Or, as the above code, an alert, or close window, etc,etc.

Would the following:

CODE
<INPUT TYPE="TEXT" NAME="text1" SIZE="40" onBlur= a=href(uri)>

be possible, where the uri was a PHP file ?

I'm trying to pass three values from three different fields to a PHP file, when the user leaves the third field. The PHP file works fine if I manually enter the parameters to be passed in the address bar of the browser, but I need to find a method to do this when the user leaves the third field on the form.

As a total stab in the dark (I'm desperate /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> ) , what about something like:

CODE
<form action="doit.php" method="post" name="frmOrder"> <input

type="hidden" name="confirm" value="true">

...... and then on the third field

<INPUT TYPE="TEXT" NAME="text1" SIZE="40" onBlur=document.frmOrder.submit();>

Although there still needs to be some method of passing parms to file 'doit.php'

Thanks,

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
Hi Peter There is a way of

Hi Peter

There is a way of "loading" a webpage (or submitting a form) where the server outputs a HTTP header that says - "there is no content to load, so don't go anywhere". This is a tip that Aspen from Sitepoint has suggested a few times for doing this - like submitting two forms on one page, and submitting a form without reloading the page (so the user can continue doing other things on the page).

Example:

Say you have a hyperlink like this:

CODE
<A HREF="hiddenload.php">Click This</A>

and then the "hiddenload.php" file might do some Database work or whatever, but it doesn't output any HTML. Instead:

CODE
<?php

header("HTTP/1.0 204 No Content" );

?>

When someone clicks the link - the "hiddenload.php" file is executed on the server, but the browser stays where it is. I think this would be what you want. So, to do it when the person leaves the field, you could do something along these lines:

CODE
<script language="Javascript">

<!--

function doHidden(theform) {

 // you can check the form values here

 theform.submit();

}

//-->

</SCRIPT>

<form action="hiddenload.php" method="post" name="frmOrder"> <input

type="hidden" name="confirm" value="true">

...... and then on the third field

<INPUT TYPE="TEXT" NAME="text1" SIZE="40" onBlur="doHidden(this.form);">

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Hi Paul, Thanks for the tips

Hi Paul,

Thanks for the tips on this one. It works about 99% now, so really I just have a one percent question. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

The code to do this is two files, TEST.PHP and DATETIME.PHP ; a cut-down version of test.php as follows:

CODE
<form action="datetime.php?name=name.value&company=company.value&email=email.value" method="post" name="frmOrder">

<input type="hidden" name="confirm" value="true">

<script language="Javascript">

<!--

function doHidden(theform) {

 // you can check the form values here

 theform.submit();

}

//-->

</SCRIPT>

<TD width="130">Full Name</TD>

<TD><input type="text" size="25" name="name"></td>

<tr><td>Company Name</td>

<td><input type="text" size="25" name="company"> </TD></TR>

<tr><td width="130">Email</td>

<td><input type="text" size="25" name="email"

onBlur="doHidden(this.form);"></td></tr>

<tr><td width="100">Phone Number</td>

<td><input type="text" size="25" name="phone"></td>

</tr>

and a full version of DATETIME.PHP

CODE
<?php

/*array of expected GET variables*/

/*Have to use $HTTP_GET_VARS for PHP 4.0.4pl1, change to $_GET for later versions of PHP*/

$expected = array( 'name' , 'company' , 'email' );

/*loop though the expected vars, check for a corresponding $_GET variable*/

foreach( $expected as $var ){

   if( !isset( $HTTP_GET_VARS[$var] ) ){

/*variable is missing exit or redirect etc*/

//        echo 'error';exit;

   }else{

       $var = $HTTP_GET_VARS[$var];

   }

}

phpinfo(INFO_VARIABLES);

//Gets the date and time in the format yyyy-mm-dd hh:mm:ss

$date = date ( "Y-m-d H:i:s" );

echo "insert into test values (NULL, '$date','$name','$company','$email')" ;

@mysql_pconnect("localhost", "dbuser", "dbpwd") or die ("cannot connect to database");

mysql_select_db("dbname");

mysql_query ("insert into test values (NULL, '$date','$name','$company','$email')") or die(mysql_error());

?>

If I 'skip' TEST.PHP and at the browser do something like http://domainname/datetime.php?name=Peter&...ail=us@work.net , it works fine. Also using TEST.PHP, as soon as the 'email' field is 'left' (tab to next fld or use mouse to get to next field), the ONBLUR is working, because that calls DATETIME.PHP and passes the correct parameters; all works okay.

Now to the one percent. What bafffles me is that to get it to work I had to comment out the line:

CODE
//        echo 'error';exit;

to stop it from exiting and not continuing. I had to change the "$_GET" to "$HTTP_GET_VARS" because I'm using PHP 4.0.4pl1. Notice I'm using PHPINFO() after the loop, and the variables are being displayed as:

HTTP_POST_VARS["company"] Work

etc. Back to the baffling part. Since I have commented out _that_ line to get it all to work, and the code after the ELSE statement is what appears to retrieve the parameters into variables, by the use of:

CODE
$var = $HTTP_GET_VARS[$var];

I can't see how the ELSE would get executed, because I have proved that the "error/exit" line was causing it to discontinue, therefore all things being equal, how are these parameters being evaluated ? Is it because

CODE
action="datetime.php?name=name.value&company=company.value&email=email.value"

creates (passes) a local variable to the form DATETIME.PHP ? If so, then the loop is not needed ?? In fact, I'm sure I put the phpinfo() at the start, and I could see the variables ! Wow, like a thunderbolt hit me, (durrrhh, boy am I dumb). Hmm, there are 4 variables in the form TEST.PHP, and HTTP_POST_VARS["phone"] has a null value, so the loop does work (3 parms passed, four fields in the form).

I can see now I don't have to actually 'GET' the parameters, the variables are already set/available. Is that correct ?

Thanks, /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
I hope this was the only

I hope this was the only question you had in the end - if not, please re-ask! /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

QUOTE
I can see now I don't have to actually 'GET' the parameters, the variables are already set/available. Is that correct ?

Yes, for your version of PHP (<4.2 I think) the PHP.ini setting register_globals is on by default, so any POST/GET variables will be automatically set as their "simple" names.

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Hi, Yes, for your

Hi,

QUOTE
Yes, for your version of PHP (<4.2 I think)  the PHP.ini setting register_globals is on by default, so any POST/GET variables will be automatically set as their "simple" names.

Thanks, that's good to know, also if I use PHP 4.2 or greater, I will have to use the POST/GET . As everything was now working okay, I decided to follow your suggestion and add the following:

CODE
header("HTTP/1.0 204 No Content" );

at the very top of DATETIME.PHP, so that the the form wouldn't actually 'go anywhere'.

QUOTE
... where the server outputs a HTTP header that says - "there is no content to load, so don't go anywhere".

I also commented out any 'echo' commands and the phpinfo() , so that nothing would display. However, now I get a 500 msg

QUOTE
malformed header from script. Bad header=HTTP/1.0 204 No Content: /home/username/cgi-bin/php

Anything to do with using such an old version of PHP (PHP 4.0.4pl1 ) ? Error msg aside, it did still add the required row, with all the correct data, into the MySQL db.

Oops, some lurking with Google and found an article called "No-Refresh links" at
http://www.sitepoint.com/article/314/2

QUOTE
If your server spits out a 500 error (internal server error), chances are good that you are running an Apache server with the CGI version of PHP, rather than the (quicker and more powerful) Apache module. In such configurations, you must instead use the following alternate syntax:

<? header("Status: 204 No Content"); ?>

So, I'll try that !! /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Thanks,

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
Hi Paul, Works great now, all

Hi Paul,

Works great now, all I see when leaving the 'email' field (ONBLUR event activated) is the mouse doing its salt timer thing, two seconds at the most. The current form remains in view, and a row is added to the db.

Thanks heaps for all your help, /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
Now all you need to add to

Now all you need to add to your page is a screen cam so you can acutally watch what they are doing! :wink:

No problems, good to see it's all working.

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Hi, Now all you need to

Hi,

QUOTE
Now all you need to add to your page is a screen cam so you can acutally watch what they are doing!

Na, can't afford that, I'll just capture all the keystrokes :wink: :wink:

Seriously though, what may appear as 'spying' is an attempt to indicate how many people start to fill out the order form, and then proceed no further. If they are not on the (email subscribed) newsletter, then we have no way of improving the service, because we can't contact them, and ask for their input, to improve the order side of things. That is why I wanted to make the 'db work' after they had filled in their email address, indicating already their willingness to supply the email adddress anyway.

Maybe the 'submit' button doesn't work sometimes, maybe other problems, the errors that are not apparent (web server logs), I'll never know about. Orders have gone down from about 25 per month to one per mth, ... why ? We need feedback. Also, the form has not been put into production as yet, because the working code now needs to be added to a much larger form.

Also, I do need to consider if it is an invasion of privacy, .... it could be ?? /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous

Joined: 11/28/2008
It depends what details you

It depends what details you are recording. I would say if you are storing the info they enter in (without submitting) then, yes, I would have an issue with that since they have not decided to hand that info over to you. As a "shop owner", you might be very interested in what motivates your customers to buy/not buy, but it's not information you are entitled to.

However, if you are just taking note of how many fields they fill out, which ones, to see if something is putting them off, then I wouldn't see a problem with it. Just make sure it doesn't backfire /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

You might also be interested in seeing if they click an out-going link on your page, for example making all your links on the order form page a bit like this:

CODE
<A HREF="exitorder.php?goto=aboutus.php">About us</A><BR>

<A HREF="exitorder.php?goto=products.php">Products</A><BR>

<A HREF="exitorder.php?goto=contact.php">Contact Us</A><BR>

Paul

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
Gidday Paul, It depends

Gidday Paul,

QUOTE
It depends what details you are recording.  I would say if you are storing the info they enter in (without submitting) then, yes, I would have an issue with that since they have not decided to hand that info over to you.  As a "shop owner", you might be very interested in what motivates your customers to buy/not buy, but it's not information you are entitled to.

Yes, I agree, if they haven't continued (submit), and I'm sure the form works, then the user definitely hasn't granted permission to hand their info over. But as the previous 'webmaster' left no documentation, and his email bock bounces, it's hard to know what has been changed, to possibly reduce orders so much. From some old files, there used to be prvision for other than Australia orders, but now it's restricted to Australia. The order form doesn't show the price, and I believe the drop-down list is messy, no order (pun intended). Also I _think_ there used to be more provision for a greater range of credit cards. How does one find out what the user would prefer ? Anyway, if you have a few mins, take a look at https://anchor.net.au/secure/imeg.com.au/or...rder-secure.php , and see if you like it. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

QUOTE
You might also be interested in seeing if they click an out-going link on your page, for example making all your links on the order form page a bit like this:
CODE
<A HREF="exitorder.php?goto=aboutus.php">About us</A><BR>

<A HREF="exitorder.php?goto=products.php">Products</A><BR>

<A HREF="exitorder.php?goto=contact.php">Contact Us</A><BR>

Paul

Thanks, I'll try that sometime, good idea.

Regards,

Peter

"The church is never a place, but always a people;
never a fold but always a flock;
never a building but always a believing assembly.
The church is you who pray,
not where you pray."
-Anonymous