Help needed in sending a form to javascript and then to PHP

Hi friends

I’m trying to create a new user function so users can enter data and become a member of my site but it won’t work. I have finished coding all the javascript and PHP script validation and sending to server code but I don’t know how to join them like this:

‘User submits form’ –> ‘javascript validates form’ –> ‘PHP validates and sends form values to database’

All I need to know is how to connect the different functions. How would I do this?

Thanks.

First of all write a JavaScript function that returns true if the JavaScript validation has passed, False otherwise.

Then, in the HTML:

<form action="yourPHPFile.php" method="post" onsubmit="return yourFunction();">

If you return false from yourFunction, the form will not submit to your PHP validation, otherwise it will.

You then treat it like every other form in your PHP validation. Just make sure you are double checking everything in PHP as you do in JavaScript, as JavaScript can be bypassed.

Your form and javascript will look like this:


<html>
<head>
	<title>test</title>
	<script type="text/javascript">
        funciton validate(frm){
            if(frm.yourname.value == ''){
                alert('Enter your name.');
                return false;
            }
            return true;
        }
	</script>
</head>
<body>
    <form name="frm1" method="post" action="process.php" onsubmit="return validate(this);">
        Your name: <input name="yourname" type="text" id="yourname" />
        <br /><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
    </form>
</body>
</html>

And your server script will look like this:


$yourname = $_POST['yourname'];
if(!empty($yourname)){
    echo 'You missed to enter your name';
}
else{
    // connect to database
    // select database
    // prepare a insert statement
    // execute the insert statement with mysql_query function.
}

Make a form that submits to the php script.
No javascript yet.
Test and refine until complete.

Create a javascript function that will return true or false depending on whether or not the form’s fields are valid.
Use the submit event of the form element to call the function as the event handler. Returning false from the event handler function will prevent the form from submitting.

edit-I’m too slow :frowning: