[英]Sending Form Data Using POST To The Current Page And Another PHP Page At The Same Time
I have seen another person ask this question in stack overflow but I did not get any clear answer/idea from 'his post' . 我看到另一个人在堆栈溢出中问这个问题,但我没有从'他的帖子'得到任何明确的答案/想法。 I want to know whether there is a way of sending form data via POST to the current PHP page
addstudio.php
to verify the entered data & if the entered data is valid it gets redirected to the next PHP page getstudio.php
. 我想知道是否有办法通过POST将表单数据发送到当前的PHP页面
addstudio.php
以验证输入的数据,如果输入的数据有效,它将被重定向到下一个PHP页面getstudio.php
。
To send the form data to the current page I used the following line:- 要将表单数据发送到当前页面,我使用以下行: -
<form action = "<?php $_PHP_SELF ?>" method = "post">
This is just done to validate the form information and if the form is left blank then a message is displayed in the same PHP page, the code for this is shown below. 这样做只是为了验证表单信息,如果表单留空,则在同一个PHP页面中显示一条消息,其代码如下所示。
<?php
$valid = true;
$studioIdError = $studioNameError = $studioAddressError = $studioPCodeError = $studioTelNoError = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST["studioId"])){
$studioIdError = " *This Field Cannot Be Empty";
$valid = false;
}
if(empty($_POST["studioName"])){
$studioNameError = " *This Field Cannot Be Empty";
$valid = false;
}
if(empty($_POST["studioAddress"])){
$studioAddressError = " *This Field Cannot Be Empty";
$valid = false;
}
if(empty($_POST["studioPCode"])){
$studioPCodeError = " *This Field Cannot Be Empty";
$valid = false;
}
if(empty($_POST["studioTelNo"])){
$studioTelNoError = " *This Field Cannot Be Empty";
$valid = false;
}
}
?>
To the above code I want to know if there is a way I can get the getstudio.php
page to run when $valid = true
. 对于上面的代码,我想知道是否有一种方法可以在
$valid = true
时运行getstudio.php
页面。
I cannot use the code include 'getstudio.php';
我不能使用
include 'getstudio.php';
的代码include 'getstudio.php';
because I want getstudio.php
to run on a seperate page. 因为我希望
getstudio.php
在一个单独的页面上运行。
I cannot use the code header("Location:getstudio.php");exit();
我不能使用代码
header("Location:getstudio.php");exit();
to run either because the values in the global array $_POST[]
is not sent to the PHP page like it is when the page is called directly from the form (Eg:- <form action = 'getstudio.php' method = 'post'>
). 运行要么是因为全局数组
$_POST[]
值没有发送到PHP页面,就像从表单直接调用页面时那样(例如: - <form action = 'getstudio.php' method = 'post'>
)。
Is there a way of running the getstudio.php
page while sending the values stored in the $_POST[]
array to the getstudio.php
page when $valid = true
? 当
$valid = true
时,有没有办法运行getstudio.php
页面,同时将存储在$_POST[]
数组中的值发送到getstudio.php
页面?
You have a few options. 你有几个选择。
In any case don't forget to re-validate before acting upon user data, unless absolutely sure the data can't be affected by a user (in the list above, only when storing data in a session, and still not recommended). 在任何情况下都不要忘记在对用户数据采取行动之前重新验证,除非绝对确定数据不会受到用户的影响(在上面的列表中,仅在会话中存储数据时,仍然不推荐)。 Then, if validating again anyways, why bother with a separate page?
然后,如果再次验证,为什么还要单独使用一个页面呢?
So as i understand you want to send Data first Addstudio.php than Getstudio.php but you want second one with redirection. 所以据我所知你想先发送数据Addstudio.php比Getstudio.php,但你想要第二个重定向。
Make your HTML like this : 像这样制作你的HTML:
<form action="getstudio.php" method="POST">
<!-- etc -->
</form>
Make your Javascript like this (include jQuery): 使你的Javascript像这样(包括jQuery):
$(".your-form-class").on('submit',function(evt){
evt.preventDefault();
var instance = $(this);
var formData = new FormData($(".your-form-class")[0]);
$.ajax({
type:"POST",
data:formData,
url:"addstudio.php",
success:function(response){
instance.submit();
}
});
});
Here is your current page 这是您当前的页面
<?php
$_POST["studioName"] = 'testdata';
$valid = true;
if(empty($_POST["studioName"])){
$studioNameError = " *This Field Cannot Be Empty";
$valid = false;
}
// send data to getstudio.php if valid
if($valid){ ?>
<form method="post" action="getstudio.php" id="senddata">
<input type="hidden" name="postdata" value="<?php print_r($_POST); ?>">
</form>
<script>
document.getElementById("senddata").submit();
</script>
<?php } ?>
Here is your getstudio.php file to receive validated post data 这是您的getstudio.php文件,用于接收经过验证的帖子数据
<?php
echo '<pre>';
print_r($_POST['postdata']);
die;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.