简体   繁体   中英

Using Ajax, JavaScript and HTML to post some data

Hello I´m very frustated because I can´t connect to my database to retrieve some files.

I have my normal html code (index.html) which adds a javascript:

<script src="js/connection.js"></script> 

The javascript (connection.js) looks something like this, which detects from a grid of elements the selected element and gets it´s text:

var texto="";

function getData($dia){

//Variable $dia is set up correctly, no problem

var dia=$dia;
/* Send the data using post*/
$.ajax({
    url: "php/setup.php",
    type: "post",
    data: {'fecha':dia},
    contentType: "application/json",
    datatype: "json",
    success: function(){
        alert("Exito");
    },
    error:function(){
        alert("failure");
    }
});
}

//Get the desired text upon click on the grid item
$(document).ready(function(){    
$(".grid__item").click(function(){
    $texto=$(this).html().substring(($(this).html().indexOf(">"),($(this).html().indexOf(">")+1)),$(this).html().indexOf("</h2>"));
    getData($texto); 
});
});

Finally using Ajax I pass the variable 'fecha', the problem is that I think it´s not making a proper connection with my php file since nothing is printing (I have a method which prints to console)

I set the post method like this ( PHP file starts here ):

debug_to_console("Print Something");
$fecha = mysql_real_escape_string($_POST['fecha']);
getPageData($fecha);

Which calls this method:

function getPageData($dia){

$sql = ("SELECT * FROM Comentarios WHERE dia='$dia'");
$result = mysqli_query(connectToDb(),$sql);
$num_rows = mysqli_num_rows($result);
$html="";
$boolean=true;
if($num_rows>0) {
    while($row = $result->fetch_assoc()) {            
        if($boolean==true){
            $html.='<div class="gray"><div class="comentario">'.$row["comment"].'</div><div class="timestamp">'.$row["dia"].'</div></div>'; 
            $boolean=false;
        }else{
            $html.='<div class="white"><div class="comentario">'.$row["comment"].'</div><div class="timestamp">'.$row["dia"].'</div></div>'; 
            $boolean=true;
        }
    }
    echo json_encode(array('html'=>($html.'<br>'.'<div class="fondo_gen"> div></div></div>'),'texto'=>$dia));
} else {
    echo json_encode(array('html'=>'<div class="transparent"><div class="nada">No hay comentarios aun :(</div></div>','texto'=>$dia));
}
}

PHP file ends here

I know that it's connecting to the database since a made a "dummy.php" file which connects to the same database and table and adds a record, without problem. I´m not really sure which is the problem, I could really appreciate it if you could help me.

PS:

My folders are setup like this:

  1. index.html
  2. js (folder) a. connection.js
  3. php (folder) a. setup.php

.
Thanks and sorry for my crappy english

Never mind, I got it fixed, I replaced the Ajax part with:

 $.post("php/setup.php",
{
    fecha: dia
},
function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
});

and received the variable using

if (isset($_POST["fecha"])){
   $fecha = $_POST["fecha"];
   getPageData($fecha);
}else{
   echo "Got nothing";
}

Thanks anyway, I appreciate the help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM