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'];
}
Related
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'];
My question is: I want to show all SQL answers from a specific table inside the <select> using php.
I have tried to get it working, but without much success as it only shows one result.
<?php
error_reporting(E_ALL);
include 'connect.php';
include 'header.php';
set_time_limit(10);
$cat = $bdd->query('SELECT cat_id,cat_name,cat_description FROM categories');
$categories_list = $cat->fetch();
While ($categories_list = $cat->fetch()) {
$cat_name = $categories_list['cat_name'];
}
$cat->closeCursor();
echo '<form method="post" action="accès/create_topic_post.php">';
echo '<label for="sujet">Sujet :';
echo '<input type="text" name="sujet" id="sujet" required autofocus>';
echo '</label>';
echo '<label for="cat">Catégories :';
echo '<select name="topic_name">';
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
echo '</select>';
echo '<input type="submit" value="Envoyer">';
echo '</form>';
?>
That's totally not the way to do it. Have a separation between PHP and HTML. And make sure you have the option echoed out inside the loop. Something like this:
<form method="post" action="accès/create_topic_post.php">
<label for="sujet">Sujet :
<input type="text" name="sujet" id="sujet" required autofocus>
</label>
<label for="cat">Catégories :
<select name="topic_name">
<?php
// Loop it here.
while ($categories_list = $cat->fetch()) {
$cat_name = $categories_list['cat_name'];
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
}
?>
</select>
</label><!-- You forgot this -->
<input type="submit" value="Envoyer" />
</form>
There are two approach to achieve your desired output.
Approach One:
Store cat_name in a new array and use it in your HTML or anywhere, where you want to use.
<?php
$cat_name = array();
while($categories_list = $cat->fetch()) {
$cat_name[] = $categories_list['cat_name'];
}
?>
<select name="topic_name">
<?php
foreach ($cat_name as $key => $value) {
?>
<option value="<?=$value?>" ><?=$value?> </option>
}
?>
</select>
Approach 2:
<?php
while ($categories_list = $cat->fetch()) {
?>
<option value="<?=$categories_list['cat_name']?>" ><?=$categories_list['cat_name']?> </option>
<?php
}
?>
Whats wrong with your code:
$cat_name = $categories_list['cat_name']; this will only store the last value of your query, you must need to store the value in an array or use <option> inside the while() loop.
You keep replacing cat_name every time you read data from the SQL server. You need to store it in an array
<?php
error_reporting(E_ALL);
include 'connect.php';
include 'header.php';
set_time_limit(10);
$cat = $bdd->query('SELECT cat_id,cat_name,cat_description FROM categories');
//$categories_list = $cat->fetch(); This line discards a row of data
$cat_names = array(); //array for category names
While ($categories_list = $cat->fetch()) {
//Add newest 'cat_name' to the array
$cat_names[] = $categories_list['cat_name'];
}
$cat->closeCursor();
echo '<form method="post" action="accès/create_topic_post.php">';
echo '<label for="sujet">Sujet :';
echo '<input type="text" name="sujet" id="sujet" required autofocus>';
echo '</label>';
echo '<label for="cat">Catégories :';
echo '<select name="topic_name">';
//Loop and read back each category name
foreach ($cat_names as $cat_name){
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
}
echo '</select>';
echo '<input type="submit" value="Envoyer">';
echo '</form>';
?>
You don't have to loop over results manually, as PDO has a function that can do it for you already, called fetchAll(). You have to use it instead of fetch() if you want to get an array of rows.
However, beside it, you need to learn PHP and programming in general. Because your current code makes little sense. To output an array, you have to use a loop:
$cat = $bdd->query('SELECT cat_name FROM categories');
$categories_list = $cat->fetchAll(PDO::FETCH_COLUMN); // here it is
echo '<form method="post" action="accès/create_topic_post.php">';
echo '<label for="sujet">Sujet :';
echo '<input type="text" name="sujet" id="sujet" required autofocus>';
echo '</label>';
echo '<label for="cat">Catégories :';
echo '<select name="topic_name">';
foreach ($categories_list as $cat_name)
{
echo "<option value='$cat_name'>$cat_name</option>";
}
echo '</select>';
echo '<input type="submit" value="Envoyer">';
echo '</form>';
Move the fetch loop to the output section of your code.
So instead of this:
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
move the loop so it will look like this:
While ($categories_list = $cat->fetch()) {
$cat_name = $categories_list['cat_name'];
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
}
Use while loop while you're outputting the category in select option.
Like this,
While ($categories_list = $cat->fetch()) {
echo "<option value=\"".$categories_list['cat_name']."\" >$categories_list['cat_name'] </option>";
}
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 have a droplist <select> list in html. How do I identify the selected value in the $_POST array after the user submits the form ?
<form action="subj_exec.php">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value=';
echo $subject_id;
echo '> ';
echo $name;
echo '</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
</form>
The subject_id is blank in another php file
echo $_POST['subject_id'] is blank.
Please help to identify the issue in the code.
Thanks,
The standard method for forms is GET, so you need to add method="POST" to your form.
get: Default. Appends the form-data to the URL in name/value pairs: URL?name=value&name=value
post: Sends the form-data as an HTTP post transaction
<form action="subj_exec.php" method="POST"> //<<<< added method
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
?>
<option value="<?= $subject_id; ?>"><?= $name; ?></option>
<?php
}
?>
</select>
<input type="submit" value="submit" name="submit" />
In your file subj_exec.php, you can output the selected value with
echo $_POST['subj_id'];
Here you go :
index.php
<form action="subj_exec.php" method="POST">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<select name="subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value="'.$subject_id.'">'.$name.'</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
subj_exec.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['submit'])) {
if(strlen($_POST['subj_id']) >= 1) {
$option = htmlentities($_POST['subj_id'], ENT_QUOTES, "UTF-8");
// Do Something here with $option
echo $option;
}else {
echo 'nothing selected.';
}
}
?>
I have this code and I'm trying to put the selected state in a subcat table.
So far it returns an empty value. I'm not sure if this is clear or not, but all I want is: select a state from the select option and submit it. I want to get the selected state name into my table subcat.
enter <?php
include("connect.php");
$state = $row['states']; //Select name
if (isset($_POST[submit])){
$query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form> here
Thanks
Change $state = $row['states'] to $state = $_POST['states']
<?php
include("connect.php");
if (isset($_POST[submit]))
{
$state = $_POST['states']; //Select name
$query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>"; // if you want to
//get the name into table, then use like this
//echo "<option value='$row[name]'>$row[name]</option>"; or
//echo "<option>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form>
Try this:
enter <?php
include("connect.php");
if (isset($_POST[submit])){
$state = $_POST['states'];
$query = "INSERT INTO subcat (sub_name) VALUES ('".mysql_real_escape_string($state)."')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo '<select name="states" id="states">
<option value="">Select a state</option>';
while ($row = mysql_fetch_assoc($sql)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
echo '</select>';
?>
<input type="submit" name="submit" value="Continue" />
</form> here
Dont forget to use mysql_real_escape_string to prevent SQL injections. I have replaced $state = $row['states']; with $state = $_POST['states'];
I dont know where u got $row from...
The above will insert the states name into the database.