The problem you have is that PHP is evaluated before html is, since you don't have anything to "exclude" the function prior to the user hitting the submit button, it's parsing the whole thing and throwing error on the fact that the $_POST
array is empty, more specifically, that the 'answer' key doesn't exist inside of $_POST
.
to fix your problem with $_POST having an undefined index of 'answer' simply make it wait until the $_POST array has been populated by the submitted form:
Assuming this file is 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 ?")
}
I don't know what raw_input() does in python, but this will help solve your indexing issue to get you back on track to translating it.
EDIT
(sorry for the delay, took a little while to write this all out)
as requested, here's an example of how your process could work:
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;