html form post gives empty array - php

I am trying to post data from html form (html code is inside index.php file) into add_student.php file which is supposed to just print $_POST array using print_r function. But there is always empty array on result.
index.php
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="stylesheet" href="css/font-awesome.css">
</head>
<body>
<div class="contact col-md-12" id="get_in_touch">
<div class="col-md-10 col-md-offset-1">
<div class="contact_title">
<h2>
Add a student
</h2>
</div>
<div class="contact_container">
<div class="contact_form col-md-8">
<form action="add_student.php" method="post">
<input name="student_name" type="text" placeholder="FULL NAME" class="input1"/>
<input name="student_email" type="text" placeholder="E-MAIL" class="input1"/>
<input name="password" type="text" placeholder="PASSWORD" class="input1"/>
<input name="major" type="text" placeholder="MAJOR" class="input1"/>
<input name="group" type="text" placeholder="GROUP NUMBER" class="input1"/>
<input name="submit" type="submit" value="ADD STUDENT"/>
</form>
</div>
</div>
</div>
</div>
</body>
add_student.php:
<?php
print_r($_POST);
?>
Why I always get empty array?

It seems that problem is in PHPStorm localhost port. When you just enter in browser the address of index.php it works perfectly. However when I run code from IDE it returns empty array.

Perhaps it will work if you use an include as the following:
include_once add_student.php;
And then use index.php as the form action. IF you later want to proceed to another page, add a header <url> at the bottom of the included script.
Make sure you check if the submit button has been pressed in the add_student.php script isset($_POST['submit'])
Data might get lost in the travel from to page

Related

Values not updating in database for registration page in php

