-1

I understand that they are different, and they each have their own special things that they do Client and Server side. But let's say you are validating a form. You use JavaScript to validate the form, then if there are no errors, you run PHP to insert a record into database. How would you do this? Is there any way to run PHP in JavaScript or call on a PHP method together?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Hardik Patil
  • 515
  • 1
  • 4
  • 16

1 Answers1

0

You can use both JavaScript and PHP validation together (in fact, you should). Probably the easiest way to do this is add a hidden submit button on your page. The visible submit button should be of type button and should trigger your JavaScript validation. Then, if the input passes your validation rules, trigger a click on the hidden button to submit the form to the server.

It is worth noting that if you do follow this route, you must also remember to catch submissions that are done using the Enter key (since this key press automatically triggers form submissions) and pass them through your own custom function.

Of course you could also disable the submit button entirely until the form passes the JavaScript validation. With this method, you can check each field as the value within it changes and then only enable the submit button when the form is fully valid.

Without examples, it is difficult to come up with a solution to your specific use case.

JoeWemyss
  • 607
  • 1
  • 10
  • 28