0
Here I am again with my silly questions but they give me the biggest headache.. Come on, I have a script that takes the data that the user typed and sends to the server, works perfectly, however I wanted to pass it by ajax for several reasons but I could not because I never worked with ajax and now q I need to be on hand.. If anyone can help me to convert this script so that it provides an answer to an ajax request I thank you.. Good evening!
Just follow the code. (Obs: this variable generates like I hadn’t finished yet, it would pick up on the like table the amount of like q that post received and it would identify the post by its id... )
<?php
$status = 1;
if( isset( $_POST['publicar'] ) ){
date_default_timezone_set('America/Sao_Paulo');
$form['data'] = date('Y-m-d H:i:s');
$form['conteudo'] = str_replace( '\r\n', "\n", DBEscape( trim( $_POST['conteudo'] ) ));
$form['status'] = DBEscape( strip_tags( trim( $status ) ) );
if( empty( $form['conteudo'] ) )
echo'';
else {
$dbCheck = DBRead( 'posts', "WHERE conteudo = '". $form['conteudo'] ."'" );
if( $dbCheck )
echo '<script>alert("Desculpe, mas já contaram esse segredo!");</script>';
else {
if( DBCreate( 'posts', $form ) ){
// Vai procurar no DB o post e em seguida pegar seu id para criar uma
// tupla na tabela de likes
$IdCheck = DBRead( 'posts', "WHERE conteudo = '". $form['conteudo'] ."'", 'id');
$UserIdCheck = DBRead( 'usuarios', "WHERE email = '". $_SESSION['email'] ."'", 'id');
$id = (string)$IdCheck[0]['id'];
//echo $id."</br>";
$user = (string)$UserIdCheck[0]['id'];
//echo $user;
$like = array(
'id_post'=> $id,
'n_like' => 0,
'id_user'=> $user
);
$geralike = DBCreate('likes', $like);
if($geralike)
echo '<script>console.log("Likes ativos do post "'.$form['conteudo'].');</script>';
else
echo '<script>console.log("Acho que ia mas num foi ¯\_(ツ)_/¯");</script>';
// este pedaço abaixo foi uma fraca tentativa de retornar para o feed o post recem feito, bom eu não tinha terminado ent ficou pela metade.
//Resposta apost concluir o post
//$lastpost = DBRead( 'posts', "WHERE id = '". $id ."'", 'conteudo');
//$postcont = (string)$lastpost[0]['conteudo'];
//$post = post($postcont);
//echo "<script>$('div.overview').append(".$post.");</script>";
//echo '<script>document.getElementById("myP").style.visibility = "visible";</script>';
//echo '<script>location.reload();</script>';
}
else
echo '<script>alert("Desculpe, ocorreu um erro...");</script>';
}
}
}
?>
Note 2: It checks if there is already an equal post comparing the content because there is no title.