Page 1 of 1

PHP and Javascript

Posted: Tue Aug 26, 2003 9:00 pm
by wso
Hi All,

I have a Form for File Records and will save the data into a MySQL database.

I would like to validate the value of a field, say data1, with the data stored in a MySQL database before the end-user continues to complete the form and before the form is submitted.

In the Form:
......
<input onblur="document.MyForm.data1.value=this.value; <?php checkexist(); ?>" type="text" ,,,,,,, >
......

And have a function called checkexist()


<?php
checkexist()
{
$checkdata = $data1;
.....
}
?>

My problem is the php variable $checkdata cannot get the value of the Form's data1.

Please help me, thanks.

Posted: Tue Aug 26, 2003 9:11 pm
by BDKR
PHP is a server side solution. That said,

Code: Select all

<input onblur="document.MyForm.data1.value=this.value; <?php checkexist(); ?>"
isn't going to work as you are expecting the function to execute in the users browers.

Unless there is a rather small set of potential datums that may exist (a set that can be sent to the browser without much overhead), you are best to make a trip back to the server for this.

I hope that made sense.

Cheers,
BDKR

Posted: Tue Aug 26, 2003 10:31 pm
by JAM
As BDKR said, that wont work, unless...

a) You could change the verifying javascript to a function in the <head> of the page. When the page is retrieved you get the values stored in the db, insert it into the head's javascript section and do a

Code: Select all

<input onblur="checkit(this.value)">
(or similiar)
If you have 1000's of values to verify with, this might not be a great idea tho =)

b) More common way, check if values are ok using $_POST after submitting the form.

PHP and Javascript

Posted: Wed Aug 27, 2003 9:43 pm
by wso
Thank you BDKR and JAM,

The form contains more than 40 fields the end-user needs to fill. If, unfortunately, the record, the end-user is filling the form, is notified that "already exist" when the form is completed and submitted, the end-user will be very upset.

So I need to verify the record whether it exists or not once the record reference is filled, I say data1 before.

As both of you suggested that if all records loaded before starting filling the form, as both of you said it is not practical, because the amount of records will be increased more and more.

I need to verify that data value before the form is completed and submitted.

However, someone suggested make use of cookie: set cookie of the value by PHP and read that cookie back by PHP and then to verify with MySQL. But I don't know how. Can you give me help.