Moiz Mansoor Ali Moiz Mansoor Ali - 8 days ago 7
PHP Question

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
.

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
$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
.

I cannot use the code
include 'getstudio.php';
because I want
getstudio.php
to run on a seperate page.

I cannot use the code
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'>
).

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
?

Answer

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

<?php
  echo '<pre>';

  print_r($_POST['postdata']);

  die;