Why HTML form isn't sending any data - php

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)

Related

A problem with processing data from html form with 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>

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.

Mysql database inserting 'blank' data instead of actual data inputted by the user through php connections

I am Practicing a bit about MySQL Database connections through php. However, i am encountering a bit of a problem. Because of Inserting the Actual Username and Password that the user inputted into the MySQL Database itself, only blank rows, or a row containing empty (ps. Its not NULL, just empty) data is being inserted in it.
The following below is how I created the actual forms that the user has to input to.
<?php
session_start();
include("testconnect.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>Login Page</title>
</head>
<body>
<style>
#signup-box{
margin:auto;
width: 300px;
padding: 30px;
background-color: grey;
}
</style>
<div id="signup-box">
<form method="POST">
<input style="width:100%" type="text" name="Username"><br><br>
<input style="width:100%" type="password" name="Password"><br><br>
<input type="button" value="Submit">
</form>
</div>
</body>
</html>
And this is how i declared my php mysql connections.
<?php
$conn=mysqli_connect('localhost','root','','practice') or die ("unable to connect to database");
$username1=mysqli_real_escape_string($conn,$_POST['Username']);
$password1=mysqli_real_escape_string($conn,$_POST['Password']);
$query="insert into usertable values('$username1','$password1')";
mysqli_query($conn,$query);
I am hoping that someone here can help me with this slightly small problem of mine.
Ps. Already looked for solutions everywhere but none of the solutions offered seems to solve the problem.
Ps. Already checked the db connection but there seems to be no problem at all
If we suppose your Php code to insert into database is testconnect.php, You must provide an action in form tag. In your code the first thing before showing anything to you in browser, is inserting in SQL and then the form will show to you. The code you provided is like this:
<?php
session_start();
$conn=mysqli_connect('localhost','root','','practice') or die ("unable to connect to database");
$username1=mysqli_real_escape_string($conn,$_POST['Username']);
$password1=mysqli_real_escape_string($conn,$_POST['Password']);
$query="insert into usertable values('$username1','$password1')";
mysqli_query($conn,$query);
?>
<!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>Login Page</title>
</head>
<body>
<style>
#signup-box{
margin:auto;
width: 300px;
padding: 30px;
background-color: grey;
}
</style>
<div id="signup-box">
<form method="POST">
<input style="width:100%" type="text" name="Username"><br><br>
<input style="width:100%" type="password" name="Password"><br><br>
<input type="button" value="Submit">
</form>
</div>
</body>
</html>
So need change it to something like this:
<?php
session_start();
?>
<!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>Login Page</title>
</head>
<body>
<style>
#signup-box{
margin:auto;
width: 300px;
padding: 30px;
background-color: grey;
}
</style>
<div id="signup-box">
<form action="testconnect.php" method="POST">
<input style="width:100%" type="text" name="Username"><br><br>
<input style="width:100%" type="password" name="Password"><br><br>
<input type="button" value="Submit">
</form>
</div>
</body>
</html>

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>

How to turn off Ajax navigation in jquery mobile. Because I will to upload image

I have problem to turn off Ajax navigation using data-ajax="false" on your form definition
Where I to setting this.
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>textinput</title>
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css">
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
</head>
<body>
<form name="form1" method="post" action="PageUploadToMySQL2.php" data-ajax="false" enctype="multipart/form-data">
ชื่อรถ : <input type="text" name="txtcarNAME"><br>
ราคา: <input type="text" name="txtcarPRICE"><br>
Picture : <input type="file" name="filUpload"><br>
<input name="btnSubmit" type="submit" value="Submit">
</form>
</body>
is my code.
You can change the properties right before jQuery Mobile starts working.
Refer to this, take a look at the ajaxFormsEnabled property and ajaxLinksEnabled if you need: http://demos.jquerymobile.com/1.0a3/#docs/api/globalconfig.html

Categories