尝试通过json .getJSON将文本框值发布到数据库-此时,我只是想查看json是否发布到页面,我的UPDATE查询工作正常...
以下内容未按要求发布:
代码:
$(document).on("click", ".submit", function(event){
alert($(this).text());
var form_data = {
FDID: $('.fdid-1').val(),
CHOICE1: $('.choice-1').val(),
CHOICE2: $(".choice-2").val()
};
$.getJSON("modify.php",form_data,function(data){
switch(data.retval){
case 0: $("#status").html("Update successful!");
break;
case 1: $("#status").html("Unable to update!");
break;
default: $("#description").html("Database error, please try again.");
break;
}
});
});
Modify.php:
<?php
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
$fdid = json_decode($_POST['FDID']);
$choice1 = json_decode($_POST['CHOICE1']);
var_dump($choice1);
// This is in the PHP file and sends a Javascript alert to the client
$message = $fdid;
echo "<script type='text/javascript'>alert('$message');</script>";
?>
代码的更多信息:
$.each( data, function ( i, val ) {
($('<div>')
.attr({
'data-role': 'collapsible',
'data-content-theme': 'c',
'data-collapsed': 'true',
'id': 'cResults'
})
.html('<h4>' + this.LastName + ', ' + this.FirstName + '</h4>'
+ '<ul data-role="listview" data-filter="true" data-filter-placeholder="Search Choices..." data-inset="true" class="makecollapsibleul">'
+ '<li><form id="productForm" action="modify.php" method="post">'
+ '<label for="fdid-1">FDID:</label>'
+ '<input type="text" name="fdid-1" class="fdid-1" value=' + this.FDID + '>'
+ '</li><li>'
+ '<label for="text-1">Choice 1:</label>'
+ '<input type="text" name="choice-1" class="choice-1" value=' + this.C1 + '>'
+ '</li><li>'
+ '<label for="text-2">Choice 2:</label>'
+ '<input type="text" name="choice-2" class="choice-2" value=' + this.C2 + '>'
+ '</li><li>'
+ 'IP: ' + this.IPADDRESS + '</li><input type="submit" class="submit" value="UPDATE" /></form><li>'
+ 'Pick Date: ' + this.PICKDATE + '</li>'
+ '</ul>'))
.appendTo('#primary');
//$(".title").append('<li>'+orderNum+' -- '+itemNum+'</li>');
$('#makecollapsible').collapsibleset().trigger('create');
$.mobile.hidePageLoadingMsg();
您不应该调用json_encode()
获取参数。它们使用www-form-urlencoded
格式发送,PHP负责对其进行解码。
您需要调用json_encode
以对要发送回的结果进行编码。
<?php
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
$fdid = $_POST['FDID'];
$choice1 = $_POST['CHOICE1'];
//var_dump($choice1);
// This is in the PHP file and sends a Javascript alert to the client
$message = $fdid;
$result = array('retval' => 0,
'code' => "<script type='text/javascript'>alert('$message');</script>");
echo json_encode($result);
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句