内部服务器错误 MAMP 使用 jQuery ajax 调用 PHP 函数
Internal server error MAMP calling PHP function with jQuery ajax
如果这是一式三份的副本,我很抱歉,但我一直在研究有关通过 ajax 调用 PHP 函数的所有问题 - 我就是做不到让它工作。我正在使用 MAMP,我的代码如下所示:
index.html
<form id="form">
<h2 class="form-header" id="form-header"></h2>
<p class="form-instruction" id="form-instruction"></p>
<div class="inputs" id="inputs"></div>
<div class="next-step" id="next-step">
<button id="btn-next" onclick="result()" type="button" class="btn-next">Go to step 2</button>or
<button class="btn-cancel" id="btn-cancel" type="button" onclick="init()">Cancel</button>
</div>
</form>
输入字段是通过 index.js 中的 init() 函数动态添加的:
let step;
let shapes = ['rectangle', 'circle', 'elipse','square'];
let shape;
function init(){
step = 1;
if(inputs.innerHTML != ""){
inputs.innerHTML = "";
};
form_header.innerHTML = "Step " + step + " : Select your shape";
form_instruction.innerHTML = 'Please select the shape that you would like to calculate the area of and press the button "Go to step 2"'
btn.innerHTML = "Go to step 2";
for (var i = 0; i < shapes.length; i++) {
inputs.innerHTML += '<div class="input"><input type="radio" id="' + shapes[i] + '" name="select" value="' + shapes[i] + '"><label for="' + shapes[i] + '">' + shapes[i] + '</label></div>'
}
}
..函数 result() 从 index.html:
调用 onclick
var width;
var height;
function result(){
var url = shape.charAt(0).toUpperCase() + shape.slice(1);
var valW = document.getElementById(shape + "w").value;
width = parseInt(valW);
var valH = document.getElementById(shape + "h");
if (valH){
valH = valH.value
height = parseInt(valH);
}
$.ajax({
type: 'POST',
url: "./shapeHandler.php",
data: {
shape: url,
width: width,
height: height
},
cache: false,
success: function (resp) {
console.log(resp);
},
error: function(xhr, status, err){
console.log(xhr, status);
console.log(err);
}
});
}
shapeHandler.php
<?php
include './classes/Circle.php';
include './classes/Rectangle.php';
include './classes/Square.php';
include './classes/Elipse.php';
$width = $_POST["width"];
$shape = $_POST["shape"];
if( isset($_POST["height"])){
$height = $_POST["height"];
echo "Height: " . $height . ". ";
}
echo "Shape: " . $shape . ". Width: " . $width;
switch($shape){
case "Circle":
$circle = new Circle($width);
$circle->calculateArea();
echo ". >>>> RESULT: " . json_encode($circle);
break;
case "Rectangle":
$rectangle = new Rectangle($width, $height);
$rectangle->calculateArea();
echo ". >>>> RESULT: " . json_encode($rectangle);
break;
case "Elipse":
$elipse = new Elipse($width, $height);
$elipse->calculateArea();
echo ". >>>> RESULT: " . json_encode($elipse);
break;
case "Square":
$square = new Square($width);
$square->calculateArea();
echo ". >>>> RESULT: " . json_encode($square);
break;
default:
echo "No shape was selected";
};
ex Circle.php
<?php
class Circle {
private $width;
private $pi = 3.14159;
public function __construct($width){
$this->width = $width;
}
public function calculateArea(){
return $this->width * $this->width * $this->pi;
}
}
但我在调用函数 calculateArea() 时只得到一个空对象“{}”:
任何输入将不胜感激!
您正在通过 POST
方法提交表单,在 PHP 中您正在检查 $_GET
中的值,检查您在 $_POST
中得到的内容,此外,创建你自己一个最小的调试环境。创建一个文件并向其中写入各种感兴趣的值。它将帮助您更好地理解流程。
LE - 您必须从 Circle
class 和其他 class 中删除 $_GET['width']
,您可以在 construct
(__construct($width, $height)
后跟 $this->width = $width
,高度相同或在 class 中需要的任何强制性 属性),然后在创建新实例时传递这些值class.
<?php
class Circle {
private $radius = $_GET["width"];
private $pi = 3.14159;
public function __construct(){
$this->radius = $radius;
}
public function calculateArea(){
return $this->radius * $this->radius * $pi;
}
}
您不能将动态 属性 作为默认值分配给 class 属性 (private $radius = $_GET["width"];
)
您还在构造函数中将变量 $radius
分配给 $this->radius
但您从未声明 [=12=]!
此外,正如 Ravenous 提到的,您需要通过 GET 将数据发送到 php 脚本或在 php 脚本中使用 $_POST
var!
这可以解决您的问题!
如果这是一式三份的副本,我很抱歉,但我一直在研究有关通过 ajax 调用 PHP 函数的所有问题 - 我就是做不到让它工作。我正在使用 MAMP,我的代码如下所示:
index.html
<form id="form">
<h2 class="form-header" id="form-header"></h2>
<p class="form-instruction" id="form-instruction"></p>
<div class="inputs" id="inputs"></div>
<div class="next-step" id="next-step">
<button id="btn-next" onclick="result()" type="button" class="btn-next">Go to step 2</button>or
<button class="btn-cancel" id="btn-cancel" type="button" onclick="init()">Cancel</button>
</div>
</form>
输入字段是通过 index.js 中的 init() 函数动态添加的:
let step;
let shapes = ['rectangle', 'circle', 'elipse','square'];
let shape;
function init(){
step = 1;
if(inputs.innerHTML != ""){
inputs.innerHTML = "";
};
form_header.innerHTML = "Step " + step + " : Select your shape";
form_instruction.innerHTML = 'Please select the shape that you would like to calculate the area of and press the button "Go to step 2"'
btn.innerHTML = "Go to step 2";
for (var i = 0; i < shapes.length; i++) {
inputs.innerHTML += '<div class="input"><input type="radio" id="' + shapes[i] + '" name="select" value="' + shapes[i] + '"><label for="' + shapes[i] + '">' + shapes[i] + '</label></div>'
}
}
..函数 result() 从 index.html:
调用 onclickvar width;
var height;
function result(){
var url = shape.charAt(0).toUpperCase() + shape.slice(1);
var valW = document.getElementById(shape + "w").value;
width = parseInt(valW);
var valH = document.getElementById(shape + "h");
if (valH){
valH = valH.value
height = parseInt(valH);
}
$.ajax({
type: 'POST',
url: "./shapeHandler.php",
data: {
shape: url,
width: width,
height: height
},
cache: false,
success: function (resp) {
console.log(resp);
},
error: function(xhr, status, err){
console.log(xhr, status);
console.log(err);
}
});
}
shapeHandler.php
<?php
include './classes/Circle.php';
include './classes/Rectangle.php';
include './classes/Square.php';
include './classes/Elipse.php';
$width = $_POST["width"];
$shape = $_POST["shape"];
if( isset($_POST["height"])){
$height = $_POST["height"];
echo "Height: " . $height . ". ";
}
echo "Shape: " . $shape . ". Width: " . $width;
switch($shape){
case "Circle":
$circle = new Circle($width);
$circle->calculateArea();
echo ". >>>> RESULT: " . json_encode($circle);
break;
case "Rectangle":
$rectangle = new Rectangle($width, $height);
$rectangle->calculateArea();
echo ". >>>> RESULT: " . json_encode($rectangle);
break;
case "Elipse":
$elipse = new Elipse($width, $height);
$elipse->calculateArea();
echo ". >>>> RESULT: " . json_encode($elipse);
break;
case "Square":
$square = new Square($width);
$square->calculateArea();
echo ". >>>> RESULT: " . json_encode($square);
break;
default:
echo "No shape was selected";
};
ex Circle.php
<?php
class Circle {
private $width;
private $pi = 3.14159;
public function __construct($width){
$this->width = $width;
}
public function calculateArea(){
return $this->width * $this->width * $this->pi;
}
}
但我在调用函数 calculateArea() 时只得到一个空对象“{}”:
任何输入将不胜感激!
您正在通过 POST
方法提交表单,在 PHP 中您正在检查 $_GET
中的值,检查您在 $_POST
中得到的内容,此外,创建你自己一个最小的调试环境。创建一个文件并向其中写入各种感兴趣的值。它将帮助您更好地理解流程。
LE - 您必须从 Circle
class 和其他 class 中删除 $_GET['width']
,您可以在 construct
(__construct($width, $height)
后跟 $this->width = $width
,高度相同或在 class 中需要的任何强制性 属性),然后在创建新实例时传递这些值class.
<?php
class Circle {
private $radius = $_GET["width"];
private $pi = 3.14159;
public function __construct(){
$this->radius = $radius;
}
public function calculateArea(){
return $this->radius * $this->radius * $pi;
}
}
您不能将动态 属性 作为默认值分配给 class 属性 (private $radius = $_GET["width"];
)
您还在构造函数中将变量 $radius
分配给 $this->radius
但您从未声明 [=12=]!
此外,正如 Ravenous 提到的,您需要通过 GET 将数据发送到 php 脚本或在 php 脚本中使用 $_POST
var!
这可以解决您的问题!