Undefined variable: POST - PHP and MySQL - php

I've got a form like this:
<form name="htmlform" method="post" action="script/gen.php?postData">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="customer">Customer:</label>
</td>
<td valign="top">
<input type="text" name="customer" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="nol">Number of licences: </label>
</td>
<td valign="top">
<input type="text" name="nol" maxlength="50" size="30">
</td>
</tr>
<tr>
<td>
<form method="post" id="submit" action="script/gen.php">
<input type="button" onClick="getKey()"; value="Generate key"/>
</td>
</tr>
<div id="innhold">
<h4>Licence Key: </h>
</div>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
The codelines of interest is:
<input type="text" name="nol" maxlength="50" size="30">
and
<input type="text" name="customer" maxlength="50" size="30">
I try to write this information to a database like this:
function postData($key1) {
//just to check if the key is equal to the one thats posted to the user
//echo '<h5>From postData' . $key1 . '</h5>';
/*echo '<script type="text/javascript"> alert("The order has been submitted successfully");
location = "/Webpanel/index.html";
</script>';*/
$customerVar = $POST['customer'];
$nolVar = $POST['nol'];
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("licencedatabase");
$query_add = "INSERT INTO licence (`customer_name`,`licence_count`) VALUES ('$customerVar','$nolVar')";
$query_exec = mysql_query($query_add) or die(mysql_error());
mysql_close();
}
But I keep getting the error:
Undefined variable: POST
How can I accomplish this? Thanks in advance.

Its $_POST not $POST.
$customerVar = $_POST['customer'];
$nolVar = $_POST['nol'];

That's because it's called $_POST.

Try using $_POST instead of $POST

to access the superglobal POST use $_POST not $POST

All PHP superglobals (such as those for GET and POST) are prefixed with an underscore, so: $POST should be $_POST.
Have a look here for more information about the available superglobals in PHP:
http://php.net/manual/en/language.variables.superglobals.php

Try using $_POST instead of $POST
Check following example:
The predefined $_POST variable is used to collect values from a form sent with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
Example:
<form action="submitform.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the user clicks the "Submit" button, the URL will look like this: http://localhost/submitform.php
The "submitform.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
might you will understand clearly.

Related

unable to store my all retrieve the data from the Mysql table to my new html form.

