我正在使用ajax / jquery在表单上进行数据库保存/抓取。我有节省和抓住的问题。我的ajax一直在失败,我不知道为什么。我知道这应该有效。我将ajaxCall移动到它自己的函数,在加载和提交时调用它,这就是事情似乎向南的时候。有人能指出我在错误的方向上正确的方向。我在下面有我的jquery和php。
JQUERY / AJAX代码
$(document).ready(function(){ AjaxCall的();
$('#submitBtn').on('click',function(event) {
var formData = $('form').serialize();
event.preventDefault();
ajaxCall(formData);
}); //END OF .ON
function ajaxCall(formData) {
$.ajax({
url: "php/save.php",
type: "POST",
dataType: "json",
//dataType: "string",
data: formData,
async: true,
success: successCall,
error: errorCall
});
} //END OF AJAX CALL FUNCTION
function errorCall() {
alert("There was an error with this request!")
}// END OF ERROR CALL FUNCTION
function successCall(data) {
//var records = $.parseJSON();
var record = "";
console.log(data);
/*$.each(data, function() {
ID = this['ID'];
NAME = this['NAME'];
PHONE = this['PHONE'];
ADDRESS = this['ADDRESS'];
var html = "<tr><td>" + ID + "</td><td>" + NAME + "</td><td>" + PHONE + "</td><td>" + ADDRESS + "</td></tr>";
$(html).appendTo("table");
});*/
//var jsonObject = $.parseJSON(data);
//alert("ajax call complete");
} //END OF SUCCESS CALL FUNCTION
}); // END OF .READY
PHP代码
class dbClass {
private $host = "localhost";
private $username = "root";
private $password = "";
private $schema = "formtest";
public $dbCon;
function __construct() {
$this->dbCon = new mysqli($this->host,$this->username,$this->password,$this->schema);
if ($this->dbCon->connect_error) {
die("Issue Connecting: " . $dbCon->connect_errorno . "Error: " . $dbCon->connect_error);
}
//var_dump($dbCon);
}
function saveDBRecord($dbName, $dbPhone, $dbAddress) {
//HOW TO FIGURE THIS OUT
$query = $this->dbCon->prepare("INSERT INTO contact (contact_name,contact_phone,contact_address) VALUES (?,?,?)");
$query->bind_param("sss",$dbName,$dbPhone,$dbAddress);
//var_dump($query);
$query->Execute();
$recordData = $this->grabRecords();
return $recordData;
}
function grabRecords() {
$query = $this->dbCon->query("SELECT * FROM contact;");
//var_dump($query);
$data = array();
$counter = 0;
$i = 0;
while($row = $query->fetch_row())
{
$record = array(
"ID" => $row[0],
"NAME" => $row[1],
"PHONE" => $row[2],
"ADDRESS" => $row[3]
);
$data[] = $record;
//$data[$i++] = "ID: {$row[0]} NAME: {$row[1]} PHONE: {$row[2]} ADDRESS: $row[3]";
//$data .= "ID: {$row[0]} NAME: {$row[1]} PHONE: {$row[2]} ADDRESS: $row[3]";
//$counter .= $counter;//var_dump($data);
//$assoc = $query->fetch_all();
//var_dump($assoc);
//return $assoc;
}
//var_dump($data);
return $data;
}
function __destruct() {
$this->dbCon->close();
}
} //END OF dbClass
$dbClassConnection = new dbClass();
/*function displayRecords($recordData) {
//var_dump($recordData[0]);
}*/
//$name = $_POST['name'];
//$phone = $_POST['phone'];
//$address = $_POST['address'];
//$name = "AutoInput";
//$phone = "AutoInput";
//$address = "AutoInput";
//var_dump($dbClassConnection);
if ($_POST['name'] == "" || $_POST['phone'] == "" || $_POST['address'] == "") {
$recordData = $dbClassConnection->grabRecords()
echo json_encode($recordData);
//echo json_encode("HELLO");
//$recordData = $dbClassConnection->grabRecords();
//echo json_encode("FIRST IF");
//var_dump($recordData);
} else {
$name = $_POST['name'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$recordData = $dbClassConnection->saveDBRecord($name,$phone,$address);
//$jsonData = displayRecords($recordData);
//var_dump(json_encode($recordData);
echo json_encode($recordData);
//var_dump($jsonData);
//echo json_encode("SECOND IF");
//var_dump($recordData);
}
//$name = $_POST['name'];
//ar_dump($name;
//echo json_encode($_POST);
//IF SUBMIT TRUE
//saveDBRecord and grabRecords
//ELSE
//JUST GrabRecords
&GT;
答案 0 :(得分:0)
问题是ajaxCall onLoad也需要序列化formData,然后问题是我如何处理IF语句的POST数据。