Error sending data to php with ajax in wordpress

Asked

Viewed 37 times

0

I have this form:

<select id="mudar_produto">
    <option></option> 
    <option value="#produto_1">Novo Produto Higiene</option> 
</select>
<section class="hide-section" id="produto_1"> 
<form class="form-validate" id="feedback_form">
    <div class="campo">
        <fieldset> 
            <h1>
                <legend>
                    <center>
                        <strong>Produtos de Higiene</strong>
            </center>
        </h1><br> 
        </div>
        <fieldset class="grupo">
    <div class="campo">
            <strong><label for="Nome do Produto">Nome do Produto</label></strong> 
            <input type="text" id="DescricaoProd" name="DescricaoProd" required="" style="width:350px">
        </div>
    <div class="campo"> 
        <strong><label for="Unidade">Unidade</label></strong> 
            <input type="text" id="DescricaoUnid" name="DescricaoUnid" style="width:160px" required="" size="120">
        </div>
        </fieldset>
        <button class="btn btn-success btn_contact" type="button">Registo</button>
</form>
</section> 

I have this ajax to send the data without refreshing the page and clean the inputs:

<script type="text/javascript"> 
$(".btn_contact").click(function () {

                $.ajax({
                    type: "POST",
                    url: "inserir",
                    data: $("#feedback_form").serialize(), // serializes the form's elements.
                    success: function (data)
                    {
                        if ($.trim(data) == 'true') {
                            $("#feedback_form").find('input').val(''); //clear text
                            $(".success_messages").removeClass('hide'); // success message
                        } else {
                            $(".error_message").removeClass('hide'); // error message
                        }
                    }
                });

            });
</script>

On the insert page I have the php code:

<?php
$servername = "xxx.xxx.x.xx"; 
$username = "xxxxxx"; 
$password = "xxxxxxxxx"; 
$dbname = "xxxxxxxx"; 
$conn = new mysqli($servername, $username, $password, $dbname); 
$conn->set_charset('utf8');
$name = isset($_POST["DescricaoProd"]) ? $_POST["DescricaoProd"] : '';
$unid = isset($_POST["DescricaoUnid"]) ? $_POST["DescricaoUnid"] : '';
if (!empty($name)) && (!empty($unid)) { 

    echo 'true';
} else {
    echo 'false';
}    

$sql = "INSERT INTO ProdHigieneteste (DescricaoProd,DescricaoUnid) 
VALUES ('$name','$unid')";

if ($conn->query($sql) === TRUE);

$sql1 = "INSERT INTO StockHigieneteste (DescricaoProd,DescricaoUnid) 
VALUES ('$name','$unid')";

if ($conn->query($sql1) === TRUE);

    //Count total number of rows
    $rowCount = $query->num_rows;

$conn->close(); 

?>

But when I click the log button I get the following error on the console:

POST http://xxx.xxx.x.xx/wordpress/index.php/insert 500 (Internal Server Error)

and does not insert into the database table.

1 answer

1


From what I understand, you have a mistake in a if in your code PHP, as below:

Error code

if (!empty($name)) && (!empty($unid)) { 

    echo 'true';
} else {
    echo 'false';
} 

Correct code:

if(!empty($name) && !empty($unid)){
        echo 'true';
} else {
        echo 'false';
}

A tip, whenever you see this error 500, try to open the error log file of your PHP, there will be described why the error is occurring and you will solve the problem more easily, or enable error logs on the browser’s own screen while developing:

Enter the following parameters directly in the php file

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.