使用ajax将mysql结果返回给php(Return a mysql result to php using ajax)

编程入门 行业动态 更新时间:2024-10-25 02:29:06
使用ajax将mysql结果返回给php(Return a mysql result to php using ajax)

我一直在处理这个问题,但我在这里看不到解决方案。 我正在创建一个简单的列表应用程序,每当用户创建一个新项目时,它应该保存到数据库中。 问题是每个项目都有一个唯一的ID(自动递增),当项目附加到列表时,该ID必须在html代码上(其他函数需要使用它,所以当它上传到数据库时,然后应该选择ID并再次发送到原始的html文件,但我不知道该怎么做(事实上,我不知道)。

这是我的php / html代码:

while($row = mysql_fetch_array($selectitems) ){ echo'<html> <li class="i­tem" data-id="'; echo $row['IDitem']; echo '"> <div class="draggertab"><img src="imatges/botofletxa.jpg" width="30" height="30"></div> <div class="deletetab"><img src="imatges/botocreu.jpg" width="30" height="30"></div> <span class="item">'; echo $row['Text']; echo'</span> </li> </html>'; }

javascript:

ajax.onreadystatechange=function() { //la función responseText tiene todos los datos pedidos al servidor if (ajax.readyState==4) { //mostrar resultados en esta capa AppendItem(); //llamar a funcion para limpiar los inputs LimpiarCampos(); } } ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //enviando los valores a registro.php para que inserte los datos ajax.send("nombre="+nom+"&IDllista="+llista); }

和PHP:

$con = mysql_connect($bd_host, $bd_usuario, $bd_password); mysql_select_db($bd_base, $con); //variables POST $nom=$_POST['nombre']; $IDllista = $_POST['IDllista']; //registra los datos del empleados $sql="INSERT INTO items (Text, IDllista) VALUES ('$nom', '$IDllista')"; mysql_query($sql,$con) or die('Error. '.mysql_error());

还有更多的代码,但我发布了我认为最重要的部分。 它现在完美运行,我只需要发送ID。

非常感谢你!!

I've been dealing with that problem for a while, but I can't see the solution here. I am making a simple list application, and anytime the user creates a new item it should be saved to the database. The issue is that every item has a unique ID (auto-incremented), and that ID has to be on the html code when the item is appended to the list (other functions need to use it, so when it's uploaded to the database, then the ID should be selected and sended again to the original html file, but I'm not sure how to do that (in fact, I've got no idea).

Here's my php/html code:

while($row = mysql_fetch_array($selectitems) ){ echo'<html> <li class="i­tem" data-id="'; echo $row['IDitem']; echo '"> <div class="draggertab"><img src="imatges/botofletxa.jpg" width="30" height="30"></div> <div class="deletetab"><img src="imatges/botocreu.jpg" width="30" height="30"></div> <span class="item">'; echo $row['Text']; echo'</span> </li> </html>'; }

The javascript:

ajax.onreadystatechange=function() { //la función responseText tiene todos los datos pedidos al servidor if (ajax.readyState==4) { //mostrar resultados en esta capa AppendItem(); //llamar a funcion para limpiar los inputs LimpiarCampos(); } } ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //enviando los valores a registro.php para que inserte los datos ajax.send("nombre="+nom+"&IDllista="+llista); }

And the PHP:

$con = mysql_connect($bd_host, $bd_usuario, $bd_password); mysql_select_db($bd_base, $con); //variables POST $nom=$_POST['nombre']; $IDllista = $_POST['IDllista']; //registra los datos del empleados $sql="INSERT INTO items (Text, IDllista) VALUES ('$nom', '$IDllista')"; mysql_query($sql,$con) or die('Error. '.mysql_error());

There is also much more code, but I posted what I think is the most important part. It works perfectly now, I just need to send the ID.

Thank you very much!!

最满意答案

您应该避免使用mysql_ *并使用PDO。 自PHP5.5起,Mysql_函数被删除

无论如何,您可以检索生成的最后一个id: mysql_insert_id()并将其返回到您的ajax处理程序,然后您可以使用var id = xmlhttp.responseText;获取它var id = xmlhttp.responseText;

PHP:

$sql="INSERT INTO items (Text, IDllista) VALUES ('$nom', '$IDllista')"; mysql_query($sql,$con) or die('Error. '.mysql_error()); echo mysql_insert_id(); // return the ID

JS:

ajax.onreadystatechange=function() { if (ajax.readyState==4) { .... var id = ajax.responseText; // get it } }

You should avoid using mysql_* and use PDO instead. Mysql_ functions are deprated since PHP5.5

Anyway you could retrieve the last id generated with : mysql_insert_id() and return it to your ajax handler, then you can get it with var id = xmlhttp.responseText;

PHP :

$sql="INSERT INTO items (Text, IDllista) VALUES ('$nom', '$IDllista')"; mysql_query($sql,$con) or die('Error. '.mysql_error()); echo mysql_insert_id(); // return the ID

JS :

ajax.onreadystatechange=function() { if (ajax.readyState==4) { .... var id = ajax.responseText; // get it } }

更多推荐

本文发布于:2023-08-02 15:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysql   ajax   php   result   Return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!