我正在尝试从数据库加载到表单中的现有数据,并允许用户编辑和更新它。但由于某种原因,当编辑的数据由ajax发送时,所有字段都是空的。加载当前数据,将其设置为字段和SQL查询本身,似乎工作正常。 所以我真的迷失在这里。
//bootstrap modal
<!--- Edit Campaign Modal------->
<div id="editCampaignModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Cmapaign Details:</h4>
</div>
<div class="modal-body">
<div id="new-campaign-com-with-server">
<div id="edit-campaign-add-msg"></div>
<form role="form" id="edit-campaign-form" class="campaign-form" method="post" action="newCampaign.php">
<fieldset class="form-group">
<div class="form-group">
<label for="ecname">Name: </label>
<input type="text" class="form-control" id="ecname" name="cname">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="eproduct">Product: </label>
<input type="text" class="form-control" id="eproduct" name="product">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="euserId">User ID: </label>
<input type="text" class="form-control" id="euserId" name="userId">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="esource">Source: </label>
<input type="text" class="form-control" id="esource" name="source">
</div>
</fieldset> <fieldset class="form-group">
<div class="form-group">
<label for="eformat">Format: </label>
<input type="text" class="form-control" id="eformat" name="format">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="etype">Type: </label>
<input type="text" class="form-control" id="etype" name="type">
</div>
</fieldset> <fieldset class="form-group">
<div class="form-group">
<label for="ecostType">Cost Type: </label>
<input type="text" class="form-control" id="ecostType" name="costType">
</div>
</fieldset> <fieldset class="form-group">
<div class="form-group">
<label for="ecostValue">Cost Value: </label>
<input type="text" class="form-control" id="ecostValue" name="CostValue">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="esaleType">Sale Type: </label>
<input type="text" class="form-control" id="esaleType" name="saleType">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="esaleValue">Sale Value: </label>
<input type="text" class="form-control" id="esaleValue" name="saleValue">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="einsertPixel">Insert Pixel:</label>
<input type="text" class="form-control" id="einsertPixel" name="insertPixel">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="etimeZone">Time Zone: </label>
<input type="text" class="form-control" id="etimeZone" name="timeZone">
</div>
</fieldset>
<fieldset>
<h2>Alerts</h2>
<fieldset class="form-group">
<div class="form-group">
<label for="eUVLLow">UVL Low: </label>
<input type="text" class="form-control" id="eUVLLow" name="UVLLow">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="eUVLHigh">UVL High: </label>
<input type="text" class="form-control" id="eUVLHigh" name="UVLHigh">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="eCPLLow">CPL Low: </label>
<input type="text" class="form-control" id="eCPLLow" name="CPLLow">
</div>
</fieldset>
<fieldset class="form-group">
<div class="form-group">
<label for="eCPLHigh">CPL High: </label>
<input type="text" class="form-control" id="eCPLHigh" name="CPLHigh">
</div>
</fieldset>
</fieldset>
<button type="submit" class="btn btn-default" value="Submit-form">Update Changes</button>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<!------------------------------>
// jQuery脚本
function editCampaign(queryArr)
{
//loading all the array data in to the form
$("#ecname").val(queryArr[1]);
// document.getElementById('ecname').value=queryArr[1];
$("#eproduct").val(queryArr[2]);
$("#euserId").val(queryArr[3]);
$("#esource").val(queryArr[4]);
$("#eformat").val(queryArr[5]);
$("#etype").val(queryArr[6]);
$("#ecostType").val(queryArr[7]);
$("#ecostValue").val(queryArr[8]);
$("#esaleType").val(queryArr[9]);
$("#esaleValue").val(queryArr[10]);
$("#einsertPixel").val(queryArr[11]);
$("#etimeZone").val(queryArr[12]);
$("#eUVLLow").val(queryArr[13]);
$("#eUVLHigh").val(queryArr[14]);
$("#eCPLLow").val(queryArr[15]);
$("#eCPLHigh").val(queryArr[16]);
$("#edit-campaign-add-msg").hide();
$("#edit-campaign-form").show();
$('#editCampaignModal').modal({show: true});
}
function getRowId(event)
{
id = $(event.target).parent().parent().parent().parent().children('td').eq(0).html();
}
$(document).ready(function() {
//event fired on edit campaign 'update changes' button press
$("#edit-campaign-form").submit(function(event){
var data = {
"CID": id
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "updateCampaignEditChanges.php",
data: data,
success: function(data)
{
$("#edit-campaign-add-msg").show();
$("#edit-campaign-add-msg").html("Success, Form Updated!");
$("#edit-campaign-form").hide();
}
});
event.preventDefault();
});
});
//用于更新更改的php代码 //现在没有表单验证
<?php
if (is_ajax()) {
test_function();
}
//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function()
{
$servername = "************.com";
$username = "*************";
$password = "**********";
$dbname = "******";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
{
$ID=$_POST["CID"];
$CNAME= $_POST["ecname"];
$PRODUCT= $_POST["eproduct"];
$USERID= $_POST["euserId"];
$SOURCE= $_POST["esource"];
$FORMAT= $_POST["eformat"];
$TYPE= $_POST["etype"];
$COSTTYPE= $_POST["ecostType"];
$COSTVALUE= $_POST["ecostValue"];
$SALETYPE= $_POST["esaleType"];
$SALEVALUE= $_POST["esaleValue"];
$INSERTPIXEL= $_POST["einsertPixel"];
$TIMEZONE= $_POST["etimeZone"];
$UVLLOW= $_POST["eUVLLow"];
$UVLHIGH= $_POST["eUVLHight"];
$CPLLOW= $_POST["eCPLLow"];
$CPLHIGH= $_POST["eCPLHigh"];
$sql = "UPDATE campaign
SET name='$CNAME',
product='$PRODUCT',
user_id='$USERID',
source='$SOURCE',
format='$FORMAT',
type='$TYPE',
cost_type='$COSTTYPE',
cost_value='$COSTVALUE',
sale_type='$SALETYPE',
sale_value='$SALEVALUE',
insert_pixel='$INSERTPIXEL',
time_zone='$TIMEZONE',
uvl_low='$UVLLOW',
uvl_high='$UVLHIGH',
cpl_low='$CPLLOW',
cpl_high='$CPLHIGH'
WHERE cid='$ID'";
$result = $conn->query($sql);
}
$conn->close();
if(isset($_POST["ecname"]))
$result="field was passed correctly :)";
else
$result="field is empty! :O";
echo json_encode($result);
}
?>
我真的很抱歉这个特别长的表格,提前感谢您的帮助。
答案 0 :(得分:1)
处理表单提交的PHP正在寻找错误的变量。当您要查找表单字段ID
时,您正在寻找表单字段name
。提交表单时,name
属性用作键,而不是id
。更改HTML以使name
属性与id
匹配,或更改PHP以使用name
属性作为键。