Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重定向到不带Javascript的post方法中包含变量的url_Php_Html - Fatal编程技术网

Php 重定向到不带Javascript的post方法中包含变量的url

Php 重定向到不带Javascript的post方法中包含变量的url,php,html,Php,Html,如果满足条件,我想将我的页面重定向到另一个页面 说, samplepage1.php $id = $_POST['id']; $name=$_POST['name']; if($max>$count){ //action on the same page } else{ //redirect to URL:index.php/samplepage2.php with the values $id & $name in POST METHOD } 我需要一个解决方案,以便这些值必须

如果满足条件,我想将我的页面重定向到另一个页面 说, samplepage1.php

$id = $_POST['id'];
$name=$_POST['name'];
if($max>$count){
//action on the same page
}
else{
//redirect to URL:index.php/samplepage2.php with the values $id & $name in POST METHOD
}

我需要一个解决方案,以便这些值必须通过post发布到“samplepage2.php”,但严格来说不使用javascript实现自动提交(我不喜欢javascript的帮助,好像用户可以在浏览器中关闭它)

@nickb的评论是正确的。如果您的表单或其他任何东西处理javascript,并且会改变页面可以做什么和不能做什么,那么试图找出如何容纳$\u帖子是毫无意义的

但是,处理此问题的一种方法是将该页面的$\u帖子切换到$\u会话

比如:

$_SESSION['form1'] = $_POST;
当您到达下一页时(确保在每一页的开头都有session_start()),如果您真的想切换回它。但是,一旦完成($\u SESSION['form1']),不要忘记取消设置($\u SESSION['form1'])。

尝试以下方法:

$id = $_POST['id'];
$name=$_POST['name'];
if($max>$count){
//action on the same page
}
else{
$url = 'http://yourdomain.com/samplepage2.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'id='.$id);
curl_exec($ch);
curl_close($ch);$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'name='.$name);
curl_exec($ch);
curl_close($ch); 
}

如果服务器上启用了
curl
,则可以使用curl将
POST
数据发送到其他表单

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "index.php/samplepage2.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'id' => 'value of id',
    'name' => 'value of name'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

一些可笑的微不足道的人实际上关闭了JS,我认为大约有2-3%的用户关闭了JS。无论如何,如果没有JS,你将无法自动执行POST重定向,你最终将显示一个“单击此按钮继续:”表单,这在IMO中很糟糕。我正在尝试实现一个在线考试门户,如果我使用JS进行编码,那么在线考试可以很容易地操作。或者,这可能是一个很好的解决方案,可以限制整个在线测试,并要求在javascript上继续…但我仅限于这样一种情况,为什么您要执行两个请求?就做一个<代码>curl_setopt($ch,CURLOPT_POSTFIELDS,数组('id'=>$id,'name'=>$name))因此,如果它到达“else”,它将重定向到“”{'条件正确???@RahulTS:这不会重定向您。它只会发送一个POST请求。因此,在这段代码之后,如果我使用header redirect重定向,是否能够从那里获得POST值'samplepage2.php'@RahulTS:No。除非您将它们保存在
$\u会话
之类的位置。他想重定向到页面(就像在HTML页面上发布表单一样)。这不会重定向您。