PHP return to Javascript



我想在这里实现的是要么回显到我的PHP,要么返回到我的Javascript。现在我必须同时做这两件事,因为PHP需要返回,Javascript需要回显。如果有办法的话,我想实现其中一个选项,同时使用这两个选项。我想同时返回到我的javascript和php,或者同时返回到javascript和php以缩短代码。这有可能实现吗?

在PHP中用include((赋值变量时,必须使用return$result;以便为该变量分配结果。当我把Javascript部分添加到我的网站时,我现在想到了必须在我的php中分配一个变量$noJS=true;然后运行include并使用if语句来了解如何输出。

if(!empty($noJS((:return$result;其他:echo$result;endif;

基本上,我想问的是,是否有更好的方法将结果返回到我的javascript?或者以某种方式让我的javascript能够获得返回结果,而不必回声并将结果作为responseText?如何获得返回响应而不是页面上的输出响应?

PHP

if (!empty($_POST)) :
$noJS = true;
$_SESSION['produce'] = include('../php/produce.php');
header("Location: produce.php?rnd=$round[round]#actions");
exit;
endif;

Javascript

xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = () => {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var notify = document.getElementById('notify'),
result = xhttp.responseText,
resarr = result.split(':'),
};
xhttp.open('POST', '../php/produce.php?rnd=' + round);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send('amount=' + amount + '&type=' + type);

外部

if (!empty($noJS)) :
return [0, 'You selected an invalid turns amount.'];
else :
echo '0:You selected an invalid turns amount.';
endif;

在外部页面上,我无法返回$result;因为xhttp.responseText只接收我在页面上回显的内容。有没有办法让我的javascript和php协同工作,这样我就可以使用其中一个?

更新:编写了一页代码,试图解释我正在努力实现的目标。在这段代码中,我无法返回,因为javascript永远不会接收到echo。相反,如果禁用javascript,将echo放在第一位,将return放在第二位,则会将echo文本放在页面内的doctype之上。这是在先前的例子中使用$noJS变量。现在,我如何让它做其中一个或另一个,并获得我需要的值给需要分配给它们的变量,而不遇到问题或不必使用$noJS变量?

Main page:
<?php
if (!empty($_POST)) :
$var = include('external.php');
/*
Result must be returned via return and not echo.
If external page echos instead of using return than $var will 
have a value of 1 telling me that the include happened successfully.
Instead of returning the pages contents
*/
endif;
?>
<!DOCTYPE html>
<html>
<head>
<script>
var data = document.getElementById('data').value,
submit = document.getElementById('submit'),
xhttp = new XMLHttpRequest();
submit.addEventListener('click', () => {
xhttp.onreadystatechange = () => {
if (xhttp.readyState == 4 && xhttp.status == 200) 
{
var notify = 
document.getElementById('notify'),
result = xhttp.responseText,
/*
Result must be returned via echo and not 
via return.
If external page returns instead of being 
echod than 'result' will have no value as there is no content on 
the external page.
*/
resarr = result.split(':');
if (resarr[0] == 0) {
notify.style.backgroundColor = '#900';
} else {
notify.style.backgroundColor = '#090';
}
notify.innerHtml = resarr[1];
};
xhttp.open('POST', 'external.php');
xhttp.setRequestHeader("Content-type", "application/x- 
www-form-urlencoded");
xhttp.send('data=' + data);
});
</script>
</head>
<body>
<?php if (!empty($var) && $var[0]) : ?>
<div id="notify" style="background-color:#090;"><?php echo 
$var; ?></div>
<?php elseif (!empty($var)) : ?>
<div id="notify" style="background-color:#900;"><?php echo 
$var; ?></div>
<?php else : ?>
<div id="notify"></div>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input id="data" name="data" type="text" placeholder="Data To 
Send" />
<button id="submit">Submit</button>
</form>
</body>
</html>

外部页面:

<?php
`enter code here` if (!empty($_POST)) :
$data = htmlspecialchars($_POST['data'], ENT_QUOTES);
if (strlen($data) > 5) :
return [1, 'Your input was longer than 5 characters!'];
/* Must return instead of echo for the variable $var to have 
the value otherwise $var will be equal to 1 instead of 
the value echoed out onto the page. */
echo '1:Your input was longer than 5 characters!'; 
/* Must echo for javascript to receive the value however if  
done in this order the return will fire first and the 
echo will never happen. If done in other order the echoed text 
will be echoed out onto the top of the page before the <!doctype html> if 
the user has javascript is disabled  */
else :
return [0, 'Your input was NOT longer than 5 characters!'];
/* Must return instead of echo for the variable $var to have 
the value otherwise $var will be equal to 1 instead of the 
value echoed out onto the page. */
echo '1:Your input was NOT longer than 5 characters!'; 
/* Must echo for javascript to receive the value however if 
done in this order the return will fire first and the echo 
will never happen */
endif;
endif;
?>

您正试图使用http重定向在两个不同的页面之间运行

您正在尝试使用著名的Location:http标头

Http重定向不是服务器的工作,它是浏览器的工作

一旦浏览器启动重定向,它就不能保证在该页面中执行JavaScript

如果目标页面中有一个JavaScript,即当浏览器看到<script>标签时将执行的produce.php

如果php有回声,includerequire都会将其呈现给浏览器

$var = include('external.php');

如果该代码有回声,它显然会在浏览器中呈现,从external.php中删除所有回声,并通过代码进行处理,使输出有条件地

如果两者都想要,那么像这个一样向$GET或$POST添加自定义属性

$GET['flag'] = 32;
$var = include('external.php');

现在在external.php中,您可以读取值32

if($GET['flag'] == 32)
return [0, 'Your input was NOT longer than 5 characters!'];
else
echo '1:Your input was NOT longer than 5 characters!'; 

最新更新