I'm trying to get the selected value from a dropdown list.
The list has name 'select_employee', when I press the button with name 'save' I hope to get the value. I'm using a POST to to get the value.
I get the error 'Undefined index: select_employee'.
<div class="form-group">
<h2>Enter Certified Course Details</h2>
<?php
// start of connect db
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "TrainingDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//end of connect db
?>
<form id="form" action="" method="post">
<?php
// Drop down list employee
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Select Employee<br>";
echo "<select name='select_employee' id='select_employee'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['id'] . "'>" . $row['fname'] . " " . $row['sname'] . " </option>";
}
echo "</select>";
echo "<br>";
}
?>
</form>
<?php
if(isset($_POST['save'])){
echo "save<br>";
$employee=$_POST['select_employee']; // error here
echo "Selected Employee" . $employee . "<br>";
}
?>
<form Employee="/employee_page.php" method="post">
<button type="submit" class="btn btn-primary" name="save" value="save">Save</button>
</form>
</div>
You are using two different form so make sure to use only one form.
Related
I am making a school project.
The meaning of this project is that i create a login/registration form,
where i can register, login, view the record.
But i also need to make a dropdown menu were i can see all the records (users) and below it i need to make a button and when i select a record/user and press the delete button it need to be deleted from the database. and i have no clue how i can do this.
here is my code:
<?php
include ("connectie.php");
include ("deletecode.php");
$conn = new mysqli("localhost", "student14_admin", "lol12345","student14_jordi");
mysqli_select_db($conn,'student14_jordi');
$sql = "DELETE FROM users WHERE ID='$_GET[id]'";
echo "<select name='user'>";
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row['username'] . ">" . $row['username'] . "</option>";
}
}
echo "</select>";
?>
<form action="">
<a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
</form>
Create a button and name it Delete
<a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
Then make a delete.php file and have these code there.
<?php include_once 'db_config_filename.php';
// get id value
$id = $_GET['id'];
// sql to delete a record
$sql = "DELETE FROM tablename WHERE id='$id'";
// print_r($sql);
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
}
else {
echo "Error deleting record: " . mysqli_error($conn);
}
//redirect here
include 'data.php';
?>
I almost have it working. i have now this code:
delete.php:
<?php
include ("connectie.php");
$sql = "SELECT * FROM users ";
$result = $conn->query($sql);
echo "<select name='user'>";
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row['username'] . ">" . $row['username'] . "
</option>";
}
}
echo "</select>";
?>
<form action="" method="GET">
<input type="hidden" name="id" value=".$row['id']." />
<input type="submit" name="delete" value="verwijderen">
</form>
<?php
include ("deletecode.php");
?>
Deletecode.php:
<?php
$servername = "localhost";
$username = "student14_admin";
$password = "lol12345";
$dbname = "student14_jordi";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM users WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.
I have a form with a select list populated from a database. When the query is executed, a success message is returned however the database is not updated.
Here is the form:
<form id="formPrice">
<?php
$conn = new mysqli('localhost', 'something', 'something', 'something')
or die ('Cannot connect to db');
$result = $conn->query("SELECT PlotNumber FROM Developments WHERE Development = 'GREENGRAVES' AND Price = 'BOOKED'");
echo "<select name='plot_update'>";
while ($row = $result->fetch_assoc()) {
echo "<option value=\"Plot\">" . $row['PlotNumber'] . "
</option>";
}
echo "</select>";
?>
<input name="price" type="text" id="price">
<input name="update" type="submit" class="update_price">
</form>
Here is the ajax request:
$('.update_price').click(function() {
var FormData = $('form').serialize();
$.ajax({
type: "POST",
url: '../php/update_price.php',
data : FormData,
success:function(html){
document.getElementById("result_two").innerHTML=html;
}
});
return false;
});
Here is the php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$price = $_POST['price'];
$plot = $_POST['plot'];
$sql = "UPDATE Developments SET Price = '".$price."' WHERE PlotNumber = '".$plot."' ";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
The page displays "record updated successfully" in the result div, however database entry remains unchanged. Any ideas why? I'm stumped.
The error seemed to be with this line in the front end form:
echo "<option value=\"Plot\">" . $row['PlotNumber'] . "</option>";
When I changed to:
echo "<option>" . $row['PlotNumber'] . "</option>";
it worked!
There is issues with form field under php script:
change your php script to the following:
<form id="formPrice">
<?php
$conn = new mysqli('localhost', 'something', 'something', 'something')
or die ('Cannot connect to db');
$result = $conn->query("SELECT PlotNumber FROM Developments WHERE Development = 'GREENGRAVES' AND Price = 'BOOKED'");
echo "<select name='plot'>";
while ($row = $result->fetch_assoc()) {
echo "<option value=" . $row['PlotNumber'] . ">" . $row['PlotNumber'] . "</option>";
}
echo "</select>";
?>
<input name="price" type="text" id="price">
<input name="update" type="button" class="update_price">
Always make sure - Select option value needs to have the actual value of the record and select field will have the form field name.
I filled my dropdown menu with data from the database. I can select here the name of the product. I have a second textbox on my page. Here i want to show the price of the selected product. I tried different ways but I o net get it working.
First of all this is my dropdown menu where the product name is showed:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM form";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option onchange='OnChange()' value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
I want to show the price of the selected value in this textbox:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbsi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT price FROM form WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<input type='text' class='form-control' name='price1' id='price1' onkeyup='getValues()' value='" . $row["price1"]. "'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Using this script i only get "0 results" in price1. It doesnt update when i change the selected value of the dropdown menu. How can I ensure that price1 gets updated when i select another value in the dropdown menu?
Update 1:
I tried to add the the following script
<script>
function OnChange(){
UpdatePoints(<?php echo $price1; ?>);
}
echo "<script type='text/javascript'> window.onchange=load; </script>";
</script>
When I use this script i still get "0 results"
Update 2:
The following is still not working:
//filename: test.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div id='price-list'>";
echo "<input type='text' class='form-control' name='price1' id='price1' value='" . $row["price"]. "'>";
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "test.php",
data:'id='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<?php
$product1=$_POST['price1'];
?>
make a ajax call and get the value of using post
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "index.php",
data:'priceid='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<select class='form-control select2' id='product1' name='product1' style='width: 100%;' onChange="getstate(this.value);">
//yourcode
</select>
<div id="price-list">
</div>
index.php
<?php
$product1=$_POST['priceid'];
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Basically I want create a reset along with search button in my search engine, but I'm stuck at writing that part, here is my code so far:
<html>
<body>
<form action="" method="post">
<input type="text" name="filter">
<input type="submit" value="GO">
</form>
<?php
if(isset($_POST['filter'])){
$sql = "SELECT id, first_name, last_name, sex FROM employee where first_name like '%".$_POST['filter']."%'";
} else {
$sql = "SELECT id, first_name, last_name, sex FROM employee";
}
?>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query($sql);
if ($result->num_rows > 0) { // output data of each row
echo '<table>';
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["first_name"]. "</td><td>" . $row["last_name"]. "</td><td>" . $row["sex"] . "</tr>";
}
echo '</table>';
} else {
echo "0 results";
}
$conn->close();
Where do I create the reset button in? really stuck at that part, please consider help
Okay, how about this approach? This closes up your SQL injection, gives you proper mark up, and has a reset button.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$output = '';
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
if(isset($_POST['filter'])){
$sql = "SELECT id, first_name, last_name, sex FROM employee where first_name like ?";
$param = '%' . $_POST['filter']. '%';
} else {
$sql = "SELECT id, first_name, last_name, sex FROM employee";
}
$stmt = $mysqli->prepare($sql);
if(!empty($param)) {
$stmt->bind_param("s", $param);
}
$stmt->execute();
if ($stmt->num_rows > 0) {
$output .= '<table>';
while($row = $stmt->fetch_assoc()) {
$output .= "<tr><td>" . $row["id"]. "</td><td>" . $row["first_name"]. "</td><td>" . $row["last_name"]. "</td><td>" . $row["sex"] . "</tr>";
}
$output .= '</table>';
} else {
//not really no results could also be an error
$output .= "<p>0 results</p>";
}
$mysqli->close();
?>
<html>
<body>
<form action="" method="post">
<input type="text" name="filter" />
<input type="submit" value="GO" />
<input type="reset" value="Reset" />
</form>
<?php
if(!empty($output)) {
echo $output;
}
?>
</body>
</html>
You also currently aren't populating the searched value back into the form.
I'm new here and I have one question.
I have one PHP-HTML Dynamic Form with this code:
<form class="insert" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?CategoryName=<?php echo $CategoryName; ?>">
<br/><label><?php echo $lang['CATEGORY_NAME']; ?>:</label>
<?php
$conn = new mysqli($SERVERNAME, $USERNAME, $PASSWORD, $DBNAME);
if ($conn->connect_error) {
die("Greska: " . $conn->connect_error);
}
$sql = "SELECT CategoryName FROM Categories WHERE ForUser = '$User_Check' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select style='text-transform: uppercase;' onchange='location = this.options[this.selectedIndex].value;' class='form-control' name='CategoryName'>";
while($row = $result->fetch_assoc()) {
echo "<option value='?CategoryName=".$row['CategoryName']."'>".$row['CategoryName']."</option>";
}
echo "</select>";
}
else {
echo "<div style='margin-top: 18px;' class='alert alert-danger'><b>$lang[MANAGE_CATEGORY_ALERT]</b></div>";
}
$conn->close();
?>
<?php
$conn = new mysqli($SERVERNAME, $USERNAME, $PASSWORD, $DBNAME);
if ($conn->connect_error) {
die("Greska: " . $conn->connect_error);
}
$sql = "SELECT FieldName FROM Fields WHERE ForUser = '$User_Check' AND ForCategory = '$CategoryName' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<label>".$row['FieldName'].":</label>";
echo "<input class='form-control' id='focusedInput' required type='text' placeholder='".$row['FieldName']."' name='".$row['FieldName']."'>";
}
}
else {
echo "<div style='margin-top: 18px;' class='alert alert-danger'><b>$lang[MANAGE_CATEGORY_ALERT]</b></div>";
}
$conn->close();
?>
<input class="btn btn-primary" type="submit" value="<?php echo $lang['CREATE_CATEGORY_BUTTON']; ?>">
</form>
Now I have problem when I want to insert posted data into MySQL tables because PHP don't know where to insert posted data, and doesn't know where to enter which data.
Can anyone help me?
Your Question is not much clear but as i can understand from the code
you have 2 tables, categories and fields . and you are rendering the form using it . when u will submit the form, u will get all the fields and 'CategoryName' which you can fetch using a $_POST variable .
I dont know in which table u want to save the data but u can easily save it like any other form .
more over u can open once in browser and save the generated file for debugging saving process so that u don't need to render again.
u can always var_dump $_POST to check the data u receive after submitting form.