Php 我想将无序列表保存到android phonegap数据库中的一列中
我想将无序列表保存到数据库中的列中。我该怎么做 切斯 火腿 创建表INGREDIENTE name varchar20,配料varchar30 我想把清单放在配料部分 下面是我用来创建列表的html和js中的代码Php 我想将无序列表保存到android phonegap数据库中的一列中,php,android,html,ajax,cordova,Php,Android,Html,Ajax,Cordova,我想将无序列表保存到数据库中的列中。我该怎么做 切斯 火腿 创建表INGREDIENTE name varchar20,配料varchar30 我想把清单放在配料部分 下面是我用来创建列表的html和js中的代码 function selectIngredient(select) { var $ul = $(select).closest('.ui-select').prev('ul'); console.log($ul[0]) if ($ul.find('inpu
function selectIngredient(select)
{
var $ul = $(select).closest('.ui-select').prev('ul');
console.log($ul[0])
if ($ul.find('input[value=' + $(select).val() + ']').length == 0) {
console.log('s')
$ul.append('<li onclick="$(this).remove();">' +
'<input type="hidden" name="ingredients[]" value="' +
$(select).val() + '" /> ' +
$(select).find('option:selected').text() + '</li>');
}
}
<!DOCTYPE html>
<html>
<head>
<title>Ingredient</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="themes/receta.min.css" />
<link rel="stylesheet" href="themes/receta.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script src="recetas.js"> </script>
</head>
<body>
<form action="insert.php" method="post">
Receta: <input type="text" name="receta">
Ingredientes: <input type="text" name="ingredientes">
<input type="submit">
<label>Ingredientes</label>
<ul>
</ul>
<select onchange="selectIngredient(this);">
<option value="Cheese">Cheese</option>
<option value="Olives">Olives</option>
<option value="Pepperoni">Pepperoni</option>
<option value="Milk">Milk</option>
</select>
</body>
</html>
<?php
$dbhost = 'localhost';
$dbuser = 'alanis_lozano';
$dbpass = '20Anahuac12';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'alanis_recetas';
mysql_select_db($dbname, $conn);
$sql="INSERT INTO Recetas (receta, ingredientes)
VALUES
('$_POST[receta]',$ingredientes = join(',', $_POST['selections'])";
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($conn);
?>
您可以将列表转换为逗号分隔的字符串:
$ingredients = join(',', $_POST['selections']);
在dom中,您可以命名选择框选项。这里真正需要做的是以分隔的方式存储成分,并在输出时将其格式化为列表 试着这样做:
<form action="save.php">
<input type="text" name="name" />
<textarea name="ingredients"></textarea>
<button type="submit">Save</button>
</form>
save.php-保存前用换行符替换任何分隔符\n
<?php
$name = $_GET["name"];
$name = mysqli_real_escape_string($name);
$ingredients = $_GET["ingredients"];
$ingredients = mysqli_real_escape_string($ingredients);
$ingredients = preg_replace("/[,\.\n\t]+/", "\n", $ingredients);
display.php-在显示之前,按换行符拆分成分并以UL格式呈现
echo "<ul>";
$ingredients = explode('\n", $ingredients);
for ($i = 0; $i < count($ingredients); $i++) {
echo "<li>{$ingredients[$i]}</li>";
}
echo "</ul>";
我正在使用eclipse、phonegap、html和php。我已经做了添加名称的数据库测试,效果很好。但是我不知道如何添加列表。下面是我如何添加名称的:是什么触发了保存?有按钮吗?表格?你意识到列表是输出,不是输入,对吗?听起来你更希望列表来自数据库。。。您可能应该有一个表单输入到DBNo,列表在html中,然后将其发送到数据库。但是我有一个问题,这个列表并不是每次都有相同的值。它改变了它的列表,有100种不同的东西。用户只选择他将要使用的。我知道你现在在做什么。。。为此,您确实应该使用AJAX实现一些东西,但是您还必须重新考虑数据库的设计。你应该有一个单独的配料表,在recipes表中有一个外键,这样你就可以每行存储一种配料。但是我有一个问题,这个列表并不是每次都有相同的值。它改变了它的列表,有100种不同的东西。用户只选择要使用的代码,我刚刚发布了包括php在内的所有代码。我知道有些地方不对劲,但我找不到。仅供参考,PHP中不推荐使用join。改用内爆。