I'm trying to make a registration page using PHP but the values are not being updated in the table and no errors are shown. I'm using a Mac for this. I have set up a XAMPP server, Have a folder named Lab in htdocs folder. Inside the Lab folder I have a file named register.php -
<?php
$db=mysqli_connect("localhost","root","","User Registration");
if(isset($_POST['submit'])){
session_start();
$name=mysqli_real_escape_string($db,$_POST['name']);
$email=mysqli_real_escape_string($db,$_POST['email']);
$password=mysqli_real_escape_string($db,$_POST['password']);
$password=md5($password);
$sql="INSERT INTO User(Name,Email,Password) VALUES('$name','$email','$password')";
mysqli_query($db,$sql);
$_SESSION['message']="You are now logged in";
}
?>
<html>
<head>
<meta charset="utf-8">
<base>
<link href="register.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'>
<link rel="shortcut icon" type="image/png" href="blogger.ico"/>
<title>Blog</title>
</head>
<body background="background.png">
<div class="topnav">
<a class="active" href="index.html">Home</a>
About
Log In
Register
</div>
<div id="login">
<form action="">
<h3>Register</h3>
<label for="name"><b>Name</b></label>
<br>
<input type="name" placeholder="Name" name="name" id = "name" required>
<br>
<br>
<label for="email"><b>Email</b></label>
<br>
<input type="email" placeholder="Email" name="email" id = "email" required>
<br>
<br>
<label for="email"><b>Password</b></label>
<br>
<input type="password" placeholder="Password" name="password" id="password" required>
<br>
<br>
<label for="password"><b>Re-enter Password</b></label>
<br>
<input type="password" placeholder="Password" name="password" required>
<br>
<p>
<input type="submit" value="Submit" id="submit">
</form>
</div>
</body>
This is my database structure -
Databse
Some modifications necessary:
1) You have not set any method for <form>, so default method is GET and you are trying to access variables with $_POST. So, please make <form method="post">
Explanation:
Case GET method:
<form method="get">
This is default method.
If no method is specified, this is the method.
But, not preferred (especially in registration/login) because all form submitted data is shown on url.
The variables submitted in the form will be accessible through:
$var = $_GET['variableName'];
Case POST method:
<form method="post">
The variables submitted in the form will be accessible through:
$var = $_POST['variableName'];
post method is secure one and variables submitted in this method are not shown on url.
This preferable method.
Now, you can choose exactly which method do you prefer.-
2) Also, PHP variables are case-sensitive. You are using
if(isset($_POST['submit'])){
And form element's name is not there. Make it name="submit"

Data entered in index.php cannot show on process.php

Was trying to follow a tuitorial online on how to submit data using html to mysql database.
I created 2 php files named index.php and process.php. What i wanted to show was what I typed in inside index.php will show on process.php when I clicked on the button "add employee". But nothing showed up.
Here is what's inside index.php
<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title>Add Employee</title>
</head>
<body>
<form method="post" action="">
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>Department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<input type="submit" value="Add Employee">
</form>
</body>
</html>
Here is a Screenshot of the form.
click here for index.php image
Wile here is what's inside the process.php
<?php
print_r($_POST);
This is what shows up
process.php image
Sorry a beginner at this. Hope you guys can help. Thank you!
Maybe you are missing the "action"?
<form method="post" action="/process.php">
You should specify what you need.Somethings like this:
<?php
print_r($_POST['first_name']);
Or use foreach loop to get full array value.
You can go through with fill up action tag so when you submit the form it will go to proccess.php page.
<form method="post" action="process.php">

PHP Post Method Error

I am using Post method in my website. In my index.html file is the following code:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Heater Login</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<form method="post" action="login.php" class="login">
<p>
<label for="login">Username:</label>
<input type="text" name="login" id="login" value="John Appleseed">
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" value="4815162342">
</p>
<p class="login-submit">
<button type="submit" class="login-button">Login</button>
</p>
</form>
</body>
</html>
And in my login.php file is the following code:
<?php
echo($_POST['login']);
echo($_POST['password']);
?>
When I open html file, enter my credentials, click submit button it goes to php file end there's nothing.
Can you change this line of code with the new one Hope it will work
<button type="submit" class="login-button">Login</button>
<input type="submit" name="submit" value="Login">
And last I want that you copy whole code from the stackoverflow and use this code.Some times space may create a problem.
This problem occurs when you directly open from its file location try opening file from server
like if you're using xampp open localhost and then try.

URL variable not being set in PHP when using jquery mobile

i have a few pages and when the input is submitted it will go to a php file which checks the url variable this could be executeeffects.php?option=blockuser
but when i go to the page the page just says "undefined" in the executeeffects php file it has a checking feature to check if a variable is set, it echos out one thing if it is or another thing if it isnt, however i didnt make it echo undefined so i dont no where that is coming from, the code is below
can someone tell me if this is a problem with jquery mobile or if im missing something please?
block.php
<!DOCTYPE html>
<html>
<head>
<title>Ryan Kelly</title>
<meta name="viewport" content="width=device-width, initial-scale=1 user-scalable=no">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script src="effects.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Ryan Kelly</h1>
</div><!-- /header -->
<div data-role="content">
<label for="basic">Username:</label>
<input type="text" name="name" id="basic" value="" />
<input type="text" name="name" id="blocked" value="" />
Submit
<?php include('listusers.php'); ?>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
executeeffects.php
<?php
if(isset($_GET['option'])){
echo 'something is set!';
} else {
echo 'nothing is set!';
}
?>
You Said "i have a few pages and when the input is submitted it will go to a php file which checks the url variable this could be executeeffects.php?option=blockuser"
This:
<input type="text" name="name" id="basic" value="" />
<input type="text" name="name" id="blocked" value="" />
Submit
will not submit the value of the inputs unless you are using some type of jQuery that you have not posted in which case this will need to be done differently.
to submit the value of the inputs you need to use a <form>

php/jquery-mobile: Second page not running validation on two page form

I'm new to php and am trying to make a very simple two page form. Any help or guidance would be very appreciated!
Problem:
The second page does not run error validation when coming to it from the first page.
Both pages run error validation correctly URL's entered directly
Setup:
Page 1 one is HTML. It POSTS to Page2.
Page 2 is php and the data from Page 1 is stored in an input field
Live example:
http://www.linplusg.com/page1.html
In IE the Page 2 URL after coming from Page 1 looks to be incorrect:
http://www.linplusg.com/page1.html#/page2.php
In FF and Chrome the URL looks fine but I think there's a flicker/refresh happening.
Does something else happen besides just opening the page when doing a POST to a page? Does something value get stored the new fields? Am I doing the form action wrong? Dazed and confused...
Thank you so much for any help or suggestions. I've been searching around for answers all night and my brain feels like jello. It seems like I'm missing something simple?!
Page1 Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="css/my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
</script>
<script>
$(document).ready(function(){
$("#initialForm").validate();
});
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-role="content">
<form id="initialForm" method="post" action="page2.php">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput3">
Zip Code
</label>
<input name="zip" id="zip" maxlength=5 value="" type="text" class="required" />
</fieldset>
</div>
<input type="submit" data-theme="e" value="Submit" />
</form>
</div>
</div>
</body>
</html>
Page 2 code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="css/my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
</script>
<script>
$(document).ready(function(){
$("#fullForm").validate();
});
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-role="content">
<form id="fullForm" method="get" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<input name="zip" id="zip" value="<?php echo $_POST["zip"]; ?>" type="" />
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput3">
First Name
</label>
<input name="first_name" id="first_name" placeholder="" value="" type="text" class="required"/>
</fieldset>
</div>
<input type="submit" data-theme="e" value="Submit" />
</form>
</div>
</div>
</body>
</html>
To prevent this you need to specify data-ajax="false" in the form element in page1.
<form id="initialForm" method="post" action="page2.php" data-ajax="false">
see http://jquerymobile.com/test/docs/forms/forms-sample.html for more information.
Without the PHP code, I can just guess, but maybe it is because in the second form you have a method="get" instead of method="post", and in PHP you check the $_POST variable?
You could preserve the ajax transitions and still have your javascript execute on your second page by moving your script inside the div with the data-role "page".
See this answer here: https://stackoverflow.com/a/6428127/2066267

Categories