将附加的元素保存到数据库 [英] Saving appended elements to database
问题描述
我用jquery制作了一个非常简单的购物清单,下面是代码:
I've made a very simple shopping list with jquery here is the code:
$(document).ready(function(){
$('.project-btn').click(function(e){
e.preventDefault();
var project = $('.project-val').val();
$('<li></li>').addClass(project).text(project).appendTo('.project-list');
});
});
http://jsfiddle.net/DQxE7/ 一切正常,但是当我重新加载页面时,我添加的所有内容都会消失. 我想保存添加的元素,这样,如果其他人访问了网站,他们可以添加到列表中.
http://jsfiddle.net/DQxE7/ everything works great however when i reload the page everything that I've appended goes away. I want to save the appened element so if someone else goes on the website they can what i have added to the list.
我该如何使用php或mysqli将这类信息保存到数据库中? 我可以将元素另存为字符串,然后通过PHP回调该字符串吗?
How would I save that sort of information to a database using php or mysqli? Can I save an element as a string and then call back that string though PHP?
我已经在Google上查询了,但是我什么也没收到,这就是为什么我在这里问它.
I've looked up on google but I got nothing that's why I am asking it here.
谢谢,我不需要如何做的代码!
I don't need the code of how to do just how to go about it, thanks!
推荐答案
$(document).ready(function(){
$('.project-btn').click(function(e){
e.preventDefault();
var project = $('.project-val').val();
$('<li></li>').addClass(project).text(project).appendTo('.project-list');
var elem={};
elem.str='<li class="'+project+'">'+project+'</li>';
$.ajax({
url: 'path/to/your/phpscript.php',
data: elem,
type: 'POST',
success: function(response)
{
//do whatever you like
}
})//end of ajax call
});
});
phpscript.php
phpscript.php
<?php
define('DB_NAME', 'your schema/database name');
define('DB_USER', 'your db user name');
define('DB_PASSWORD','password');
define('DB_HOST', 'localhost');
$link=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$link)
{
die('Could not connect: '.mysqli_error());
}
$db_selected= mysqli_select_db($link,DB_NAME);
if(!$db_selected)
{
die('Cant use : '.mysqli_error());
}
$sql="insert into yourtable values('$_POST[str]')";
mysqli_query($link,$sql);
mysqli_close($link);
?>
这篇关于将附加的元素保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!