V
V
VAZSOFT2019-09-28 20:55:53
PHP
VAZSOFT, 2019-09-28 20:55:53

Save POST request to TXT?

Tell me, in general, there is a site
in it, when sending a post request through the chrome console, tokens are visible in the post request.
so you need to save these tokens in txt
in general, such an order
I went to the site sent a post request, it was saved in txt
how to implement. ??

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Anton Mashletov, 2018-05-30
@mashletov

if (isset($_POST['num1']) && isset($_POST['num2']) && isset($_POST['operator'])) {
  $num1 = intval($_POST['num1']);
  $num2 = intval($_POST['num2']);
  $operator = $_POST['operator'];

  switch ($operator) {
     case '+':
         echo $num1 + $num2;
         break;
     case '-':
         echo $num1 - $num2;
         break;
  }
   
} else {
  echo "Что-то не передано!";
}

M
Maxim Timofeev, 2018-05-30
@webinar

1. Since php 5.5 it is possible as you did, but for earlier versions empty should be a variable, not an expression, so depending on the version of php, it might be worth a try
2. You are not correctly converting data to integer

$num1 = int($_POST['num1']); // не верно
$num1 = (int) $_POST['num1'];  //верно

R
Roman Terekhin, 2018-05-30
@RomaZveR

This is specifically for your piece of code. Ideally, you need to look at everything and rewrite from scratch.

<?php

$num1 = intval(isset($_POST['num1']) ? $_POST['num1'] : 0);
$num2 = intval(isset($_POST['num2']) ? $_POST['num2'] : 0);

if (empty($num1) || empty($num2)) {
  echo "Что-то не передано!";
}

?>

A
Alexander Toropov, 2018-05-30
@nefone

I made a variant with the output of a message under the field that was not filled in:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $require = ['num1', 'num2']; //поля которые надо заполнить.
    $errors[] = ''; //массив в котором сохраним ошибки
    
    foreach($require as $key) {
        if(empty($_POST[$key])) {
            $errors[$key] = 'Это поле надо заполнить!';
        }
    }
}
?>

<form action='calc.php' method="post">
      <label>Число 1:</label>
      <br />
      <input name='num1' type='text' />
      <?php
        $msg = isset($errors['num1']) ? "Введите первое число" : "";
        echo $msg;
      ?>
      <br />
      <label>Оператор: </label>
      <br />
    <label for="operator">
        <select name="operator" id="operator">
          <option value="+">+</option>
          <option value="-">-</option>
        </select>
      <br />
      <label>Число 2: </label>
      <br />
      <input name='num2' type='text' />
      <?php
        $msg = isset($errors['num2']) ? "Введите первое число" : "";
        echo $msg;
      ?>
      <br />
      <br />
      <input type='submit' value='Считать'>
    </form>
    <br />
    <br />
<?php
    if (count($errors) > 0) {
        echo 'Заполните все поля!';
    }
?>

First, we check if the form was submitted, and then we check if all the required fields from the form are filled in.

@
@salomatin, 2019-09-28
_

Collect data from $_POST into variables and save to file
$name = $_POST['name'];
$text = $_POST['text'];
$filed = "save.txt";
$rez = "Name: ".$name." Text: ".$text;
file_put_contents($filed, $rez);
You can send a request to a php file via AJAX

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question