Using the onblur in PHP?

Hi,

I’ve read up a little bit on the ONBLUR (Javascript) event, and I would like to include it in a PHP file, and keep as much code invisible to the user, by using PHP. Is this possible ?

The objective is, when the user leaves the email field on a form, to insert one row into the MySQL db. I would like the insert code not to be visible to the user. At present, the PHP file does the MySQL connect, etc, at the start, displays some html, and the field in question is coded as:

<tr><td width="130"><FONT SIZE="2" COLOR="#66CCFF" FACE="Verdana, Arial">
Email</td>
<td>
<input type="text" size="25" name="email"></td></tr>

I would like to modify the above code, to include the ONBLUR event, so that when the user leaves the field ‘email’, one row is inserted into the MySQL db. Can the ONBLUR event simply call a PHP function ? I realise it can call a Javascript function, as per the example below:

Enter email address <INPUT TYPE="text" VALUE="" NAME="userEmail" onBlur=addCheck()>

<script type="text/javascript" language="JavaScript">

function addCheck() {
     alert("Please check your email details are correct before submitting")
}
</script>

I would like the function that is called to be defined in the PHP code. I have noticed code like:

$this->tb->add("disk.gif", "javascript:window.parent.view.save()", "Save", "Save Changes");

in PHP code, so I assume you can even call Javascript from PHP ??

All I want to do is define a function in the (same) PHP file, that inserts the row into the MySQL db, and the ONBLUR event to call that function. :slight_smile:

Thanks,

Peter

Not in the way that you are suggesting, but you could use an IFRAME and a little bit of JS to do it.

Javascript is a client-side language, which means that it is run on the users computer and consequently, can be accessed at any time. PHP, however is server-side which means that in most cases, all your PHP is run before your user even sees the page.

Hi Peter
Remember me?
PHP and Javascript cannot interact directly, having said that there are some tricks you can use to achieve a similar effect, such as using Javascript to load an image behind the scenes, where the image is actually a PHP script on the server that spits out an image file. But I don’t think that you need this “live” updating of the database.
This code:


<?php
$this->tb->add("disk.gif", "java script:window.parent.view.save()", "Save", "Save Changes" );
?>

would just be calling the “add” method of a PHP object tb (nested inside another PHP object $this) which probably sets the HREF property of a hyperlink around the image to a Javascript function. It doesn’t actually call the Javascript function, just prints it out.
If you want to stop the user having to click Ok to multiple alerts, why not bundle all of the message together into a single alert box?


<?php ?>
<SCRIPT LANGUAGE="Javascript">
<!--
function checkForm(formobj) {
message = '';
if (!formobj.email.value) {
message += "You must enter a valid email address.\
\
";
}
if (!formobj.address.value) {
message += "You must enter your postal address.\
\
";
}
if (!formobj.theirname.value) {
message += "You must enter your name.\
\
";
}
if (!message) {
return true;
}
else {
alert(message);
return false;
}
}
//-->
</SCRIPT>
<?php ?>

Where checkForm is called using the onSubmit property of the form.

EDIT: That’s these new html boxes - I’m not used to them yet. What hapenned to good old copy and paste??

Well that didn’t come out looking very nice :wink:

Hi,

Would there be a method of using what you have suggested, and keep the code hidden from the client ?

Javascript is a client-side language, which means that it is run on the users computer and consequently, can be accessed at any time. PHP, however is server-side which means that in most cases, all your PHP is run before your user even sees the page.

Yes, but my limited (and naive) understanding is that a server will do nothing unless initiated by a client request. Couldn’t the JS/client-side trigger a PHP file (or Perl script) to be executed on the server, to insert the row into a MySQL table ? In theory, I see no reason why it can’t be done. :slight_smile:

Thanks,

Peter

Hi Paul,

Yes, I remember you. :slight_smile:

Thanks for supplying an example of how to load an image in this manner. Can a similar method be used to use JS/onblur event to “insert a row into a db behind the scenes, …” ??

If you want to stop the user having to click Ok to multiple alerts, why not bundle all of the message together into a single alert box ?

My main objective is to see what the user is doing. There are many hits to a certain url, and users then go to actually place an order, but there are no orders coming through. I know for certain the ordering process works fine, so what is stopping the user from proceeding ?

Thanks,

Peter

My main objective is to see what the user is doing. There are many hits to a certain url, and users then go to actually place an order, but there are no orders coming through. I know for certain the ordering process works fine, so what is stopping the user from proceeding ?

Like a sort-of spy program? I get the picture now.

How is your form set out? How much info do you gather before you ask for their credit card details. If it’s all on the same page, you might find lots of people are curious, and they just take a look (to see the total price, for example), but of course they may not be interested in actually proceeding. This is probably the norm, but maybe you can re-work you order form page to make it more enticing, have some bargains - just like in the aisles of a shopping centre near the checkout. The solution might have more to do with customer psychology than tricky PHP/Javascript coding.

I’m not sure how well it would perform over a dialu-p connection, and you have to consider people’s privacy too. It won’t tell you anything if they just hit the back button of their browser, or close the window. You could try monitoring any links you have on that page.

Hi,

Yes, I 'm trying to track what people are doing, and, as you say, at the same time respect their privacy.

How is your form set out? How much info do you gather before you ask for their credit card details.

Lots of info collected beforehand, see https://anchor.net.au/secure/imeg.com.au/order-secure.php . The prices aren’t actually displayed on the order form, but on a seperate form, see link at the left hand side menu. Possibly not seeing the prices on the order form stops people from proceeding, but the main/home page has the links to products and prices. For most people, you check out, in this sequence, products -> prices -> order

… maybe you can re-work you order form page to make it more enticing

One thing I think is messy is the ‘product type’ drop-down box, it’s like spaghetti. When a client checks out the catalogue, prices or products, it is all arranged neatly, defined under product categories.

The MySQL db never had product category, but I’m in the process of adding it, so that the drop-down box appears sorted in that sequence, like:

<select size=1>
  <optgroup label="Light Sticks">
    <option>4" Lightstick [BULK]</option>
    <option>6" Lightstick [BULK]</option>
  </optgroup>
  <optgroup label="Tri-Colour necklaces">
    <option>22" Tricolour necklace</option>
  </optgroup>
  <optgroup label="Glow Rings">
    <option>6" Glowring</option>
    <option>8" Glowring</option>
  </optgroup>
</select>

Maybe prices should appear somewhere on the order form, that could be stopping people from proceeding (although a ‘confirm’ page appears before the order is submitted.) ?

You could try monitoring any links you have on that page.

I have access to the all the hits, etc, and other stats, but I wouldn’t know how to determine the ‘path’ people are taking, other than by IP address/date/time.

Thanks,

Peter

Just a quick idea if you don’t want to mess with JS: Flash would be perfect for this, nice server-side integration, easy to implement and the form shouldn’t take up too much space either.

Hi,

Well, after a lot of help from different people, I was able to do it. See
Is the ONBLUR event limited to Javascript ?

Peter

Interesting header, thanks for posting that.