Insert information into bank with AJAX

Asked

Viewed 853 times

0

I need to enter information in the database, but I need the page not update.

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

Like I’m doing:

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 falls in the message "An error occurred while entering your record!"

If any information is missing for you to help me, please let me know. Thank you!

@Edit

I will post the most complete code to make it easier to understand, I suspect my mistake may not necessarily be in js

How was the JS function: Function setEpisodeComplete(episodeID, media) {

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 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 var_dump the sql command to see how it was, and found that it came like this:

SELECT * FROM ayzac_season WHERE ayzac_midia_id ='12'

That’s probably why it’s not working

1 answer

1


Apparently the request to your script has been made successfully, but it is returning a different result from 1. Change the url and pass the data through the attribute data and use the method POST to enter data.

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

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 just see the result in the Chrome console tab CTRL+F12. I believe you have some error in PHP, can a syntax error or with the SQL itself that is running.

And don’t forget to change the PHP script to receive the data with POST using the $_POST global variable or the function filter_input, which I recommend!

Browser other questions tagged

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