0

Consider the following code and let me know how to play with the global post in the PHP. Although I have run this code by duplicating the code at two places I need to access the already written code.

I have a file abc.php:

if (isset($_POST['test'])) {
    return 'hello test1';
} elseif(isset($_POST['test2'])){
    return 'hello test2';
} else {
    return "test3";
}

Now I have another file efg.php:

if (isset($_GET['hello'])) {
    //Here, I need content from abc.php 
}
/* More code... */

How do I pass the POST from one page to another?

Gal Silberman
  • 3,756
  • 4
  • 31
  • 58
bansal
  • 99
  • 8
  • 1
    Welcome to Stack Overflow! Please be a bit more specific when asking a question: *What is your actual question?* / *What do you expect?* / *What error do you get?* **For Help take a look at "[How to ask](https://stackoverflow.com/help/how-to-ask)"** – Hille Apr 03 '18 at 06:58
  • This is a low quality question. tried to clear the intend. – Gal Silberman Apr 03 '18 at 10:11

3 Answers3

0
include "abc.php"; 
require "abc.php"; 
require_once "abc.php"; 

All of these will work in PHP to bring abc.php into the other file! Make sure you use correct path.

Perhaps

$file = $_SERVER['DOCUMENT_ROOT'] . "/folder/abc.php"; 
if(file_exists($file) !== false){
 require $file; 
}

Depends on how you have it setup!

Use echo instead of return on abc.php

Nerdi.org
  • 895
  • 6
  • 13
0

You should not use return in the global scope, but you can actually get the returned value like this:

if (isset($_GET['hello'])) {
    $value = include 'abc.php';
}

read more about return in the official documentation: http://php.net/manual/en/function.return.php

xander
  • 1,780
  • 9
  • 16
  • only your reply seems to be relevant to my query. But there is one more catch i.e. abc.php has if conditions which is based on checking the $_POST. So let me know how to check that condition while doing in the way you described. – bansal Apr 03 '18 at 08:27
  • I just noticed you have mixed GET and POST variables in your question, is that really the case (you can actually send GET parameters to a POST request)? I don't know what your php script receives and how, maybe explain that a little bit more what you need. – xander Apr 03 '18 at 08:43
  • actually abc.php is file for fetching the data from the database.This file I'm using for ajax call. But know I want the same piece of code to be called in efg.php file.This file need to get the same data data from databse but this time at the run time so no ajax is there. – bansal Apr 03 '18 at 09:52
  • @Abhi PHP doesn't know or care about an AJAX call, but where are your `$_POST` variables coming from if not send from your AJAX call? Noting is stopping you from overwriting those variables manually, but maybe you should change your design and don't use `$_POST` at all in your script if you need multiple ways to use that file. There are many solutions. – xander Apr 03 '18 at 11:25
0

I'm assuming that what you need is to request another PHP page in your script and pass a test data to it using POST method.

The easiest way to do it is using cURL

// full URL to your PHP script
$url = 'http://example.com/abc.php';

// what post fields?
$fields = array(
   'test' => '1st Value',
   'test2' => '2nd Value',
);

// encode your post data
$postvars = http_build_query($fields);

// open connection
$ch = curl_init();

// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

// execute post
$result = curl_exec($ch);

// close connection
curl_close($ch);

Credit goes to: https://stackoverflow.com/a/1217836/266076

Hassan Al-Jeshi
  • 1,450
  • 2
  • 15
  • 20