A problem with processing data from html form with PHP - php

I'm just about to learn php and that is my first try to process data from an html form. I was writing this php code using some articles from the Internet and it seems to be correct but after entering all the data and than pressing "submit" nothing is happening. I mean just a white browser screen.
Here is my code:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
<form method="post" action="form__2.php">
<label for="surname">Surname</label>
<input type="text" name="surname" placeholder="Johnson" required> <br>
<label for="name">Name</label>
<input type="text" name="name" placeholder="Carl" required> <br>
<label>Gender:</label>
<label for="male">M</label>
<input type="radio" name="male" checked>
<label for="female">F</label>
<input type="radio" name="female"> <br>
<label for="education">Education:</label>
<select id="education" name="education" required>
<option value="less-basic">Less than basic</option>
<option value="basic">Basic</option>
<option value="mid-school">Middle-school</option>
<option value="high-school">High-school</option>
<option value="bachelor">Bachelor's or equivalent</option>
<option value="master">Master's or equivalent</option>
<option value="doctor">Doctorate or equivalent</option>
</select> <br>
<label for="courses">Enroll in courses</label>
<input type="checkbox" name="courses" required> <br>
<label>Was you dealing with our company later?:</label>
<label>Yes</label>
<input type="radio" name="company" value="yes" checked>
<label>No</label>
<input type="radio" name="company" value="no"> <br>
<input type="submit" name="submit">
</form>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
<?
if ( count($_GET) > 0 ) // checking if the form sends any data
{
$name = htmlspecialchars($_GET['name']); // getting name from GET-values
$surname = htmlspecialchars($_GET['surname']); // getting surname from GET-values
if ( strlen($_GET['name']) >= 1 && strlen($_GET['surname']) >= 1) // if the name/surname has more than 1 letter, then..
{
echo "Dear " . $surname . " " . $name . ". We are great to have you signed for our courses. We are hoping for "; // msg
if ( $_POST['company_yes']=='yes' ) // if radiobutton is checked
{
echo "continuing our partnership."; // msg
} else
{
echo "our future partnership."; // msg
}
} else
{
echo "Your name is too short";
}
}
?>
</body>
</html>

You didn't check if a Post happened, and why call to GET request if you send a post request?
A GET request is data that passes through the URL -> https://www.something.net?data=hi
$_GET['data'] will give "hi" value.
POST is data that is passed mostly through forms with method="POST"
To get the data of the form change the form__2.php to the following:
<?php
if(isset($_POST['submit']){ //the name of the submit button
echo $_POST['education'];
echo $_POST['female'];
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
</body>
</html>

Related

Why HTML form isn't sending any data

I am trying to get data with php, so here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" required><br>
<input type="submit" value="Submit" name="submit"><br>
</form>
<?php
print_r($_POST);
if(isset($_POST['submit']))
{
print_r($_FILES);
}
?>
</body>
</html>
It always prints an empty array, meaning $_POST is always empty. Could you please help me
Note: it does receive a data if you submit only the file (name is empty)

php calculation keeps getting the answer as 0

this is the index.php file
if(isset($_POST['submit']))
{
echo header('Location:answer.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<form method="post">
<div class="form-group">
<label> Insert P value </label>
<input type="text" name="p" placeholder="please insert your P value">
</div>
<br>
<div class="form-group">
<label> Insert R value </label>
<input type="text" name="r" placeholder ="please insert your R value ">
</div>
<br>
<div class="form-group">
<label> Insert your N value </label>
<input type="text" name="n" placeholder=" please insert your N value">
</div>
<br>
<button type="submit" name="submit" > Submit </button>
</body>
</html>
this is the answer.php file
<?php
$p=isset($_POST['p'])?$_POST['p']: " ";
$r=isset($_POST['r'])?$_POST['r']:" ";
$n=isset($_POST['n'])?$_POST['n']: " ";
$si=(int)$p*(int)$r*(int)$n/100;
echo "Hello this is the final answer ".$si;
?>
i want the answer to be displayed in another page that is answer.php but whatever number i am inserting i keep getting the answer as 0. Please help and thank you.
You need to make your form post directly to answer.php. By making it post to itself and then redirecting you're losing all the submitted data - it's sent to your index page and then not transmitted again to the answer page. The redirect causes a separate GET request and doesn't contain any of the submitted data.
The other alternative is to move all the logic into index.php and not bother with a separate script at all.
From what I saw from your code. Inside the form tag, you need to add action='answer.php', this is a direct link to the page you are requesting data from, in this case answer.php.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<form method="post" action="answer.php">
<div class="form-group">
<label> Insert P value </label>
<input type="text" name="p" placeholder="please insert your P value">
</div>
<div class="form-group">
<label> Insert R value </label>
<input type="text" name="r" placeholder ="please insert your R value ">
</div>
<br>
<div class="form-group">
<label> Insert your N value </label>
<input type="text" name="n" placeholder=" please insert your N value">
</div>
<button type="submit" name="submit" > Submit </button>
</body>
</html>

PHP isset function is not working to print data

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form method="POST">
<input type="text" name="text">
<input type="button" value="Check" name="submit">
</form>
<p>
<?php
if (isset($_POST['submit'])){
$a=$_POST['text'];
echo "You have written" . $a;
}
?></p>
</body>
</html>
I am using PHP isset function here in this html form, but don't know why it is not echoing anything. It should print the text written inside the input box. Please help me why this is not working?
To submit the form, you need to use submit type for button, not button:
<input type="submit" value="Check" name="submit">
Tested, changing the type resolves your issue.

HTML 5 Bug Input Type Date POST Form

<?php
if(isset($_POST['VACA_INI'])){echo "<br>" . $_POST['VACA_INI'];}
else{echo "<br> .... NO POST VACA_INI .....";}
if(isset($_POST['VACA_FIN'])){echo "<br>" . $_POST['VACA_FIN'];}
else{echo "<br> .... NO POST VACA_FIN .....";}
if(isset($_POST['aaa'])){echo "<br>" . $_POST['aaa'];}
else{echo "<br> .... No he recibido el aaa .....";}
?>
<!doctype html>
<html class="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>....</title>
</head>
<body>
<form name="Vacas" id="Vacas" method="post" action="">
<input type="text" id="aaa" name="aaa" value="aaa">
<input type="date" id="VACA_INI" required value="2018-02-02" ></div>
<input type="date" id="VACA_FIN" value="2018-02-02" required >
<button id="Enviar" name="Enviar" type="submit" class="btn btn-primary">Enviar</button>
</form>
</body>
</html>
Return:
.... NO POST VACA_INI .....
.... NO POST VACA_FIN .....
aaa
chrome: Versión 63.0.3239.132 (Build oficial) (64 bits)
Input don't have the corresponding name VACA_INI and VACA_FIN.
Also remove the </div> closing tag.
<input type="date" id="VACA_INI" name="VACA_INI" required value="2018-02-02" >
<input type="date" id="VACA_FIN" name="VACA_FIN" value="2018-02-02" required >

Form always submits as GET even with POST method

I am having a problem with my form submission. The code is below. When I submit, it is submitting as GET and not as POST, even though my method is post. I have tried method="POST" and method="post". What am I missing here?
<!doctype html>
<html>
<head>
<title>Test</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<?php
echo "POST: ";
print_r($_POST);
echo " GET :";
print_r($_GET);
?>
<form role="form" name='test' method=“post” action="test2.php">
<label for="name">Name</label>
<input name="name" type="text" />
<input type="submit" name="submit" />
</form>
</div>
</body>

Categories