Sending json to php server using jquery ajax - json error

后端 未结 1 513
逝去的感伤
逝去的感伤 2021-01-25 01:30

I have problem with my form on my webpage. I am trying to use ajax and json to send the form values to php server, but I can\'t make properly json object.

My JS

1条回答
  •  北海茫月
    2021-01-25 01:45

    I was able to get the json object and parse it on php side. Some variable might be named different, partly because some of it is pre-written code. Here are the steps I took.

    Folder Structure

    js/script.js
    php/get_data.php
    index.html
    

    Index.html a basic form.

    script.js upon submit, gather data and set it to an object and send to php via ajax.

    $(function() {
        // process the form
        $('#feedbackform').submit(function(event) {
    
            // get the form data - obj that will POSTED to get_data.php
            var formData = {
                'email'       : $('#email').val(),
                'subject'     : $('#subject').val(),
                'message'     : $('#message').val()
            };
    
            // process the forum
            $.ajax({
                type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
                url         : 'php/get_data.php', // the url where we want to POST
                data        : formData, // our data object
                dataType    : 'json', // what type of data do we expect back from the server
                encode      : true
            })
                // using the done promise callback
                .done(function(data) {
                    // log data to the console so we can see
                if ( ! data.success) {
                    console.log(data);
                } else {
                    console.log(data);
                }
    
            });
    
            // stop the form from submitting the normal way and refreshing the page
            event.preventDefault();
        });
    
    });
    

    get_data.php receives the data from script.js, do some validations, and then send and an e-mail.

    ';
    
        //subject of the  email
        $subject = $info_data[1];
        $result = str_replace(' ', ' ', $info_data[2]);
        $result = nl2br($result);
        $result = wordwrap($result, 70, "\r\n");
    
        //body of the email.
        $message = "";
        $message .= "

    " . $result . "

    "; $message .= "
    "; $message .= ""; $headers = "From: Email, Some " . "\r\n" . 'Reply-To: '. $info_data[0] . "\r\n" ."X-Mailer: PHP/" . phpversion(); $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=UTF-8"; //sent mail: check to see if it was sent. if(mail($to, $subject, $message, $headers)){ $data["went"] = "went"; $data['message'] = 'Success!'; }else{ $data["went"] = "didn't go"; $data['message'] = 'Sorry!'; } // echo the log echo json_encode($data); } ?>

    lot to cover, let me know if you any questions. I'll be happy to answer.

    0 讨论(0)
提交回复
热议问题