I have a small problem.
<select name="level_id"> is not posting.
So i get an error like : Undefined index : level_id
Exactly what can i do?
$sql1 = 'SELECT T_ABILITY.PK AS AB_PK,T_ABILITY.ABILITY_NAME AS AN,T_ABILITY_LEVEL.PK AS LE_PK,T_ABILITY_LEVEL.LEVEL_NAME AS LN
FROM T_USER_ABILITY_REL,T_ABILITY,T_ABILITY_LEVEL WHERE
T_USER_ABILITY_REL.ABILITY_FK = T_ABILITY.PK AND
T_USER_ABILITY_REL.ABILITY_LEVEL_FK = T_ABILITY_LEVEL.PK AND
T_USER_ABILITY_REL.USER_FK = '.$user_id.'
ORDER BY AN';
$stmt1 = oci_parse($conn, $sql1);
$r1 = oci_execute($stmt1);
while ($row1 = oci_fetch_array($stmt1, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<form method="post">';
echo '<tr>';
echo '<td>'.$row1["AN"].'</td>';
echo '<input type="hidden" name="ability_id" value="'.$row1["AB_PK"].'"/>';
echo '<td class="select-level">';
$sql2 = 'SELECT PK,LEVEL_NAME FROM T_ABILITY_LEVEL ORDER BY LEVEL_ORDER';
$stmt2 = oci_parse($conn, $sql2);
$r2 = oci_execute($stmt2);
echo '<select name="level_id" class="form-control selectpicker" data-container="body" data-live-search="true" data-size="5" title="Seviye Seçiniz">';
while ($row2 = oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<option '.($row2["PK"] == $row1["LE_PK"] ? 'selected="selected"' : "").' value="'.$row2["PK"].'">'.$row2["LEVEL_NAME"].'</option>';
}
echo '</select>';
echo '<button type="submit" name="update-user-ability" class="btn btn-success">Güncelle</button>';
echo '<button type="submit" name="delete-user-ability" class="btn btn-danger">Sil</button>';
echo '</td>';
echo '</tr>';
echo '</form>';
}
submit part below
if (isset($_POST["update-user-ability"])) {
$user_id = $_GET["user_id"];
$ability_id = $_POST["ability_id"];
$level_id = $_POST['level_id'];
This is because your select is empty (that is, the oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC returns no rows.)
A select, when no option are present, will not return a value to the receiving php script.
Add a default option before the loop to be sure something is passed even if your sql query returns nothing.
this script will return an empty post:
<html>
<body>
<form method="post">
<select name="select"></select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(0) { }
while this one will have the value 12 defined:
<html>
<body>
<form method="post">
<select name="select">
<option value="12">12</option>
</select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(1) { ["select"]=> string(2) "12" }
Could be that the value of level_id isn't send along, because it is a <select>. Not sure, but could be that only <input /> values are properly posted.
You could use a <input type="hidden" name="level_id_val" /> and update its value with JavaScript every time the value of <select name="level_id"> is changed (using .onchange).
Then, in PHP, use that field instead of the select: $level_id = $_POST['level_id_val'];
Related
In my database I have 2 tables:
To insert data, I have a form that populates dropdown options from the table formulation. This is what the insert form for formulation dropdown looks like:
<?php
$formulation = '';
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
$formulation .= '<option value="' . $row["formulationID"] . '">' . $row["formulation_name"] . '</option>';
}
?>
<select>
<option value="">Select formulation</option>
<?php echo $formulation; ?>
</select>
Now I am working on the ‘Update’ form. But my question is how can I populate the ‘Formulation’ field dropdown with the data from the formulation table (like as the insert form) but pre-selected with the existing formulation value for the name from the items table? Like this image below:
I am having problem with how I should build the form. How should I proceed with this form?
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
while ($row = $query->fetch_assoc()) {
$output['data'][] = array(
$row['name'],
);
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<!--What should be the codes here? -->
</select>
</div>
<button type = "submit">Save changes</button>
</form>
Thanks in advance for your suggestion.
Note: I'm not a user of mysqli so maybe there will be some error, but you will get the idea. This will not tackle the update part, just the populate part
Since you are editing a certain item, I will assume that you have something to get the item's itemID.
<?php
$sql = "SELECT * FROM items WHERE itemID = ?";
$query = $connect->prepare($sql);
$query->bind_param("s", $yourItemID);
$query->execute();
$result = $query->fetch_assoc();
$itemName = $result['name'];
$itemFormulation = $result['formulation_fk'];
//now you have the name and the formulation of that certain item
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text" value="<?php echo $itemName; ?>"><br>
<label>Formulation</label>
<select >
<?php
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['formulationID']; ?>" <?php echo ($row['formulationID'] == $itemFormulation) ? 'selected' : ''; ?>>
<?php echo $row['formulation_name']; ?>
</option>
<?php
}
?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I changed the code to better suit the problem, there may be typos, just comment for clarification
If I have understand Your question... You have to put Your result into a string. For example:
<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);
$option = '';
while ($row = $query->fetch_assoc()) {
$name=$row['name'],
$option.='<option value="$name">$name</option>'
}
echo json_encode($output);
?>
<form action=" " method="POST">
<div>
<label>Name</label>
<input type="text"><br>
<label>Formulation</label>
<select >
<?=$option?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
I hope to be of help
This should do the trick:
<?php
$itemsSql = "SELECT * FROM items WHERE itemId = 5";
$itemQuery = $connect->query($sql);
$item = $itemQuery->fetch_assoc();
$formulationsSql = "SELECT * FROM formulation";
$formulationsQuery = $connect->query($sql);
$formulations = $itemQuery->fetch_assoc();
?>
<form action="updateItem" method="POST">
<div>
<label>Item Name</label>
<input type="text" value="<?= $item[0]['name']; ?>"><br>
<label>Formulation</label>
<select>
<?php foreach($formulations as $formulation){
echo '<option value="'. $formulation['formulationId'].'">' .
$formulation['formulation_name'] . '</option>';
} ?>
</select>
</div>
<button type = "submit">Save changes</button>
</form>
When I submit the form I don't get the value of the select option I tried using POST and session but it always show nothing
main.php
<form role="form" method="POST" action="test.php">
<?php if($id == 1 OR $id==2){
echo" <p> No data</p> ";}else{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata )
or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data )) {
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
$_SESSION['data_id']= $data_id;
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
$dataID = isset($_POST['data_id']) ? $_POST['data_id'] : '';
echo "data is $dataID";
Name of your input type select is data and you are accessing it with data_id so you have to use $_POST['data'] instead of $_POST['data_id']
get value of select box using name "data" as you have set name="data" in <select> box in html:
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is".$dataID;
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
}
?>
</select>
<?php
}
?>
<button type="submit" class="btn btn-primary">show</button>
test.php
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is $dataID";
try this one..
Check below points its may be creating issue.
Check first your <select class="form-control" name="data"> name is data so you can access it using $_POST['data'] not data_id.
Check for <option value="'.$row['data_id'].'"> may be data_id not giving correct value try to check with static value.
I modified this code for you,please use this code.Its work for me,i hope this code will work also for you.
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
?>
<option value="<?php echo $row['data_id']; ?>"><?php echo $dataName ?></option>
$_SESSION['data_id']= $row['data_id'];
<?php
}
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
<?php
if(isset($_POST['data']))
{
echo $_POST['data'];
}
?>
I want two <option> output form database by while loop and also need to receive it form $_POST[''] function but i am getting only one <option> output. double <option> not appears form these codes. what wrong i am doing here?
$q6 = mysql_query("SELECT * FROM menu");
echo '<form action="" method="post">'.'Need To Change <select name="tobechanged">';
while ($row = mysql_fetch_array($q6)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
while ($row = mysql_fetch_array($q6)) {
$menu_name2 = $row['menu_name'];
echo '<option value="'.$menu_name2.'">'.$menu_name2.'</option>';
}
echo '</select><br>
<input type="submit" name="pchange_submit" value="Change it">
</form>';
if (isset($_POST['pchange_submit'])) {
echo $_POST['tobechanged'];
}
$q6 = mysql_query("SELECT * FROM menu");
echo '<form action="" method="post">'.'Need To Change <select name="tobechanged">';
while ($row = mysql_fetch_array($q6)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
<input type="submit" name="pchange_submit" value="Change it">
</form>';
if (isset($_POST['pchange_submit'])) {
echo $_POST['tobechanged'];
}
i want to echo selected parent value. but i am getting error- Notice: Undefined index:
How can i echo selected parent value then? Whats wrong i am doing?
$q = mysql_query("SELECT * FROM menu");
echo '<form action="" method="post">
Menu name:<input type="text" name="mname"><br>
<select>';
while ($row = mysql_fetch_array($q)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
<input type="submit" name="submit" value="Add Menu">
</form>';
if (isset($_POST['submit'])) {
echo $mname = $_POST['mname'];
echo $parent = $_POST[$menu_name];
}
add name to the select box and get the value of select box by name.
Updated code:-
$q = mysql_query("SELECT * FROM menu");
echo '<form action="" method="post">
Menu name:<input type="text" name="mname"><br>
<select name="menu_name">';
while ($row = mysql_fetch_array($q)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
<input type="submit" name="submit" value="Add Menu">
</form>';
if (isset($_POST['submit'])) {
echo $mname = $_POST['mname'];
echo $parent = $_POST['menu_name'];
}
$_POST[$menu_name] probably doesn't exist, because only two elements in your form have name attributes. The text input and the submit input.
option elements aren't posted as part of the form, but rather the selected option's value for the select element. But your select element has no name, therefore no key to use in the key/value pair, so it isn't posted.
Give the element a name:
<select name="someName">
Then in the POST, you would be able to fetch the selected value just as you do for any other form element:
$_POST['someName']
You need to add name attribute to select tag.
echo '<form action="" method="post">
Menu name:<input type="text" name="mname"><br>
<select name="any_name">';
$q = mysql_query("SELECT * FROM menu");
while ($row = mysql_fetch_array($q)) {
$menu_name = $row['menu_name'];
echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
<input type="submit" name="submit" value="Add Menu">
</form>';
if (isset($_POST['submit'])) {
echo $mname = $_POST['mname'];
echo $select_option_name = $_POST['any_name'];
}
Note: mysql_* functions are depricated, use mysqli_* functions
I don't know what I have done to this but I had the form working and now it has stopped. My problems are with the bottom if statement where I am trying to delete the user. As I said it was working properly and now it has stopped. I just can't figure out what the issue is? And yes I do backup but clearly should do it more!
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
if(isset($_POST['action2'])){
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
}
}
Use the code like this,
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="hidden" name="DelRooMId" value="'.$roomID.'">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
}
}
if(isset($_POST['action2'])){
$roomID = $_POST['DelRooMId']; // sanitize the input
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
I have pulled the DELETE block outside of your code. It may help you. The problem with your code is $_POST['action'] will be null at the time of DELETE operation , so the code never executes, that may be the error
Note - Make sure to get the $roomID when you are using the below structure