0

I have a php file A.php contained a form called formA .

The formA get the result and then post and store into the MYSQL through php & SQL & traditional HTML form posting method .

In one of the field in formA ,I want to post this value $amountto another php file B.php .It is because there is a hidden form in B.php .I tried to post this with php-curl method for further action .

In the B.php , I tried to test whether I can get the $amount successfully.

Both print_r($_POST); print_r($_GET); show Array ( ) Array ( ) ,which means that fail to get the amount

Here is my code :

A.php:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  // Validate amount
    $input_amount = trim($_POST["amount"]);
    if (empty($input_amount)) {
        $amount_err = "Please enter the amount.";
    } elseif (!ctype_digit($input_amount)) {
        $amount_err = 'Please enter a positive integer value.';
    } else {
        $amount = $input_amount;
    }
//storing to db
 if (empty($CName_err) && empty($Address_err) && empty($amount_err) && empty($Phone_err)) {
        // Prepare an insert statement
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO donation (CName, Address, Phone, Amount ,Ticket, Purpose) VALUES (?, ?, ?, ? ,?, ?)";

        $q = $pdo->prepare($sql);
        $q->execute(array($CName, $Address, $Phone, $amount ,$Ticket ,$Purpose));
        Database::disconnect();



        //curl part start:  post the `amount`
        $ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"B.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "amount=amount,input_amount=input_amount");




// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);



    header("Location: B.php");
}

}
?>

//Form A -the field that get the `amount` value 

    <div class="form-group <?php echo (!empty($amount_err)) ? 'has-error' : ''; ?>">                             
                                 <label>* Donation amount</label>                    
               <input list="amount" name="amount"  multiple class="form-control"> 
   <datalist id="amount" >
    <option value="100">
    <option value="300">
    <option value="500">
    <option value="1000">
  </datalist>  
                     <span class="help-block"><?php echo $amount_err; ?></span>
                        </div>
....other fields. .... 

B.php:

<?php
print_r($_POST);
print_r($_GET);
    $amount = null;
    if ( !empty($_GET['amount'])) {
        $amount = $_REQUEST['amount'];
    }
     ...later  action....

?>

How to tackle this problem ?

moon
  • 9
  • 5
  • 1
    If you do a cURL call to a URL and then redirect to the same URL, you won't get the data you send through cURL. The cURL call and your redirect are two totally separate requests. – M. Eriksson Jun 29 '18 at 05:11
  • It cannot be done in this way as said by @MagnusEriksson – Vamsi Krishna Jun 29 '18 at 05:13
  • If you want to post data to one url, handle the data (store in database or whatever) and then pass some values to a new page, you an store that data in a session, which you then can use to populate the form on the new page. – M. Eriksson Jun 29 '18 at 05:14
  • @MagnusEriksson No, they are in two URL . the db I post and the var I passed – moon Jun 29 '18 at 06:09
  • @MagnusEriksson Yes , I recalled my memory . I can store it in a session .But is it still possible to do it in the method of `cURL`? Which one is easier ? Would you like to build the sample code for this – moon Jun 29 '18 at 06:10
  • _"No, they are in two URL"_ - Not sure what you answered, or even mean, by that. If you want to pass a variable together with the user redirect, you can't use cURL. When you make a cURL request, then it's your server that does the request, not the users client so when you then redirect the user, it's not only a new request, it also comes from a totally different client so those request has absolutely _nothing_ in common. Drop the cURL path since it's the wrong path. Store the value in a session and use that after redirect the user. That's absolutely the simplest way of doing it. – M. Eriksson Jun 29 '18 at 06:23

1 Answers1

0

Your CURLOPT_POSTFIELDS value is incorrectly formatted, and does not contain the values you're expecting. The encoding of your values needs to be in what's known as application/x-www-form-urlencoded. Essentially, variables separated by &.

David Walsh has a terrific example here.

Your string should look something like this. "amount=".$amount."&input_amount=".$input_amount

aholmes
  • 458
  • 9
  • 20
  • That's the least of the OP's issue since the cURL-call shouldn't even be there in the first place. – M. Eriksson Jun 29 '18 at 05:15
  • hey bro , any easy coding solutions to tackle my case ? – moon Jun 29 '18 at 06:12
  • @MagnusEriksson his overall goal is not clear, and architecting the appropriate solution was not part of his question. Sending a request while handling a request is doable, regardless of the wisdom of doing so. – aholmes Jun 29 '18 at 06:46
  • @moon I strongly suggest trying to grok David Walsh's code. It has the information to help you out. – aholmes Jun 29 '18 at 06:47
  • @Magnus Eriksson oops - I didn't see the overflow code block. You are right, though I'm still not certain what's being asked. – aholmes Jun 29 '18 at 06:50
  • As I read it, the OP has a form that get's posted to a page. That page handles the posted data. After that's done, the OP want's to redirect the user to another page but also post some data to it to be used on that page. Since redirects are using GET, the OP is first trying to post the data using cURL, then redirect the user to the same page and use the data that was posted in the cURL request, which obviously won't work. – M. Eriksson Jun 29 '18 at 07:05