[英]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: 我有我正常的HTML代码(index.html),它添加了一个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: javascript(connection.js)看起来像这样,它从元素网格中检测所选元素并获取它的文本:
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) 最后使用Ajax我传递变量'fecha',问题是我认为它没有与我的php文件建立正确的连接,因为没有打印(我有一个打印到控制台的方法)
I set the post method like this ( PHP file starts here ): 我设置了这样的post方法( PHP文件从这里开始 ):
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 PHP文件在这里结束
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. 我知道它连接到数据库,因为它创建了一个“dummy.php”文件,它连接到同一个数据库和表并添加一条记录,没有问题。 I´m not really sure which is the problem, I could really appreciate it if you could help me. 我不确定哪个是问题,如果你能帮助我,我真的很感激。
PS: PS:
My folders are setup like this: 我的文件夹设置如下:
. 。
Thanks and sorry for my crappy english 谢谢,抱歉我蹩脚的英语
Never mind, I got it fixed, I replaced the Ajax part with: 没关系,我修复了,我用以下代码取代了Ajax部分:
$.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 无论如何,谢谢,我感谢你的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.