500 Internal Server Error when submitting form via Ajax [duplicate]

41
September 03, 2019, at 9:00 PM

This question already has an answer here:

  • How can I make PHP display the error instead of giving me 500 Internal Server Error [duplicate] 7 answers

I am creating a custom form with the intention of saving the data to a table in a custom database.

I have created the form and use jQuery to collate the relevant info, Ajax to activate the PHP to then send the info to the database table.

I am currently getting a 500 internal server error though and am not sure how to tackle this.

The code I have used

HTML Form:

<form id="dataForm" name="dataform" method="POST" action="">
    <span>
    <label>Job Date:</label>
    <input id="adddate" class="add-date" type="date" placeholder="Placeholder Text" name="adddate">
</span>
<br>
<span>
    <label>Customer/Company Name:</label>
    <select id="addcompany" name="addcompany" class="limitedNumbChosen add-company" multiple="false">
        <option value="1">company1</option>
    <option value="2">company2</option>
    </select>
</span>
<br>
<span>
    <label>Contact Name:</label>
    <input id="addcontact" class="add-contact" type="text" placeholder="John Doe" name="addcontact">
</span>
<br>
<span>  
    <label>A:</label>
    <input id="addfrom" class="add-from" type="text" placeholder="Postcode from" name="addfrom">
</span>
<br>
<span>
    <label>B:</label>
    <input id="addto" class="add-to" type="text" placeholder="Postcode to" name="addto">
</span>
<br>
<span>
    <label>Subbie Name:</label>
    <select id="addsubbie" name="addsubbie" class="limitedNumbSelect2 add-driver" multiple="true">
        <option value="1">Subbie</option>
        </select>
</span>
<br>
<span>
    <label>Income (£):</label>
    <input id="addincome" class="add-income" type="number" placeholder="£120" name="addincome">
</span>
<br>
    <span>
    <label>Driver Payment (£):</label>
    <input id="addpayment" class="add-payment" type="number" placeholder="£100" name="addpayment">
</span>
<br>
<span>
    <button id="addsubmit" type="submit" value="submit">Submit</button>
</span>
</form>

functions.php (register Ajax)

function eazy_add_scripts() {
    wp_register_script('add-job', get_stylesheet_directory_uri() . '/addjob.js', array('jquery'),'1.1', true);
    wp_enqueue_script('add-job');
    wp_localize_script( 'add-job', 'addjob',
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'eazy_add_scripts' );  

jQuery/Ajax

jQuery(document).ready(function () {
    $("#addsubmit").click(function(e){
        e.preventDefault();
        e.stopPropagation();
        var dataForm = jQuery('#dataForm').serialize();
        jQuery.ajax({
            type: "POST",
            url:  addjob.ajax_url + '?action=so56917978_upload',
            data: dataForm,
            dataType:"json", 
            success:function(data){
                alert("Job Added");
            }
        });
        return false;
    });
});

functions.php - send to database

add_action( 'wp_ajax_so56917978_upload', 'so56917978_upload_callback' );
add_action( 'wp_ajax_nopriv_so56917978_upload', 'so56917978_upload_callback' );
function so56917978_upload_callback() {  
    if(isset($_POST['adddate'], $_POST['addcontact'], $_POST['addfrom'], $_POST['addto'], $_POST['addincome'], $_POST['adddate'], $_POST['addsubbie'], $POST['addcustomer']));{
    //Register variables
    $adddate = $_POST['adddate'];
    $addcontact = $_POST['addcontact'];
    $addfrom = $_POST['addfrom'];
    $addto = $_POST['addto'];
    $addincome = $_POST['addincome'];
    $addpayment = $_POST['adddate'];
    $addsubbie = $_POST['addsubbie'];
    $addclient = $POST['addcustomer'];
    //connect with Database
    $host_name = 'xxx.hosting-data.io';
    $database = 'xxx';
    $user_name = 'xxx';
    $password = 'xxx';
    $connect = mysql_connect($host_name, $user_name, $password, $database);
    //Send to database
    if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
        $protocol = $_SERVER['SERVER_PROTOCOL'];
        if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
            $protocol = 'HTTP/1.0';
        }
        header('Allow: POST');
        header("$protocol 405 Method Not Allowed");
        header('Content-Type: text/plain');
        exit;
    }else{
        if($name != NULL && $email != NULL && $phone != NULL && $state != NULL ){
            // Database connection
            $conn = mysqli_connect("localhost","database_user","database_password","database");
            if(!$conn) {
            die('Problem in database connection: ' . mysql_error());
            }
            // Data insertion into database
            $query = "INSERT INTO `dbxxx`.`add_job` (adddate, addcontact, addfrom, addto, addincome, addpayment, addsubbie, addcompany) VALUES ('$adddate', '$addcontact', '$addfrom', '$addto', '$addincome', '$addpayment', '$addsubbie', '$addclient')";
            mysqli_query($conn, $query);
            $output = array(
            "msg" => "success",
            );
        }else{
            $output = array(
            "msg" => "error",
            );
        }
        echo json_encode($output);
        };
    };
};

I dont think the last part of code is having any effect as yet because the 500 error seems to take effect before it gets called but any help figuring out the 500 error would be great,.

Answer 1

Use die() at the end of your function(s). This should resolve your issue.

READ ALSO
How to call a function when all elements (called by ajax) have been loaded?

How to call a function when all elements (called by ajax) have been loaded?

I want to call a function when all the elements in the div called by ajax are loadedJquery:

50
Html scripts in react with div id not working

Html scripts in react with div id not working

I'm actually working on a React project with voip business and there's a tool that I need to include on my Main component, but that tool is made for html pages

42
Why Javascript event running multiple times per click?

Why Javascript event running multiple times per click?

I wrote a js-class which handles the Ajax-calls when a user clicks on a buttonSo the user can simply click on open/close and can open/close a entity in my database

56