简体   繁体   中英

Cannot post the var to another php file successfully

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 $amount to 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); 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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM