How to submit form after calling curl post


Joined
Jan 1, 2023
Messages
1
Reaction score
0
I am not exactly sure how to word this question so bare with me but basically i am creating site for users where they will be able to upload 'test.php' file to there own server and it will display a support form. Now my problem is whenever they submit form, I can't get the form input values. Whenever I use $_POST['subject'] it returns empty.

Here is 'test.php' file:
PHP:
<?php
header("Cache-Control: max-age=0, private, no-cache, no-store, must-revalidate");
define("APIKEY","APIKEY-2893-0889-2351-2804");
define("FILE_NAME","ex1.php");
define("GET_PARAM","online");


function getdomain(){

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://example.com/api');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
  curl_setopt($ch, CURLOPT_TIMEOUT, 400);
  $domain = curl_exec($ch);
  if(filter_var(gethostbyname($domain), FILTER_VALIDATE_IP)){
      return $domain;
  }
  else{
      $domain = 'example.com/api';
      return $domain;
  }

}


function sendRequest($post_data) {
    $CURLOPT_URL = 'https://' . getdomain()."/".FILE_NAME;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $CURLOPT_URL);
    curl_setopt($ch, CURLOPT_POST,true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 400);
    $x = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    if ($x == '403'){
        return header('HTTP/1.0 403 Forbidden');
    }elseif($x == "404"){
        return header('HTTP/1.0 404 Not Found');
    }else{
        return $x;
    }
}

if (isset($_GET[GET_PARAM])) {
    $token = $_GET[GET_PARAM];
    $post_data = [
        'apikey' => APIKEY,
        'token'  => $token,   
    ];
    echo sendRequest($post_data);

}else{
    header('HTTP/1.0 403 Forbidden', true, 404);
    exit();
}
?>

Here is the form file 'ex1.php':
Code:
<form action="" method="POST">
  <div class="form-group">
    <label for="">Subject</label>
    <input type="text" name="subject" placeholder="Enter Subject" required>
  </div>
  <div class="form-group">
    <label for="">Message</label>
    <textarea name="message" placeholder="Describe issue..." required></textarea>
  </div>     
</form>
 
Ad

Advertisements

Joined
Jan 8, 2023
Messages
21
Reaction score
1
It looks like the issue is that the form's action attribute is set to an empty string, so when the form is submitted, it will be sent to the same page. However, it looks like the form processing code is written in a separate PHP file (ex1.php), so the form submission will not be able to access the $_POST variable in ex1.php.

To fix this issue, you will need to specify the correct action for the form. In this case, you will need to set the action attribute to the URL of the ex1.php file. For example:
PHP:
<form action="https://example.com/ex1.php" method="POST">
  <!-- form fields go here -->
</form>

Alternatively, you could include the form processing code in the same PHP file as the form, and use the $_POST variable directly in that file.
 
Ad

Advertisements

Joined
Jan 30, 2023
Messages
108
Reaction score
8
It looks like the issue is that you're trying to access the values submitted by the form with $_POST, but the form's method is not set to POST. Change the method to POST in the form tag:

Code:
<form action="" method="POST">

That should fix the issue and you should be able to access the values submitted by the form with $_POST.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top