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.
on forum submit event, you can call the function of javascript, name that function validations, and put basic validations there like user should enter his name, should not leave empty the email box, etc.. and if validation goes well, then form will be submitted to php page, u will set the php page on form action event. and php page will receive form data, php coding will connect to database and with insert sql query will insert the data into database table.
Okay. But can I make the PHP script be on the same page so when PHP validates it, the user doesn't lose all her imput if it returns false?
you have to handle population the fields in PHP so the user doesn't lose their input. this is the standard way.
yes you can do it . make the form action to the same script and in the input field's value attribute , instead of <input type="text" name="foo" value=""> Code (markup): change it to <input type="text" name="foo" value="<?php echo $_POST['foo']; ?>"> Code (markup): Hope this helps!
<input type="text" name="foo" value="<?php echo $_POST['foo']; ?>"> Code (markup): Just wondering, is it safe to echo user POST data straight back out like that, with no verification. I can't THINK of a way to exploit that for some nefarious purpose, but that doesn't mean someone else can't! Worth validating in PHP too in some way.
You can add the onSubmit event to the forum to run the validation function when the form in submitted. Just add onSubmit where you declare the start form tag and put the javascript function in it. If you want to validate each field individually then you can place a onChange event on input text fields to validate when a user changes the text.
>'User submits form' --> 'javascript validates form' --> 'PHP validates and sends form values to database' validate with php first. if($_POST['formvar'] != $towhatyouwant){ //go to error page } then setup the javascript to just stop the form from posting if you dont like input data. Just validating with javascript is not safe because users can bypass it.
Yes, you didn't say what the validation was for, but obviously you do the same (and other) validation checks with PHP afterwards too - the JS validation is just to save the user having to refresh your page, saves time on your server etc. but obviously if someone types in some bit of MySQL code into your email field you obviously want PHP to catch that serverside!
yes, you can work on single php file instead of two files one html and other php. form will submit on the same page and you can keep the values like this; <input name='fname' type='text' value='<?php echo $_POST['fname']?>'> Code (markup):