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>
 
Joined
Jan 8, 2023
Messages
27
Reaction score
2
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.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
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.
 
Joined
Jun 16, 2023
Messages
1
Reaction score
0
There are a few possible reasons why you might be getting an empty value for $_POST['subject'].

  • The form might not be submitting correctly. Make sure that the form is properly configured and that the submit button is actually submitting the form data.
  • The form might be using a different method than POST. The $_POST variable is only populated for forms that use the POST method. If the form is using a different method, such as GET, then the $_POST variable will be empty.
  • The form might be being blocked by a firewall or filter. Firewalls and filters can sometimes block form submissions. If you are using a firewall or filter, make sure that it is configured to allow form submissions.
If you have checked all of these possibilities and you are still getting an empty value for $_POST['subject'], then you might need to consult with a web development expert to help you troubleshoot the issue.

Here are some additional tips for troubleshooting form submissions:

  • Use a form checker tool. There are a number of form checker tools available online that can help you identify and fix common form submission errors.
  • Test the form on multiple browsers. Make sure that the form works correctly in all of the major browsers, such as Chrome, Firefox, and Safari.
  • Test the form on different devices. Make sure that the form works correctly on different devices, such as desktop computers, laptops, and mobile phones.
By following these tips, you can help to ensure that your forms are submitting correctly and that you are able to get the data that you need.

Jennifer-
developer and writer at https://sites.google.com/site/bestessaywritingservicereview/
 
Joined
Jul 4, 2023
Messages
475
Reaction score
58
Show which of the given code files you are trying to get the access to $_POST['subject'].

For example code for ex1.php should be looks more like that, if you left attribute "action" as blank:

PHP:
<?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $subject = $_POST['subject'] ?? null;
    $message = $_POST['message'] ?? null;

    if ($subject && $message) {
      // ... your code
    }
  } else {
?>
<form method="POST">
  <div class="form-group">
    <label for="subject">Subject</label>
    <input type="text" id="subject" name="subject" placeholder="Enter Subject" required>
  </div>
  <div class="form-group">
    <label for="message">Message</label>
    <textarea id="message" name="message" placeholder="Describe issue..." required></textarea>
  </div>
  <div class="form-group">
    <button type="submit">Submit</button>
  </div> 
</form>
<?php } ?>


BTW, when the form code has been "called" from the ex1.php file

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

<form action="ex1.php" method="POST">

means the same, because web browser use as default for attribute "action" the name of the php file
 
Last edited:

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

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top