PHP and Javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wso
Forum Newbie
Posts: 2
Joined: Tue Aug 26, 2003 9:00 pm

PHP and Javascript

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
wso
Forum Newbie
Posts: 2
Joined: Tue Aug 26, 2003 9:00 pm

PHP and Javascript

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