PDA

View Full Version : Validating with javascript client side and subsequent action using server side script


raghavan20
06-05-2005, 12:58 PM
Hi all,

I have got a form which has a few fields and I am validating the form using the client side javascript and then if all the fields are valid I want to make some subsequent action using server side script vbscript of php.

The actual form is 'members registration' and I validate the fields using javascript functions and then I want to create an user using server side script, if the fields are valid.

How can I pass control from client side script to server side script function?

I am attaching the code for you.

Thank you very much for your efforts

_Aerospace_Eng_
06-05-2005, 01:03 PM
So which part are you having trouble with the client side or the server side? I'm guessing the server side. Are you wanting to check if a field has info in it onsubmit? If it doesn't don't submit? Is that basically what you want? Just looking at your script you have valid=false; that should just be return false; that way if the field is blank or in correct the form won't submit.

raghavan20
06-05-2005, 01:58 PM
I think I didnot explain the problem clearly.

Anyway, I have checked all the fields using javascript(client side) and after finding that all the fields are valid I wanted to transfer control to the server side script(either vbscript or php) which creates a member using the field values.

so you can summarize it as, I want to validate using client-side javascript but create users or deal with the database using server side script

What I have given above is just the client side validation code. if I want to call a function:
for ex:
<?php
function insertIntoDb(){
str = "insert into Members_tbl values (something)";
mysql_query(str);
}
?>

How can I give a call to this function from the client side javascript function, in this case validateForm()?

IKLOP
06-05-2005, 03:24 PM
You can't call a php function from javascript. What you need to do is give the action of the form a php page:
<form ... action="somepage.php">
Then at the end of the validateForm function return the value of valid by adding return valid; If false is returned, the form won't be submitted, but if true is returned then the action page (somepage.php) will be called and the data sent to it. In that page, you would then call whatever function you need to check the info and add it to a database or do whatever you need to do with the data.

raghavan20
06-05-2005, 05:50 PM
Thank you mate, now the return of the 'valid' variable from the validateFunction determines whether the form should be submitted or not. Thanks for understanding the problem and helping me out.

Is this the conventional and the standard way to do it? Do most programmers do the validation using client side script(preferably javascript) and return the value to the form tag and then branch to server side code?

The reason I am asking you is, I have developed two projects using Asp and I validated everything using server side vbScript because I felt it easier to handle since I knew VB. I am now doing a project with Php so I am thinking of adopting best practices.

Do advise me on this regard.

I also have got another problem with setting cookies during login in Php and I have posted the problem in the server side php forum, hope you could help me on that issue as well.

Thank you again.

IKLOP
06-05-2005, 06:34 PM
That is the way it is usually done when doing javascript validation, however, without serverside validation it is useless.

Javascript can't be used at all in security. It is very easy to get around javascript validation if someone was trying to hack you or something, so the data must be checked again on serverside anyway.

Really, its only use is to prevent the user from having to reload the page to find out if they messed something up, which to me doesn't outweight to work of writing a javascritp validator. It might be useful in a few situations, but I find it more work than it's work.