How to pass value from formfield into executable JSON

34 Views Asked by At

I would like to fill in the textfield on the html and when I click the button, the value of that textfield should automatically be passed onto the value for "name" under CURLOPT_POSTFIELDS. How do I do that?

Here is my code:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.example.com',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
  "name": <<<THIS IS WHERE VALUE FROM FORM SHOULD GO>>>>  
  "price_amount": 1000,
  "price_currency": "usd",
  "order_id": "ID",
}

',
  CURLOPT_HTTPHEADER => array(
    'x-api-key: <api key>',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>



 
<!DOCTYPE html>  
<html>  
<head>  
         
         
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,700">
  <title></title>  
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
      </head>  
      <body>  
           <div class="container" style="width:500px;">  
                <h4 align="">User Details</h4><br />                 
                <form method="post">  
                     
                     <label>Name</label>  
                     <input type="text" name="name" class="form-control" /><br />
                     <br /><br />                       
                     <input type="submit" name="submit" value="submit" class="btn btn-info" /><br />                      
                </form>  
           </div>  
           <br />  
      </body>  
 </html>

I want the value from the text form field in html to be added in the executing JSON array.

0

There are 0 best solutions below