How to make input values appear in json?

Asked

Viewed 361 times

0

then I am doubtful here, it would 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 take the values typed in -> (de) -> (for)

and play on 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 location is in bold that should take the value of the input and play in the indicated location, I thank you..

tried many things but always of error, and I am being obliged to use this method without can change 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"
}');

2 answers

0


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

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

Then decode the json’s "text" with the function json_decode

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

Then you can replace the key value "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 the way you prefer. I hope I’ve been helpful and clear.

-1

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

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

This area will take 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 shown

$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 show of json after encoding by a whole

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

Obs: the places where there is no $ in the showing of json and what is by default the form and you do not want the input of the form amend it.

I hope I explained it well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.