【发布时间】:2014-03-18 17:06:00
【问题描述】:
我想将一些 select2(jquery 插件)输入数据添加到数据库中......所以首先我有 html 并且 html 很好:
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="vrsta">Vrsta aktivnosti</label>
<div class="col-md-4">
<input type="hidden" id="vrsta_rada" class="form-control select2-container select2-container-multi"/>
</div>
</div>
我将select2数据转换成字符串:
var data1 = $("#vrsta_rada").select2("data");
$.each(data1, function(key, value) {
var vrsta_rada = data[key].text
});
我尝试使用 ajax/jquery/php 将此变量 vrsta 添加到数据库中
ajax:
$.ajax({
url: "insertAkt.php",
type: "POST",
async: true,
data: { vrsta_rada:vrsta_rada},
dataType: "html",
success: function(data) {
$('#output').html(data);
$('#myModal').modal('hide');
drawVisualization();
},
});
和php:
try {
$DBH = new PDO($dsn, $user, $pass, $opt);
$STH = $DBH->prepare("INSERT INTO aktivnosti (vrsta) VALUES (:1)");
$STH->bindParam(':1', $_POST['vrsta_rada']);
$STH->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
echo "<p>Data submitted successfully</p>";
}
在浏览器控制台中出现错误:
Uncaught InvalidStateError: Failed to read the 'selectionDirection' property from 'HTMLInputElement': The input element's type ('hidden') does not support selection. jquery-latest.js:7546
gn jquery-latest.js:7546
x.param jquery-latest.js:7519
x.extend.ajax jquery-latest.js:8021
(anonymous function) zadaci.html:341
x.event.dispatch jquery-latest.js:5095
v.handle
【问题讨论】:
-
这里有什么问题?
-
是不是像格式不正确的 json 一样简单。尝试`数据:{“vrsta_rada”:vrsta_rada},'
标签: javascript php jquery ajax input