Unable to echo value from html <option> from while loop - php

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

Related

Post dynamic checkbox ids with values

Here is a form which contains dynamic checkbox retrieved from database table named "categories". All i am trying is to echo category id + name on a new page. I want all the selected categories with their ID's. the problem is the output shows the category names but it shows the last category id with each name.
Form
<form method="post" action="insert_try.php" enctype="multipart/form-data">
<label>Select categories</label>
<?php
$result = mysqli_query($con,"select * from categories");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
?>
<br><input type="checkbox" name="categories_chk[]" value="<?= $row["cat_name"]; ?>"> <?= $row["cat_name"]; ?>
<input type="text" name="cat_chk_id" value="<?= $row["cat_id"]; ?>">
<?php
}
}
?>
<input type="submit" name="submit" value="Add">
</form>
insert_try.php
if(isset($_POST['submit'])) {
$cat_chk = $_POST['categories_chk'];
$cat_chk_id=$_POST['cat_chk_id'];
foreach ($cat_chk as $checkbox) {
echo $checkbox;
}
echo $cat_chk_id;
I don't know why you need this but in your case I would do this:
while($row = mysqli_fetch_array($result)) {?>
<br><input type="checkbox" name="categories_chk[<?= $row["cat_id"]; ?>]" value="<?= $row["cat_name"]; ?>"> <?= $row["cat_name"]; ?>
<?php
}
See, I set index of categories_chk as a category id.
If you print_r($_POST['categories_chk']) you will have a key=>value array where key is category id and value is it's name.
You can iterate over it kinda:
foreach ($_POST['categories_chk'] as $id => $name) {
echo 'Category id ' . $id . ' with name ' . $name;
}
Of course, I need to mention that $row["cat_id"] must be unique for such approach, otherwise you will have two same keys in array and the latter overwrites the previous one.

Pre-Populate Form with Dropdown Menu in PHP/MySQL?

I am creating a simple CRUD application that will be used as a blog. For the edit page, I want to have a dropdown menu with the blog titles of each post. When an option/blog post is selected, I want it to populate the "Title" and "Message" fields, so it can then be edited and saved to the database.
I got it to retrieve the titles of the blog posts, but I am struggling to make it populate the "Title" and "Message" fields so it can be edited when the option is selected.
I have 4 rows in my database: row[0] is the title, row[1] is the message, row[2] is the timestamp and row[3] is the ID.
Thanks guys. I appreciate it.
<form action="edit.php" id="myform" method="post" autocomplete=off>
<input type="hidden" name="action" value="show">
<p><label>Entry:</label><select name="blog">
<?php
$result = mysqli_query($con, "SELECT * FROM blog");
while ($row = mysqli_fetch_array($result)) {
$chosen = $row['bid'];
}
if (isset($_GET['blog'])) {
$id = $_GET['blog'];
$result = mysqli_query($con, "SELECT * FROM blog WHERE bid='$id'");
$row = mysqli_fetch_array($result);
}
$result = mysqli_query($con, "SELECT * FROM blog");
while ($row = mysqli_fetch_array($result)) {
$id = $row['bid'];
$title = $row['title'];
$selected = '';
if ($id == $chosen) {
$selected = "selected='selected'";
}
echo "<option value='$id' $selected>$title</option>\n";
}
?>
</select></p>
<p><label>Title:</label> <input type="text" id="newtitle" name="newtitle" value="<?php echo $row[0]; ?>"></p>
<p><label>Message:</label> <input type="text" id="newmessage" name="newmessage" value="<?php echo $row[1]; ?>"></p>
<p><input type="hidden" name="id" value="<?php echo $row[3]; ?>"></p>
<br><p><input type="submit" name="submit" value="Submit"></p>
</form>
I have managed to somewhat figure out the answer on my own. It's probably not the most ideal way of doing it, but for anyone else potentially stuck on this - here is my code:
The only issue I'm having now is figuring out how to keep my option selected.
<form action="edit.php" id="myform" method="post" autocomplete=off>
<input type="hidden" name="action" value="show">
<p><label>Entry:</label><select name="blog" onchange="window.location.href = blog.options[selectedIndex].value">
<option selected disabled>Select One</option>
<?php
$result = mysqli_query($con, "SELECT * FROM blog");
while ($row = mysqli_fetch_array($result)) {
$id = $row['bid'];
$title = $row['title'];
echo "<option value='edit.php?edit=$row[bid]'>$title</option>\n";
}
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$result = mysqli_query($con, "SELECT * FROM blog WHERE bid='$id'");
$row = mysqli_fetch_array($result);
}
?>
</select></p>
<p><label>Title:</label> <input type="text" id="newtitle" name="newtitle" value="<?php echo $row[0]; ?>"></p>
<p><label>Message:</label> <input type="text" id="newmessage" name="newmessage" value="<?php echo $row[1]; ?>"></p>
<p><input type="hidden" name="id" value="<?php echo $row[3]; ?>"></p>
<br><p><input type="submit" name="submit" value="Submit"></p>
</form>

