您遇到的问题是 PHP 在 html 之前被评估,因为在用户点击提交按钮之前您没有任何东西可以“排除”该函数,它正在解析整个事情并抛出错误$_POST 数组为空,更具体地说,$_POST 中不存在“answer”键。
要解决 $_POST 具有未定义的“答案”索引的问题,只需让它等到 $_POST 数组被提交的表单填充:
假设这个文件是index.php:
<form action="index.php" method="post">
Name: <input type="text" name="answer" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>
<?php
function F($q)
{
echo $q;
$a = $_POST['answer']; // a = raw_input(q)
$ret = strcmp($a, 'y') == 0 ? 1 : 0;
return $ret;
}
if(isset($_POST['submit'])){
F("ha ha ha ?")
}
我不知道 raw_input() 在 python 中的作用,但这将有助于解决您的索引问题,让您回到翻译它的正轨。
编辑
(抱歉耽搁了,花了一点时间才写完)
根据要求,以下是您的流程如何工作的示例:
index.php
<input id="send_request" type="submit" name="submit" value="Submit me!" />
<div id="result"></div>
<script>
var obj_merge = function(a, b, e){
var c={},d={};
for(var att in a)c[att]=a[att];
for(var att in b){if(e&&typeof c[att]==='undefined'){d[att]=b[att];}c[att]=b[att];}
if(e)c={obj:c,extras:function(){return d;}};
return c;
};
var serialize_obj = function(data, str){
if(!str) str = "";
if(data){
for(var i in data){
if(typeof data[i] === 'object') str += serialize_for_post(data[i], str);
else str += (str.length <= 0) ? i+"="+data[i] : "&"+i+"="+data[i];
}
}
return str;
};
var do_ajax = function(input_config){
var default_config = {
url: null,
req_type: 'GET',
async: true,
data: null,
callback: null
};
var config = obj_merge(default_config, input_config);
if(typeof config.url === 'undefined') return false;
if(config.data){
config.data = serialize_obj(config.data);
if(config.req_type.toUpperCase() === 'GET'){
config.url = config.url+"?"+config.data
}
}
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest(); // IE7+, FFx, Chr, Opa, Safi
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE5 & IE6
}
if("withCredentials" in xmlhttp){
xmlhttp.open(config.req_type,config.url,config.async);
} else if(typeof XDomainRequest != "undefined"){
xmlhttp = new XDomainRequest();
xmlhttp.open(config.req_type,config.url);
} else {
xmlhttp = null;
}
if(!xmlhttp){
console.log("CORS Support Missing.");
return false;
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState === 4){
if(typeof config.callback === 'function'){
config.callback(xmlhttp.responseText, xmlhttp);
} else {
console.log(xmlhttp);
}
}
};
xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
if(config.req_type.toUpperCase() === 'POST'){
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send('POST&'+config.data);
} else {
xmlhttp.send();
}
};
document.getElementById('send_request').onclick = function(event){
var result = prompt("Enter Something");
do_ajax({
url: "ajax_page.php",
data: {"answer": result},
req_type: "POST",
callback: function(response){
var json_data = JSON.parse(response);
var output = json_data ? "" : response;
if(output.length === 0 && json_data){
for(var key in json_data){
output += key+": "+json_data[key]+"<br />";
}
}
document.getElementById('result').innerHTML = output;
}
});
};
</script>
ajax_page.php
<?php
function F($a){
return strcmp($a, 'y') == 0 ? 1 : 0;
}
if(!empty($_POST) && array_key_exists('answer', $_POST)){
$result = F($_POST['answer']);
/*
* DO SOMETHING WITH $result
*/
echo json_encode(array('status' => true, 'result' => $result));
die;
} else {
echo json_encode(array('status' => false, 'error' => 'No answer was provided'));
die;
}
echo json_encode(array('status' => false, 'error' => 'Something went wrong, sorry about that!'));
die;