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
?>