Two html <option> output from php while loop not works

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'];
}

Submitting always gets the last value

<form action="" method="post">
<?php
include 'Includes/database_connection.php';
$sql = "select * FROM sims" ;
$result = mysql_query($sql,$con);
while($row = mysql_fetch_assoc($result)){
?>
<ul class="category_list">
<input type="hidden" value="$id1" name="hidden">
<li><?php echo $row['phonenr'];?><input type="hidden" value="<?php echo $row['id'];?>" name="id"></li>
</ul>
<?php
}
?>
<input type="submit" name="submit">
</form>
So i got the above form where you can select phonenumbers and when you submit them a database should be updated. And there are 23 id's in it. After submitting the form it always takes the last value. What am i doing wrong?
if(#$_POST ['submit'])
{
$id = $_POST["id"];
echo $id;
include 'Includes/database_connection.php';
mysql_query("UPDATE pairings SET sim_id='$id'
WHERE unit_id='$id1'")
}
Change your hidden field name to array like this
<input type="hidden" value="<?php echo $row['id'];?>" name="id[]">
then on PHP side use loop to retrieve
foreach ($_POST['id'] as $val) {
$id = $val;
include 'Includes/database_connection.php';
mysql_query("UPDATE pairings SET sim_id='$id'
WHERE unit_id='$id1'")
}
Slight modification specified by chandresh_cool, would get the result that you expect.
The input name is replaced with id, so the post key contains only the row[id], not the $_POST['id']
Instead change the name of the hidden field to accept as a array like this
<input type="hidden" value="<?php echo $row['id'];?>" name="id[]">
Then you can iterate id array as specified by chandresh_cool

Dynamic DropDownList works but how to get value? PHP

Hi I have the following code:
$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
And this one (databaseupdate.php):
<?php
$username = "root";
$password = "usbw";
$hostname = "localhost";
$db_name = "examples";
$tbl_name = "cars";
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$nameupdate = $_POST['nameupdate'];
$yearupdate = $_POST['yearupdate'];
$test = $_POST['test'];
$query = "UPDATE cars SET name = '$nameupdate', year = '$yearupdate' WHERE id='$test'";
mysql_query($query) or die (mysql_error());
if($query){
echo "Successful";
echo "<BR>";
echo "<a href='databaseconnect.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
So, the list is populated by all the car names from the database, which is good but from this point I don't know how to get the id that comes with the car's name.
I have tried it with a query that says "wher id='$test'" (as you can see in my code) but that returns empty.
Example of what I want:
If I select "Mercedes" from the dropdownlist and want to update it with the textboxes I want my code to know that Mercedes has an ID of 1.
I don't know how I can do that and I hope someone can help me out here.
Thanks.
First of all include your dropdown inside the form.
Second if you want ID for your drop down selection, fetch id from db for the car name in $result, then do like this:
$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['ID']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
echo $dropdown;
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
Done.
do like this: (just add dropdown inside your form)
<?php
$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['id']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";
?>
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
<?php echo $dropdown; ?>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<select name='test'>
<?php while($row = mysql_fetch_assoc($result)) {
echo "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
}?>
</select>
<input type="submit" value="Update">
</form>
in your databaseupdate.php
$values = $_POST['test'];
$values contains selected values
The main problem is that you didn't included your select into form tag. Also to get values from POST method you should use $_POST in PHP or $_REQUEST
to check what $_POST contains you can use function
var_dump($_POST);

Categories