Can I only check with jQuery or do I need PHP?

I understand the difference between the two languages, but when it comes to form validation. Is jQuery enough?

Can I validate my form in jQuery / JavaScript and then make an AJAX call to submit the completed form to an email address? Is it even possible to send form data to an email address using JavaScript?

Or is it somewhat safer to do everything in PHP?

I ask this because using PHP is what I'm used to, however, I recently started working with more advanced JavaScript and jQuery.

+4
source share
7 answers

- jQuery , - .

, .

, ( , , )

+8

JQuery. PHP ( ), , JavaScript. , , , .

, (JQuery/JavaScript) . JQuery .

AJAX, :

PHP

//if there is an error:

header('HTTP/1.1 500 Internal Server Error');
header('Content-Type: application/json; charset=UTF-8');
die(json_encode(array('message' => 'ERROR', 'code' => 1337)));

//if there is not an error:
header('Content-Type: application/json');
echo json_encode(array("message"=>"All good here!"));

JQuery

//this is on submit, but you could do other events
$(document).ready(function(){
    $("form").submit(function(e)
    {
    e.preventDefault();
    $.ajax({
        type: "post",
        url: "path/to/your.php",
        data: $('form').serialize(),
    success: function(data)
    {
            alert(data.message);
        },
        error:  function(xhr, status, error) {
          var err = eval("(" + xhr.responseText + ")");
          alert(err.Message);
        },
    dataType: "json"});
    }});
});

. , , .

+5

, - , (PHP), .

jQuery/JavaScript: . , (, ), .

+4

: jQuery , . , JavaScript , PHP. , - , , - , SQL- ...

PHP, jQuery , , , , ...

, , , JavaScript.

+3

jQuery/JavaScript - , , . SMTP-, , , PHP .

, :)

+1

, - , IT - , , , - I.e. , !

JavaScript , , , , ! , , JavaScript?

+1

well.. , , .

Javascript/jquery - , PHP - . ? Javascript , , . PHP , . , , PHP ( ) . , , .

... jquery/JS , , ... JS . "" , .

In short, PHP is always ... To improve the experience (and highly recommend), also use JS :)

0
source

Source: https://habr.com/ru/post/1542393/


All Articles