Amp html Amp XHR表单页面重定向
我创建了amp表单。 当我点击Butona时,我检查表单中不同页面的值。 我在这里没有问题。 如果所有的数据都是正确的,我想指示一个唯一的地址。 当表格为正数时,我如何指导访客Amp html Amp XHR表单页面重定向,amp-html,Amp Html,我创建了amp表单。 当我点击Butona时,我检查表单中不同页面的值。 我在这里没有问题。 如果所有的数据都是正确的,我想指示一个唯一的地址。 当表格为正数时,我如何指导访客 <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script> <script async custom-template="amp-mustach
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
<form method="POST" action-xhr="//example.com/control" target="_top">
<input type="checkbox" name="items[]" value="1" />
<input type="checkbox" name="items[]" value="2" />
<input type="checkbox" name="items[]" value="3" />
...
<button type="submit" class="btn">Check and go</button>
<div submit-success>
<template type="amp-mustache">
{{message}}
</template>
</div>
<div submit-error>
<template type="amp-mustache">
{{message}}
</template>
</div>
</form>
我在名为message的变量中显示Json带来的请求。
我想转发一个不同的地址,而不是显示最后一条消息。如果您在后端使用PHP来控制XHR表单的提交值,那么您只需遵循以下步骤即可:
if( !empty($_POST) ){
//--YOUR OTHER CODING--
//...........
$domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
$redirect_url = 'https://example.com/forms/thank-you/'; //-->MUST BE 'https://';
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: ".$domain_url);
if( isEmailAlreadyExist($_POST['email']) ){ //-->SAMPLE Valiation!
//-->Any 40X status code!
//Reference-1: https://github.com/ampproject/amphtml/issues/6058
//Reference-2: http://php.net/manual/pt_BR/function.http-response-code.php
header("HTTP/1.0 412 Precondition Failed", true, 412);
echo json_encode(array('errmsg'=>'This email already exist!'));
die();
}else{
//--Assuming all validations are good here--
if( empty($redirect_url) ){
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
}else{
header("AMP-Redirect-To: ".$redirect_url);
header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin");
}
echo json_encode(array('successmsg'=>'My success message. [It will be displayed shortly(!) if with redirect]'));
die();
}
}
存在一个错误,重定向URL必须是绝对HTTPS URL,否则AMP将抛出错误,重定向将不会发生。
希望这会有所帮助 而不是使用
action-xhr="//example.com/control"
使用
并让URL//example.com/control重定向用户必须使用的:
header('AMP-Redirect-To: https://...');
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin, AMP-Redirect-To");
请添加您的代码我添加代码。这不是这个问题的答案!
header('AMP-Redirect-To: https://...');
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin, AMP-Redirect-To");