Enter information in the bank with AJAX

0

I need to insert information into the database, but I need the page not to update.

The problem is that I do not know anything about AJAX and even looking for the codes on the net, I could not solve

How do I do:

function setEpisodeComplete(episodeID, midiaType){
var url = "../control/ayzac_control_setComplete.php?id=" + episodeID + "&tp=" + midiaType;
$.ajax({
    url: url,
    type: 'GET',
    cache: false,
    error: function(){
        alert('Erro: Inserir Registo!!');
    },
    success: function(result)
    { 
        if($.trim(result) == '1')
        {
            alert("O seu registo foi inserido com sucesso!");
        }
        else{
            alert("Ocorreu um erro ao inserir o seu registo!");
        }

    }
});}

It always drops in the message "There was an error inserting your record!"

If there was any information missing for you guys to help me, let me know. Thanks!

@edit

I'll post the most complete code to make it easier to understand, I suspect my error might not necessarily be in js

How did the JS function:     function setEpisodeComplete (episodeID, midiaType) {

var url = "../control/ayzac_control_setComplete.php";
$.ajax({
    url: url,
    type: 'POST',
    dataType: 'html',
    cache:false,
    data: {"id": episodeID, "tp": midiaType},

    error: function () {
        alert('Erro: Inserir Registo!!');
    }
    ,
    success: function (result) {
        console.log(result);
        if ($.trim(result) == '1')
        {
            alert("O seu registo foi inserido com sucesso!");
        } else {
            alert("Ocorreu um erro ao inserir o seu registo!");
        }
    }
}
);}

The file that JS calls ayzac_control_setComplete.pho:

require ('../models/ayzac_class_midia.php');
$objUser = new userMidia();
session_start();
$userId =   $_SESSION['idSession'];
$midiaId = filter_input(INPUT_POST,'id');
$midiaType = filter_input(INPUT_POST,'tp');
$midiaId = $objUser->removeChar($midiaId);
$midiaType = $objUser->removeChar($midiaType);
var_dump($midiaType);
$objUser->setComplete($midiaId,$userId,$midiaType);

And finally the code in php:

function setComplete($midiaId, $userID, $midiaType) {
    $connect = new ControllerConnect ();
    $objCon = $connect->controllerConnect();
    $sql = "SELECT * FROM ayzac_midia_status WHERE ayzac_user_id = '" . $userID . "' AND ayzac_midia_id = '" . $midiaId . "'";
    $query = $objCon->executeSQLQuery($sql);
    if ($midiaType == 1) {
        if ($objCon->getSqlNumRows($query) >= 1) {
            $sql = "UPDATE ayzac_midia_status SET ayzac_midia_status = '2' WHERE ayzac_user_id = '" . $userID . "' AND ayzac_midia_id = '" . $midiaId . "'";
        } else {
            $sql = "INSERT INTO ayzac_midia_status (ayzac_user_id,ayzac_midia_id,ayzac_midia_status) VALUES ('" . $userID . "','" . $midiaId . "','2')";
        }
    } else if ($midiaType == 2) {
        $sql = "SELECT * FROM ayzac_season WHERE ayzac_midia_id ='" . $midiaId . "'";
        $season = $objCon->executeSQLFetchAssoc($sql);
        $i = 0;
        while (isset($season [$i] ['ayzac_season_id'])) {
            $sql2 = "SELECT ayzac_episode_id FROM ayzac_episode WHERE ayzac_season_id = '" . $season [$i] ['ayzac_season_id'] . "'";
            $episodes = $objCon->executeSQLFetchArray2($sql2);
            $j = 0;
            while (isset($episodes [$j] ['ayzac_episode_id'])) {
                $this->setEpisodeComplete($userID, $episodes [$j] ['ayzac_episode_id']);
                $j ++;
            }
            $i ++;
        }

        if ($objCon->getSqlNumRows($query) >= 1) {
            $sql = "UPDATE ayzac_midia_status SET ayzac_midia_status = '2' WHERE ayzac_user_id = '" . $userID . "' AND ayzac_midia_id = '" . $midiaId . "'";
        } else {
            $sql = "INSERT INTO ayzac_midia_status (ayzac_user_id,ayzac_midia_id,ayzac_midia_status) VALUES ('" . $userID . "','" . $midiaId . "','2')";
        }
    } else if ($midiaType == 3) {
        if ($objCon->getSqlNumRows($query) >= 1) {
            $sql = "UPDATE ayzac_midia_status SET ayzac_midia_status = '2' WHERE ayzac_user_id = '" . $userID . "' AND ayzac_midia_id = '" . $midiaId . "'";
        } else {
            $sql = "INSERT INTO ayzac_midia_status (ayzac_user_id,ayzac_midia_id,ayzac_midia_status) VALUES ('" . $userID . "','" . $midiaId . "','2')";
        }
    }
    $objCon->executeSQLQuery($sql);
    echo 1;
}

I made a var_dump in the sql command to see how it was, and found it came like this:

SELECT * FROM ayzac_season WHERE ayzac_midia_id ='12'

That's probably why it's not working

    
asked by anonymous 24.10.2017 / 22:24

1 answer

1

The request to your script was apparently successful, but it is returning a result other than 1 . Change the url and pass the data by the data attribute and use the POST method to enter data.

You have to keep track of what your PHP script displays, the best way to do this is to add the dataType: "html" parameter in the $ .ajax function and display the result of the success function on the console.

Try this:

function setEpisodeComplete(episodeID, midiaType){
    var url = "../control/ayzac_control_setComplete.php";
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'html',
        data: {"id": episodeID, "tp"= midiaType}
        error: function(){
            alert('Erro: Inserir Registo!!');
        },
        success: function(result) { 
            console.log(result);
            if($.trim(result) == '1')
            {
                alert("O seu registo foi inserido com sucesso!");
            } else {
                alert("Ocorreu um erro ao inserir o seu registo!");
            }
        }
    });
}

Then you just see the result in the chrome console CTRL+F12 tab. I believe that you have some error in PHP, it may be a syntax error or with the SQL itself that is running.

And do not forget to change the PHP script to receive data with POST using the $ _POST global variable or the filter_input , which I recommend!

    
24.10.2017 / 22:43