i got one problem with this . everytime i go insert it the page just refresh and blank. i dont know where is the problem but i checked my sql the data i use is the same. so i dont know where the problem is.
<form enctype="multipart/form-data" method="POST"> <tr>
<td> Generic Name:<input type="text" class="form-control" name = "gname"></td>
</tr>
<tr>
<td> Brand Name:<input type="text" class="form-control" name = "bname"></td>
</tr>
<tr>
<td> Quantity:<input type="number" min="0" class="form-control" name = "mqty"></td>
</tr>
<tr>
<td> Description:<input type="text" class="form-control" name = "mdesc"></td>
</tr>
<br>
<tr>
<td> <input type="submit" class="btn btn-info" name="add" Value="Add"></td>
</tr>
</form>
<?php
include "../../functions/connect.php";
error_reporting(0);
date_default_timezone_set('Singapore');
$date = date('m/d/Y h:i:s a', time());
?>
<?php
include "../../functions/connect.php";
extract($_POST);
if(isset($add)){
$sql = "INSERT INTO `tbl_meds`(`date`,`generic`,`brand`,`description`,`medqty`) VALUES ('$date','$gname','$bname','$mdesc','$mqty')";
$result = mysql_query($sql) or die("Verification Error: " . mysql_error());
}
?>
EDIT: Result of var_dump($_POST) -
array(5) {
["gname"]=> string(11) "Paracetamol"
["bname"]=> string(8) "Biogesic"
["mqty"]=> string(3) "100"
["mdesc"]=> string(71) "The most prescribed Headache and Fever brand that's Effective and Safe."
["add"]=> string(4) "Save"
}
if(isset($_POST["add"])){
this will work for you
your form has POST method. So on PHP side you have to handle it with $_POST global variable.
Related
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
when i print using: var_dump($_GET["own_pools"]); I get
array(4) {
[0]=> string(1) "5"
[1]=> string(3) "300"
[2]=> string(3) "280"
[3]=> string(2) "50"
}
the parameters in the link are showing correctly
index?own_pools[]=5&own_pools[]=300&own_pools[]=280&own_pools[]=50&visitor_expect=100000000&ticket_price=15&submit_calc=Calculate
but after that the values did not get the right data back in the form and shown like this:
How can I get it calculated and get the values correctly from the input and returned the entered values back to the input value.
I have searched and read but couldn't find the answer...
I have the following code
<?php
$ticket_price=0;
$visitor_expect=0;
$total_value=0;
$own_pools=0;
$pool_result=0;
if(isset($_GET['submit_calc']) && isset($_GET['own_pools'])) {
$ticket_price = $_GET['ticket_price'];
$visitor_expect =$_GET['visitor_expect'];
$own_pools=$_GET['own_pools'];
if(is_numeric($ticket_price) && is_numeric($visitor_expect)){
// pools calculations
$rev_visitors=((int)$_GET["visitor_expect"]* (int)$_GET["ticket_price"]);
$total_value=($rev_visitors*0.01)*30;
$pool_result = $total_value ;
}
}
?>
<form action="" method="get" >
<table>
<tr>
<?php
// Display headers in one row as colmun
$tds = '';
$sum_main_pools=0;
foreach( $calculations as $index =>$calculation ) {
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
// create TDs for the values
if ($calculation['Calculation']['poolname']<>"Regional Pool"){
$tds .= '<td>
<div class="input text" id="pool_calc">
' . $calculation['Calculation']['total_units'] . '
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
</div>
</td>';
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}
} else {
$tds .= '<td>
' . $calculation['Calculation']['total_units'] . '
</td>';
}
}
var_dump($_GET["own_pools"]);
?>
</tr>
<tr>
<?php
// Printing the values
echo $tds;
?>
</tr>
</table>
<?php
$pool_result = $total_value + $sum_main_pools;
?>
<table>
<tr>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Expected Visitors per day
<input name="visitor_expect" type="text" value="100000000" id="visitor_expect" maxlength="9" size="9" >
</div>
</td>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Ticket Price
<input name="ticket_price" type="text" value="15" id="ticket_price" maxlength="2" size="3" >
</div>
</td>
</tr>
<tr >
<td style="width: 60%">
<div>
<label > <?php echo (isset($pool_result) ? $pool_result : "");?></label>
<input name="submit_calc" type="submit" value="Calculate" id="submit_calc">
</div>
</td>
</tr>
</table>
</form>
Solved it:
added in foreach a key in foreach
foreach( $calculations as $key =>$calculation ) {
and in input value added as array
<input name="own_pools[]" type="number" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'][$key];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}
I have a form where I ask the user to enter some values. Then, using PHP, I'm trying to process the values the user entered.
The issue I'm facing is that, after the user submitted the form, all my values are set to 1 instead of the value the user entered.
Here is the form:
<form method="post" action="checkOutResult.php">
<p> Transaction #:
<input type="text" name="transactionNum" placeholder="Input #">
</p>
<table>
<tr>
<td>Student ID</td>
<td>Employee ID</td>
<td>Professor ID</td>
</tr>
<tr>
<td>
<input type="text" name="studentID" placeholder="7 Digit Student ID">
</td>
<td>
<input type="text" name="empID" placeholder="7 Digit Employee ID">
</td>
<td>
<input type="text" name="profID" placeholder="7 Digit Professor ID">
</td>
</tr>
</table>
<table>
<tr>
<td>Product ID</td>
</tr>
<tr>
<td>
<input type="text" name="productID" placeholder="Scan Barcode">
</td>
</tr>
</table>
<center>
<table>
<tr>
<iframe src="http://free.timeanddate.com/clock/i4nrjjak/n179/fn10/fs24/fcb40431/ftb/bas2/bat1/bacfff/pa8/tt0/tw1/td2/th2/tb4" frameborder="0" width="334" height="74"></iframe>
<td>CheckOut Date</td>
<td>CheckOut Time</td>
</tr>
<tr>
<td>
<input type="text" name="checkoutDate" placeholder="YYYY-MM-DD">
</td>
<td>
<input type="text" name="checkoutTime" placeholder="HH:MM:SS">
</td>
</tr>
</table>
</center>
<center>
<table>
<tr>
<td>Due Date</td>
<td>Due Time</td>
</tr>
<tr>
<td>
<input type="text" name="dueDate" placeholder="YYYY-MM-DD">
</td>
<td>
<input type="text" name="dueTime" placeholder="HH:MM:SS">
</td>
</tr>
</table>
</center>
<input type="submit" name="Submit" value="submit">
</form>
This is the PHP code that is executed upon form submission.
<?php
include ("thesis.php");
$transactionNum = isset($_POST['transactionNum']);
$studentID = isset($_POST['studentID']);
$empID = isset($_POST['empID']);
$profID = isset($_POST['profID']);
$productID = isset($_POST['productID']);
$checkoutDate = isset($_POST['checkoutDate']);
$checkoutTime = isset($_POST['checkoutTime']);
$dueDate = isset($_POST['dueDate']);
$dueTime = isset($_POST['dueTime']);
$query = "INSERT INTO Transactions(transactionNum, studentID, empID, profID, productID, checkoutDate, checkoutTime, dueDate, dueTime)
VALUES ('$transactionNum', '$studentID', '$empID', '$profID', '$productID', '$checkoutDate', '$checkoutTime', '$dueDate', '$dueTime')";
if($link->query($query) === TRUE){
echo "New Record Created Successfully. ";
echo"<br>";
}
else{
echo" Error: " .$query ."<br>" .$link->error;
}
echo"<br>";
echo "Transacton #: ".$transactionNum;
echo"<br>";
echo "Student ID: ".$profID;
echo"<br>";
echo "Employee ID: ".$empID;
echo"<br>";
echo "Professor ID: ".$profID;
echo"<br>";
echo "Product ID: ".$productID;
echo"<br>";
echo "Out Date: ".$checkoutDate;
echo"<br>";
echo "Out Time: ".$checkoutTime;
echo"<br>";
echo "Due Date: ".$dueDate;
echo"<br>";
echo "Due Time: ".$dueTime;
echo"<br>";
?>
Why do I keep getting 1s instead of the values I submit with the form?
The following command retuns true (1 as a string) if the field empID isset in your $_POST data.
$empID =isset($_POST['empID']);
it should be
$empID = isset($_POST['empID']) ? $_POST['empID'] : "";
and so on.
Which means: catch the empID of $_POST if there is one, else catch an empty string. It is a short version of the following:
if(isset($_POST['empID'])) {
$empID = $_POST['empID'];
} else {
$empID = "";
}
You are getting boolean for isset here
$empID = isset($_POST['empID']);
Which is why it is giving you a value of 1 as that is the value for true in this case.
Resolve with if isset($_POST['empID']) {$empID = $_POST['empID'];}
My html form is:
<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
<table>
<tr>
<td>Email Address</td>
<td>First Name</td>
<td>Surname</td>
<td><td>
</tr>
<tr>
<td> <input type="text" name="emailAddress" value="example#email.com"> </td>
<td> <input type="text" name="firstName" value="First Name"> </td>
<td> <input type="text" name="surname" value="Surname"> </td>
<td> <input type="submit"> </td>
</tr>
</table>
</form>
and the php that it is calling is:
$Email = $_GET["email"]
$firstName = $_GET["fName"]
$surName = $_GET["surname"]
$sql = "INSERT INTO emailaddress (EmailAddress, FirstName, LastName)
VALUES ($Email, $firstName, $surName)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>`
It is meant to pass the inputted information on to a database. It is connecting to the database without an issue when I use already use pre-created values for it to input, but as soon as I try and get it to use inputted values and call the php it just returns with a blank page.
You are missing the semicolons at the end of the line for the first 3 lines of the php file.
A blank page is typical for a syntax error. Whenever a blank page is displayed for no good reason put the following lines at the top of your php file and you should see the php error displayed.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Besides that:
You will also receive a SQL error because you are not enclosing the php variables in quotes when you construct the query. You should REALLY consider escaping all user submitted data.
$_GET["email"] should be $_GET["emailAddress"] as set in the form
$_GET["fName"] should be $_GET["firstName"] as set in the form
Consider using the html attribute "placeholder" instead of writing the hint as a value.
Also, what user3590911 said about the quote
Your
<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
should be:
<form enctype="multipart/form-data" action="AddEmail.php" method="GET">
try this,
<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
<table>
<tr>
<td>Email Address</td>
<td>First Name</td>
<td>Surname</td>
<td><td>
</tr>
<tr>
<td> <input type="text" name="email_address" value="example#email.com" > </td>
<td> <input type="text" name="firstName" value="First Name"> </td>
<td> <input type="text" name="surname" value="Surname"> </td>
<td> <input type="submit"> </td>
</tr>
</table>
</form>
<?php
$Email = $_GET["email_address"]
$firstName = $_GET["firstName"]
$surName = $_GET["surname"]
$sql = "INSERT INTO emailaddress (EmailAddress, FirstName, LastName) VALUES ($Email,$firstName, $surName)";
$queries = $conn->query($sql);
if ( $queries ) {
echo "New record created successfully";
}
else
{
die('Invalid query: ' . mysql_error());
}
$conn->close();
?>
<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
to
<form enctype="multipart/form-data" action="AddEmail.php" method="post>
I get a information from my DB and then I build a from based on the information. The idea is that I want to capture the score each players made in a game. This obviously mean that I would have an input field with the same name attribute of PID as well as for the score input field. My code looks like this:
<form name="enterscores" id="enterscores" method="post" action="parseFiles/parse_enterscores.php">
<b>Game number: </b> <input type="text" id="entergameID" name="entergameID" maxlength="10" size="10" placeholder="Game number" />
<hr />
<table width="50%">
<tr>
<td>
<b>First name</b>
</td>
<td>
<b>Last name</b>
</td>
<td>
<b>Score</b>
</td>
</tr>
<?php
foreach ($LoginIDs as $PID){
$Login_ID = $PID;
cleanNumber($Login_ID);
foreach (getPlayerDetails($Login_ID) as $playerdDetails) {
$Login_ID = (int)$playerdDetails->Login_ID;
$First_Name = $playerdDetails->First_Name;
$Last_Name = $playerdDetails->Last_Name;
?>
<tr>
<td>
<input type="hidden" name="PID" id="PID" value="<?php echo $Login_ID;?>" />
<?php echo $First_Name; ?>
</td>
<td>
<?php echo $Last_Name; ?>
</td>
<td>
<input type="hidden" id="number_of_players" name="number_of_players" value="<?php echo $number_of_players; ?>" />
<input type="text" id="playerscore" name="playerscore" placeholder="Score" maxlength="3" size="3" />
</td>
</tr>
<?php
}
}
?>
<tr>
<td>
<input type="submit" id="enterscoresbtn" name="enterscoresbtn" value="Submit scores" />
</td>
</tr>
</table>
</form>
<?php
}?>
On my parse page I have the following code:
<?php
if(!$_POST){
header('Location: ../');
exit;
}
if(isset($_POST['enterscoresbtn'])){
require_once (__DIR__ . '/../functions/functions.php');
echo '<pre>';
var_dump($_POST);
echo '</pre>';
$PIDs = $_POST['PID'];
print_r($PIDs);
$playerscores = $_POST['playerscore'];
$number_of_players = $_POST['number_of_players'];
cleanNumber($number_of_players);
// $player = array_combine($PIDs, $playerscores);
//print_r($player);
}?>
The output I get looks like this:
array(5) {
["entergameID"]=>
string(2) "15"
["PID"]=>
string(1) "9"
["number_of_players"]=>
string(1) "4"
["playerscore"]=>
string(3) "104"
["enterscoresbtn"]=>
string(13) "Submit scores"
}
How do I get to post these values and how would I be able to get each login id tie to the correct score on the parse page please?
create your inputs like <input name="playerscore[<?php echo $Login_ID ?>]" /> or something like that. this will give you post data like
$_POST['playerscore'] = array( 23=> 123, 42 => '<somePlayerScore>' /* and so on ... */ )
i would have written this as a comment, but this would not allow formatting the post propperly
I have a classrooms in schools and when I click on a certain classroom, I want to add students into it but my actual code is doing something stupid. It adds a student but i can see the student in all classrooms, not just in the one that i added him into. So when Im in classroom number 1, I see a form in there, I can add a student there, ... see how it works here:
here is the code: http://www.xxxx.xx/projekt/
here is my code in file trieda.php
<table align="center"><tr><td>
<form action="vlozit2.php" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="hidden" name="id_triedy" value="<?= $trieda['id_triedy'] ?>" />
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
</td></tr></table>
<?php
$result = mysqli_query($prip,"SELECT * FROM student ORDER BY meno");
while($student = mysqli_fetch_array($result))
{
echo "<br /><table cellspacing='1' cellpadding='1' class='tabulka1' align='center'><tr>";
echo "<td width='200'><a href='student.php?id_triedy=".$trieda['id_triedy']."".id_student=".$student['id_student']."'>".$student['meno']." ".$student['priezvisko']."</a></td>";
?>
<td width='300px' align='right' bgcolor="#fbfbfb">Zmazať</td>
</tr></table>
<?php
}
?>
here is vlozit2.php (a code that works for the form to add a student)
if(isset($_POST['submit']))
{
//meno a priezvisko
$student = $_POST['meno'];
$student = $_POST['priezvisko'];
$trieda = $_POST['id_triedy'];
//connect to the database
include 'config.php';
//insert results from the form input
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES('$_POST[meno]', '$_POST[priezvisko]', '$_POST[id_triedy]')";
$add = "<table align='center'>
<tr>
<td> Študent bol úspešne pridaný do triedy. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
$not_add = "<table align='center'>
<tr>
<td> Študent s týmto menom a priezviskom už je v tejto triede. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
if (mysqli_query($prip, $sql)) {
echo $add;
}else{
echo $not_add;
}
mysqli_close($prip);
}
?>
Try to replace your part of code with these snipets:
1) in trieda.php
<form action="vlozit2.php?id_triedy=<?php echo $_GET["id_triedy"];?>" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
2) in vlozit2.php
$student = $_POST['meno'];
$priezvisko = $_POST['priezvisko'];
$id_trieda = $_GET['id_triedy'];
and
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES( '{$student}', '{$priezvisko}', {$id_trieda} )";
Hopefully you store your id_trieda as INT type.
In your vlozit2.php file is nothing about inserting of class id. So put
<input type="hidden" name="classId" value="<?= $trieda['id'] ?>" />
to your form and in vlozit2.php get this value from $_POST['classId'] and insert it with other students data or anywhere you want to have it.