我想保存四个字段,点击一个是文本框两个复选框和一个wysihtml5Editor使用ajax,如下所示:
<input type="text" id="title" name="title" required title="required" placeholder="title" data-bind="value:title" />
<input type="checkbox" id="active" name="active" class="check" data-bind="checked: active=='true'" />Active
<input type="checkbox" id="logon" name="logon" class="check" data-bind="checked: use_logo=='true'" />Logo
<textarea id="iiii" name="iiii" class="htmleditor" rows="9" cols="50" style="width: 600px; height: 190px;"></textarea>
<button type="button" name="save" id="save" class="btn btn-primary">Save</button>
我的表名是contracts
,字段名称如下:
title(textbox),def_next(wysihtml5Editor),active(checkbox),use_logo(checkbox)
我正在按以下条件进行:
if ($_POST['what']=="save"){}
我尝试过ajax
$.ajax({
type: 'POST',
url: root + 'data/comapanydata_contractorslist?json',
data: {
what: "save",
text: $('#iiii').val(),
def_text: def_text
},
success: function (data) {},
dataType: 'json'
});
但没有工作,所以请在此建议我。
答案 0 :(得分:0)
这对我有用:
的index.html
<script>
$(function() {
$("#form").submit(function() {
data = $("#form").serialize();
$.ajax({
type : "GET",
url : "save-data.php",
data : data,
success : function() {
$("#form").find("input[type=text], textarea").val("")
$("#form").find("input[type=checkbox]").prop('checked', false);
}
});
return false;
});
});
</script>
<form action="save-data.php" method="get" id="form">
<input type="text" id="title" name="title" required title="required" placeholder="title" data-bind="value:title" />
<input type="hidden" name="active" value="off" />
<input type="checkbox" id="active" name="active" class="check" data-bind="checked: active=='true'" />Active
<input type="hidden" name="logon" value="off" />
<input type="checkbox" id="logon" name="logon" class="check" data-bind="checked: use_logo=='true'" />Logo
<textarea id="iiii" name="iiii" class="htmleditor" rows="9" cols="50" style="width: 600px; height: 190px;"></textarea>
<input type="submit" name="save" id="save" value="Save" class="btn btn-primary"/>
</form>
将输入保存到变量:
节省-data.php
<?php
$required = array('title', 'iiii', 'active', 'logon');
$error = false;
foreach ($required as $field) {
if (empty($_GET[$field])) {
$error = true;
}
}
if (isset($_GET["title"])) {
if ($error) {
echo "All fields are required.";
} else {
$title = $_GET["title"];
$active = $_GET["active"];
$logon = $_GET["logon"];
$iiii = $_GET["iiii"];
//mysql query ...
}
}
?>
答案 1 :(得分:0)
如果要保存所有4个字段,则需要将数据从所有字段传输到服务器端脚本。也许是这样的:
$.ajax({
type: 'POST',
url: root + 'data/comapanydata_contractorslist?json',
data: {
what: "save",
def_text: $('#iiii').val(),
title: $('#title').val(),
active: $('#active').val(),
use_logo: $('#logon').val()
},
success: function (data) {},
dataType: 'json'
});