I am take over a website and needed to rebuilt from the current sources code, I didn't know about which framework it was use and there is no documentations at all. So far I am facing an issue on the contact form, this is pretty simple for me if not wrapped by a framework (I'm not familiar in framework and OO), I tried to use my own way to built a form and jquery ajax for php request. I end up failed in php side, in firebug's console log, it came out error with 500 Internal Server Error as shown on the screenshot:

enter image description here

the form is just simple like:

<form id="ContactForm1" method="post">
    <input type="text" name="name" class="form-input" placeholder="Name (required)" autofocus  />
    <span class="error" id="name_error"></span>
    <input type="text" name="email" class="form-input" placeholder="Email (required)"  />
    <span class="error" id="email_error"></span>
    <input type="text" name="phone" class="form-input" placeholder="Phone (required)"  />
    <span class="error" id="phone_error"></span>
    <textarea name="message" class="form-input"  placeholder="Message (required)" ></textarea>
    <span class="error" id="msg_error"></span>
    <input class="form-btn" id="form-btn" type="button" value="Submit" />
</form>

Ajax:

$(document).ready(function() {

    $("#alert").hide();

    $('#form-btn').click(function(){
        var params = $('#ContactForm1').serialize();
        //alert(params);
        $.ajax({
            url: 'inc/post_form.php',
            type: 'POST',
            data: params,
            dataType: 'json',
            success: function(response){

                if(response.success == 'success'){

                    window.location.replace(window.location);
                    $('#alert').html('Thank you for contacting us. We will get back to you at soonest.').show();

                }else{
                    $('[id$="_error"]').html('');

                    $.each(response.error, function(key, value){
                        if(value){
                            $('#' + key + '_error').html(value);
                        }
                    });
                }
            },
            error: function(){
                console.log(arguments);
            }
        });

    });


});

both form and js are placed in views directory, the post_form.php is under the website root folder domain.com/inc/post_form.php, I'm not sure whether a MVC is not support on call the php file directly while the form is called.

post_form.php

<?php
header('Content-Type: application/json');
include('functions.php');

$name = $mysqli->real_escape_string(ucwords(strtolower($_POST['name'])));
$email = $mysqli->real_escape_string(strtolower(trim($_POST['email'])));
$phone = $mysqli->real_escape_string(trim($_POST['phone']));
$the_msg = $mysqli->real_escape_string(nl2br(ucfirst($_POST['message'])));

$func = new Functions();
$message = array();

if(empty($name) || strlen($name) < 2){
    $message['error']['lname'] = 'Last name is required';
}
if(!$func->check_email_address($email)){
    $message['error']['semail']  = 'Invalid email address';
}
if(empty($phone) || strlen($phone) < 10){
    $message['error']['phone'] = 'Phone no. is required';
}
if(empty($the_msg)){
    $message['error']['message'] = 'Your message is too short';
}


if(!isset($message['error'])){

    $findChar = array('\r\n');

    $to = 'info@domain.com';
    $subject = 'You Have New Enquiry';
    $msg = '<html>
                <body>
                    <p>
                        <table cellpadding="3" cellspacing="0" style="width:70%;">
                          <tr>
                            <td width="30%" valign="top"><b>Client\'s Name:</b></td>
                            <td width="70%" valign="top">'.$name.'</td>
                          </tr>
                          <tr>
                            <td valign="top"><b>Email:</b></td>
                            <td valign="top">'.$email.'</td>
                          </tr>
                          <tr>
                            <td valign="top"><b>Phone:</b></td>
                            <td valign="top">'.$phone.'</td>
                          </tr>
                          <tr>
                            <td valign="top"><b>Message:</b></td>
                            <td valign="top">'.str_replace($findChar, '', $the_msg).'</td>
                          </tr>
                        </table>
                    </p>

                </body>
            </html>';
    $from = $email;
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $headers .= 'From: <'.$from.'>' . "\r\n";

    if(mail($to, $subject, $msg, $headers)){
        $message['success'] = 'success';
    }

}

echo json_encode($message);

?>

Can anyone share me a workaround on this? whether using ajax request or set form action="post_form.php" as long it can works, or any alternate way?

Related posts

Recent Viewed