How to make the value of the input appear in json?

0

So I'm having doubts here, would it be possible to make the input fields I have play what I typed in a JSON?

EX: This would be the form

        <form role="form" method="post" id="searches" accept-charset="utf-8" action="<?php bloginfo('template_url');?>/form.php" novalidate >

                <label class="input-da-serach-1" for="de">De <input type="text" id="de" name="de" placeholder="Cidade:" required ></label>

                <label class="input-da-serach-1" for="searches=[to_airports]">Para <input type="text" id="searches=[to_airports]" name="searches=[to_airports]" placeholder="Cidade:" required ></label>

                <label class="input-da-serach-2" for="date_go">Ida <input type="date"  name="go_date" min_days="1" max_days="365" required></label>

                <label class="input-da-serach-2" for="data_back">Volta (opcional)<input type="date" name="search[back_date]" min_days="1" max_days="365" ></label>

                <label class="input-da-serach-2">Passageiros <input type="text" name="" placeholder="01" ></label>

                <button type="submit" id="searchsubmit" value="Procurar">Procurar</button>

            </form>

he would have to get the values typed in - > (de) - > (to)

and playing in JSON

class MCrypt
{
  private $key;
  public function __construct($token) {
    $this->key = $token;
  }

  function crypt($message, $url_encode = true) {
    $iv = mcrypt_create_iv(16, MCRYPT_RAND);
    $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $message, MCRYPT_MODE_CBC, $iv);
    $encrypted = base64_encode($encrypted) .'|'. base64_encode($iv);
    if ($url_encode) return urlencode($encrypted);
    else return $encrypted;
  }
  function encrypt($message, $url_encode = true) {
    return $this->crypt($message, $url_encode);
  }
  function decrypt($encrypted_message, $url_decode = true) {
    if ($url_decode) $tokens = urldecode(trim($encrypted_message));
    else $tokens = trim($encrypted_message);
    $tokens = explode('|', $tokens);
    if (count($tokens) == 2) {
      $crypt = base64_decode($tokens[0]);
      $iv = base64_decode($tokens[1]);
      $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $crypt, MCRYPT_MODE_CBC, $iv);
      echo preg_replace('[x00-x1Fx80-xFF]', '', $decrypted);      
    }
    else return false;
  }
}

$url = "******";
$crypt = new MCrypt('*********');

$encrypted_data = $crypt->encrypt('

{"searches":
["amigo", "multiplus", "tudo_azul"],
"type":"award_flight_money_tax",
"includes":"none",
"from_airports":"**jogar o valor do de aqui**",
"to_airports":"**jogar o valor do para aqui**",
"go_date":"2018-03-22",
"go_date_flexibility":"0",
"back_date":"2018-03-28",
"back_date_flexibility":"0",
"adults":"1",
"children":"0",
"babies":"0",
"cabin":"E"
}');

?>

urls: <?php 

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,"data={$encrypted_data}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
echo ($output);

 ?>

The place is in bold that should take the value of the input and play in the indicated place, thank you already.

I have tried many things but always the error, and I am being forced to use this method without changing this area.

{"searches":
["amigo", "multiplus", "tudo_azul"],
"type":"award_flight_money_tax",
"includes":"none",
"from_airports":"**jogar o valor do de aqui**",
"to_airports":"**jogar o valor do para aqui**",
"go_date":"2018-03-22",
"go_date_flexibility":"0",
"back_date":"2018-03-28",
"back_date_flexibility":"0",
"adults":"1",
"children":"0",
"babies":"0",
"cabin":"E"
}');
    
asked by anonymous 20.03.2018 / 02:56

2 answers

0

First, retrieve the value of the fields with the POST method.

$de = $_POST['de']; $para = $_POST['para'];

Next, decode the "text" of json with the function json_decode

$decode = json_decode($string_com_o_json, true); The "true" parameter converts the string to associative array.

Next, you can override the value of the key "from_airports" and "to_airports" by setting them again: $decode['from_airports'] = $de; $decode['to_airports'] = $para;

Then, turn the already changed variable into json again:

$decode = json_encode($decode);

And then use the $ decode variable any way you prefer. I hope I have been helpful and clear.

    
20.03.2018 / 03:10
-1

I did it! After some clarification from @Haroldo Torres, I changed some things.

Follow the code ready for one day if someone appears with the same doubt.

This area will get your inputs

$de = $_POST['de']; 
$para = $_POST['para'];
$date_in = $_POST['date_in'];
$date_out = $_POST['date_out'];

$decode = json_decode($string_com_o_json, true);

Here you will create a json to be displayed

$decode['searches'] = "amigo";
$decode['type'] = "award_flight_money_tax";
$decode['includes'] = "none";
$decode['from_airports'] = $de;
$decode['to_airports'] = $para;
$decode['go_date'] = $date_in ;
$decode['go_date_flexibility'] = "0";
$decode['back_date'] = $date_out ;
$decode['back_date_flexibility'] = "0";
$decode['adults'] = "1";
$decode['children'] = "0";
$decode['babies'] = "0";
$decode['Cabin'] = "E";

$decode = json_encode($decode);

Here he does the json show after coding as a whole

$encrypted_data = $crypt->encrypt ( $decode );
  

Note: the places where you do not have the $ in the json sample and what is by default of the form and you do not want the input of the form change.

I hope I have explained well.

    
20.03.2018 / 17:00