Here i am facing a problem with the while loop.. (i think) unable to store my all retrieve the data from Mysql table to my html form.
the first row is only getting posted and the rest of the rows are not posting due to same name element getting repeated from the while loop,
Here the $_POST['mechanic_name']; one time i'm using.. any problem with this..
because this is not in a while loop, ore if you think any other problems with the code below pls advice
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
</tr>
<!--* For save php script*-->
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,) VALUE ('$mechanic_ID','$mechanic_name'[enter image description here][1])";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
plz help me...!`
<div>
<form role="form" method="post" action="schedule.php">
<table class="table table table-inverse table-hover">
<fieldset>
<div>
<input class="form-control" placeholder="Username" name="month" type="Month" value="January">
</div>
<thead class="thead-inverse">
<tr>
<th>Mechanic Id</th>
<th>Mechanic Name</th>
<th>Woking Day Selection</th>
<th>Delete User</th>
</tr>
</thead>
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
<td>
<div class="weekDays-selector">
<input type="checkbox" name="Sun" id="weekday-sun" class="weekday" />
<label for="weekday-sun">S</label>
<input type="checkbox" name="Mon" id="weekday-mon" class="weekday" />
<label for="weekday-mon">M</label>
<input type="checkbox" name="Tue" id="weekday-tue" class="weekday" />
<label for="weekday-tue">T</label>
<input type="checkbox" name="Wed" id="weekday-wed" class="weekday" />
<label for="weekday-wed">W</label>
<input type="checkbox" name="Thu" id="weekday-thu" class="weekday" />
<label for="weekday-thu">T</label>
<input type="checkbox" name="Fri" id="weekday-fri" class="weekday" />
<label for="weekday-fri">F</label>
<input type="checkbox" name="Sat" id="weekday-sat" class="weekday" />
<label for="weekday-sat">S</label>
</div>
</td>
<td>
<!--button-->
<input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >
</td>
</tr>
</fieldset>
<?php } ?>
</table>
</form>
</div>
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
$Sun=$_POST['Sun'];//same
$Mon=$_POST['Mon'];//same
$Tue=$_POST['Tue'];//same
$Wed=$_POST['Wed'];//same
$Thu=$_POST['Thu'];//same
$Fri=$_POST['Fri'];//same
$Sat=$_POST['Sat'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,month,Sun,Mon,Tue,Wed,Thu,Fri,Sat) VALUE ('$mechanic_ID','$mechanic_name','$month','$Sun','$Mon','$Tue','$Wed','$Thu','$Fri','$Sat')";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
?>
ok
your text input have same static name
you shuld give it an array name if you want to save it like :
<input type="text" name="mechanic_ID[]" value="first_one" />
<input type="text" name="mechanic_ID[]" value="second_one" />
and when get data
$mechanic_ids = $_POST['mechanic_ID'];
// this give you data in array ('first_one','second_one')
can resume
foreach($mechanic_ids as $mechanic_id){
// do some thing in this one
}
here good example about arrays form html and php
here is the things is mechanic table whole data (mechsanic_ID , Mechanic_name) able to retrieve, it's showing all the data; because of while loop, But when register it the very first row only able to posted and the rest of the rows are not postinginterface look like this.

How to submit two php forms to one location

I have two php forms, both for registration means (enter name, username, password etc). My first registration form (reg1.php) contains the basic inputs as I mentioned above and then I have a second registration form (reg2.php) that includes checkboxes where the form would ask the user other questions before finalizing their sign up.
What I am having trouble with is that when I enter data into my inputs in the first registration form, then proceed to the next registration page and fill that out and click Sign Up, my output on my server only obtains the information I inputted in the second registration page, not the first.
How can I get both inputted data from the two registration pages to appear together? I have been told about POSTBACK to help with this but I am not all that familiar with how to use it.
First php page:
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table style="width:300px">
<form name="Rego" method="post" action="rego2.php">
<tr>
<td><label>Name:</label></td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type="password" name="password" id="pass" /></td>
</tr>
<tr>
<td><label>Confirm Password:</label></td>
<td><input type="password" name="conpassword" id="conpass"/></td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Next Page">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
Second php page
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
When the first form is submitted either store the data in a $_SESSION variable and retrieve it when you process the second form, or write it to hidden <input> elements on your second form and retrieve it from the $_GET or $_POST array
In 2nd input form you take first inout from value and keep those one as hidden.
and 2nd input form you should
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<input type="hidden" name="fname" value="<?php echo $_POST['fname'];?> " />
<input type="hidden" name="username" value="<?php echo $_POST['username'];?> " />
<input type="hidden" name="passowrd" value="<?php echo $_POST['password'];?> " />
<input type="hidden" name="conpassowrd" value="<?php echo $_POST['conpassword'];?> " />
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
I hope it will work for you.
In the second page, you can get the post data from page 1 and set it in a hidden field like this, say the fname:
<input type="hidden" name="fname" id="fname" value="<?php echo $_POST['fname'] ?>">
When you submit from the second page, you can catch this data in the destination page, say
$_POST['fname']

Returning PHP variables data back to an HTML Page

I am fairly new to php and have only written basic post email returns. However what im trying to achive now is to return the data held within a variable back to the webpage in a specific location on the page.
So far I have a form that collects the data Name & Surname, this is collected by php.
$firstnameField = $_POST ['name'];
$surnameField = $_POST ['surname'];
All I want to do is print the data within these variables back to the screen in a specific location on the page. I have looked around but others are talking about databases and ajax, and I havent the slightest about that stuff.
Thanks in advance.
It's pretty simple: just use echo/print inside a <?php ?> code block wherever you want that variable printed in your document:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$firstname = $_POST['name'];
}
?>
<html>
<body>
<?php if (isset($firstname)) { echo "<p>Hello, $firstname</p>"; } ?>
<form method="POST">
Enter a name: <input type="text" name="name" />
</form>
</body>
</html>
<?php
if ((isset($_POST["dataEntered"])) && ($_POST["dataEntered"] == "Yes"))
echo "Hello! " . $_POST["name"] . " " . $_POST["surname"];
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="form1" id="form1">
<table>
<tr>
<td>Name:</td>
<td><input name="name" type="text" id="name" value="" size="32" /></td>
</tr>
<tr>
<td>Surname:</td>
<td><input name="surname" type="text" id="surname" value="" size="32" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Send Form" /></td>
</tr>
</table>
<input type="hidden" name="dataEntered" value="Yes" />
</form>
<?php
}
?>

two forms conflict with each other

I have placed two forms on one page. Bothe forms work fine separately but when they are placed on one page at the same time they conflict with each other. Here are both forms:
Contact Form:
<form name="contactform" id="contactform" method="post" action="#targetAnchorPage2">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
<td colspan="2" class="error">
<?php
require_once("contact_send.php");
?>
</td>
</tr>
<?php
}
?>
<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>
<tr><td colspan="2"><label id="tworows" for="message" class="<?=$message_class?>">your message:</label></td></tr><tr><td colspan="2"><textarea name="message" cols="22" rows="6" value="<?=$message_class?>"></textarea>
</td></tr>
<tr>
<td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">
</td>
</tr>
</table>
</form>
Subscribe Form:
<form name="subscribeform" id="subscribeform" method="post" action="#targetAnchorPage3">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
<td colspan="2" class="error">
<?php
require_once("subscribe_send.php");
?>
</td>
</tr>
<?php
}
?>
<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>
<tr>
<td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">
</td>
</tr>
</table>
</form>
How can this be solved? Is it caused by the "required_once" command?
I am guessing that since you are showing the required files based on the same criteria isset($_POST['name']) and since both forms have the name field you end up showing the code in both requires regardless of which form is submitted. You should simply change the form field names on on of the forms such that they are different.
Both forms have the same action attribute, they both point back to the same page (note that the hash is not sent to the server). As they both have a field called name and you are checking for that, both actions get executed regardless of which form was sent in.
You can do either:
use different scripts / form processors (don't post back to the same page)
use a different check for each form, for example by adding a hidden input that will allow you to distinguish between the forms.
Add
formaction="Your_URL"
Attribut in Button

PHP Validation Message appears on top of my user form

I'm having a problem with one of my PHP form validation scripts.
Basically, I have a form that when a user submits information it does validation checking (via PHP) and outputs the result (Success, Error1, Error2 etc) via an echo statement
However, after a user Submits the form (the action=same page ie. it posts to itself) the validation message appears at the top of the form or next to it. I want the echo message to appear under the form.
This is what is happening now in the page:
Error: Your password is incorrect
Username:
Email:
Password:
Submit Button
This is what I want it to look like:
Username:
Email:
Password:
Submit Button
Error: Your password is incorrect
Does anyone know how to remedy this?
Here is the code:
<form id="username_check" name="username_check" method="post">
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username"
value="<?php echo($_POST['Username']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address"
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</form>
<?php
//If form was submitted
if (array_key_exists('submit',$_POST)){
//Do something
echo "Form validation here.....";
}
?>
This is dependent on the order of execution of the code. If you are using an IDE, set a breakpoint before the validation and step through. Otherwise, I'm pretty sure we'll need to see some of the code to get an idea of why this is happening.
Try:
<form id="username_check" name="username_check" method="post">
<table>
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username"
value="<?php echo($_POST['Username']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address"
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</table>
</form>
<?php
//If form was submitted
if (array_key_exists('submit',$_POST)){
//Do something
echo "Form validation here.....";
}
?>
If you are calling the
echo $error_message;
before showing the form, then the message is displayed before the form.
If you want the error message after the form just move the echo command after the form is printed out.
Sounds like you are echo'ing out your error message, then echo'ing out the form.
Do it the other way around.
Don't echo the error save it in to a variable and print it behind the form print.
That way you can also check every time if an error occur during form validation, simple check if error-variable is emtpy/false

Categories