Answer the question
In order to leave comments, you need to log in
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
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 "Что-то не передано!";
}
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']; //верно
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 "Что-то не передано!";
}
?>
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 'Заполните все поля!';
}
?>
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 questionAsk a Question
731 491 924 answers to any question