我很抱歉,如果这是一式三份的复制品,但我一直在讨论通过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;
}
}
但我只得到一个空对象&#34; {}&#34;在调用函数calculateArea():
时任何输入都将非常感谢!
答案 0 :(得分:2)
<?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;
}
}
您不能将动态属性作为默认值分配给类属性(private $radius = $_GET["width"];
)
您还要在构造函数中将变量$radius
分配给$this->radius
,但您从未声明$radius
!
同样正如Ravenous所提到的,您需要通过GET将数据发送到php脚本或使用PHP脚本中的$_POST
var!
这可以解决您的问题!
答案 1 :(得分:1)
您正在通过POST
方法提交表单,而在PHP中,您正在检查$_GET
中的值,检查$_POST
中的内容,并创建一个最小的调试环境。创建一个文件并写入各种感兴趣的值。它将帮助您更好地理解流程。
LE - 您必须从$_GET['width']
班级和其他班级中删除Circle
,您可以在width
中加入construct
(__construct($width, $height)
后跟$this->width = $width
,高度相同或类中需要的任何强制属性)然后在创建类的新实例时传递这些值。