0

A HTML form has an input field. The user will enter the value in the HTML form and based on the value further steps will be taken.

The input field accepts characters and that is what is being entered . In the HTML form it is defined correctly.

The problem is the value entered is NOT getting stored in the variable in PHP. This is really simple but for some reason I am not able to catch the problem.

Please note this is NOT the actual code that will be deployed. I am taking care of SQL injection in the deployed code. Just need to figure out what is going wrong here.

I have tried displaying the value entered in the form. It just returns NULL. What am I doing wrong here?

<?php
$host = 'localhost';
$dbname = 'dbname';
$username = 'user';
$password = 'password';


$mysqli = new mysqli("localhost", "user", "password", "dbname");

if($mysqli->connect_error)
{
die("$mysqli->connect_errno: $mysqli->connect_error");
}
$field1=$_POST['field1'];

var_dump($field1);
?>

The HTML code:

<!DOCTYPE html>
<html> 
<body>
<basefont face = "sans-serif" size = "2" color = "#ff0000">
<form id="queryForm" method="POST" action="/test.php">
<div class="row">
<fieldset class="hm-fieldset">
<legend class="hm-legend">Enter Query Criteria:</legend>
<div class="col">
<span style="font-family:sans-serif">
Field1<br> <input type="text" id="field1" style="height:10px; 
width:105px"> </span>
</div>
<button>Submit</button>
</fieldset>
</div>
</form>
</body>
</html>
David D'Lima
  • 105
  • 9

2 Answers2

4

You need to give your inputs a name attribute for them to work in PHP (see the manual). Try changing it to this:

<input type="text" id="field1" name="field1" style="height:10px; width:105px">

See also this excellent Q&A on the difference between id and name.

Nick
  • 118,076
  • 20
  • 42
  • 73
  • adding the name tag did not help. Still the same issue. – David D'Lima Aug 13 '19 at 06:41
  • @DavidD'Lima glad to hear it - what went wrong the first time? – Nick Aug 13 '19 at 07:00
  • I have no idea. I just logged out from cpanel and logged in again . Deleted the php file and placed it again. It probably does not make any sense but the bottomline is problem solved!!!! – David D'Lima Aug 13 '19 at 07:02
  • I did not unaccept. I was trying to put both yours and Nixon's answer but apparently only one can be marked as the correct answer. Anyways I have accepted yours as the correct answer. – David D'Lima Aug 13 '19 at 07:05
  • @DavidD'Lima yeah, unfortunately you can't accept more than one answer. – Nick Aug 13 '19 at 07:05
2

You have not mentioned the name attribute in HTML form. If you specify the name it attribute , it should work . Every thing else seems to be fine.

<span style="font-family:sans-serif">
Field1<br> <input type="text" name="field1" id="field1" style="height:10px; 
width:105px"> </span>

nXn
  • 774
  • 4
  • 13
  • I added the name attribute. Still the same issue. When I try to display the value it returns NULL. – David D'Lima Aug 13 '19 at 06:41
  • Is your PHP file in the same folder as your HTML code or is it in a different folder. Can you check on the path of the folder. – nXn Aug 13 '19 at 06:49
  • I have tried your code and have got the output as **string(1) "1"**. I have removed the db part from the code and it worked. – nXn Aug 13 '19 at 06:54
  • I made the changes as suggested and deleted the file from the folder and placed it again. It worked !!! Thank you! Not sure why it did not work the first time I made the changes as suggested by you. – David D'Lima Aug 13 '19 at 06:58