I have an issue in a simple PHP MySQL project. When I try to fetch a record from a select option, it only gives me the id. I want the value from the select to be passed too.
<form class="form-style-9" method="POST" action="function.php?type=updatedistrict">``
<ul>
<li>ID:
<input type="hidden" name="dist_id" class="field-style field-full align-none" value="<?php echo $ids;?>" readonly />
</li>
<li>District Name:
<input type="text" name="name" class="field-style field-full align-none" value="<?php echo $names;?>" />
</li>
<li>Postal Code:
<input type="text" name="code" class="field-style field-full align-none" value="<?php echo $codes;?>"/>
</li>
<li>Province
<select name="province_id" class="form-control">
<option value="">Select Province</option>
<?php
$sql = mysqli_query($conn, "SELECT id, province From province");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['id'] ."'>" .$row['province'] ."</option>" ;
}
?>
</select>
<li>
<input type="submit" value="Update" name="submit" />
</li>
</ul>
</form>
This is the update function code:
case 'updateprovince':
if (isset($_POST['submit'])) {
$id = $_POST['dist_id'];
$prov_id= $_POST['province_id'];
$query=$conn->query("UPDATE province SET province='$prov_id' where id='$id'");
if ($query) {
echo '<script> window.location.replace("province.php");
</script>';
}
else
{
echo "issue";
}
}
break;
You can post your province name instead of the id this way:
echo "<option value='". $row['province'] ."'>" .$row['province'] ."</option>" ;
Your $_POST['province_id'] will then contain the province value instead of the current id.
A way to pass along both the id and the value (in case you need to retain the id for some additional processing) through your option would be this:
echo "<option value='". $row['id'] . '|'. $row['province'] ."'>" .$row['province'] ."</option>" ;
Then after the submit, you'd simply split the two by the '|' delimiter.
if (isset($_POST['submit'])) {
$id = $_POST['dist_id'];
$province = explode('|', $_POST['province_id']);
$province_id = $province[0]
$province_name = $province[1]
Related
This is process_upcategory.php
I want to update the category name or the category id with another category name/id by its category id or by its category name.
I'm new to php
<?php
require('includes/config.php');
if(!empty($_POST))
{
$msg=array();
if(empty($_POST['cat']))
{
$msg[]="Please full fill all requirement";
}
if(!empty($msg))
{
echo '<b>Error:-</b><br>';
foreach($msg as $k)
{
echo '<li>'.$k;
}
}
else
{
$cat_nm=$_POST['cat[0]'];
$cat_id=$_POST['cat[1]'];
$query= "UPDATE `category` SET cat_nm='$cat_nm' WHERE cat_id='$cat_id'";
mysqli_query($conn,$query) or die("can't Execute...");
mysql_close($link);
header("location:category.php");
}
}
else
{
header("location:index.php");
}
?>
Now this is category.php, just a snippet of code. Not whole code
<form action='process_upcategory.php' method='POST'>
<b style="color:darkgreen">UPDATE CATEGORY </b> <br>
<b style="color:darkgreen">Old Category</b>
<br>
<select name="cat[]" multiple>
<?php
$query="select * from category ";
$res=mysqli_query($conn,$query);
while($row=mysqli_fetch_assoc($res))
{
echo "<option>".$row['cat_nm'];
echo "<option>".$row['cat_id'];
}
?>
</select>
<br>
<b style="color:darkgreen">New Category</b><br>
<input type='text' name='cat[0]'></input><br>
<input type='text' name='cat[1]'></input>
<input type='submit' value=' UPDATE '>
</form>
I want to update the category name with another category name by its category id or by its category name. I get undefined index cat[0] and cat[1]
When you end an input name with [] it wil be converted to an array by php. The correct way to get the values in this case would be something like this:
$cat=$_POST['cat'];
$cat_nm=$cat[0];
$cat_id=$cat[1];
I combined the two routines into one script.
I added 'sub' to the form to distinguish from when the form was submitted or not.
I used list() in the query result loop.Used mysqli_fetch_array($result, MYSQLI_NUM) rather than mysqli_fetch_assoc($res)
used foreach() to loop through the $_POST['cat']
Added 'value' to the <option value=""> to hold the id
Eliminated the switching from HTML mode to PHP mode by using HEREDOC.
<?php
if (intval($_POST['sub']) == 1){
$newcat = $_POST['new'];
foreach($_POST['cat'] as $key=>$value){
if(strlen($newcat[$key]) > 0){
mysqli_query($conn,"UPDATE `category` SET `cat_nm`='$newcat[$key]' WHERE `cat_id`='$value'");
}
}
}
echo <<<EOT
<html><head><style>h4,h3{color:darkgreen;margin:.2em;}</style></head><body>
<form action="#" method='POST'>
<h3>UPDATE CATEGORY</h3>
<h4>Old Category</h3>
<select name="cat[]" multiple>
EOT;
$sql="SELECT `cat_nm`, `cat_id` FROM `category` ";
$result=mysqli_query($conn,$sql);
while(list($cat_nm,$cat_id) = mysqli_fetch_array($result, MYSQLI_NUM)){
echo " <option value=\"$cat_id\">$cat_nm</option>\n";
}
echo <<<EOT
</select>
<h3>New Category</h3>
<input type="text" name="new[0]" /><br/>
<input type="text" name="new[1]" /><br/>
<input type="hidden" name="sub" value="1" /><br/>
<input type="submit" value=" UPDATE />
</form>
</body></html>
EOT;
?>
hope you fine and well,
i have a drop down list that contains categories list as follows:
<div class='form-group'>
<br/>
<label class='control-label col-md-2 'for='id_date'>Category</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
</div>
</div>
below this select, i have another select which is to choose element from the category as follows :
<div class='form-group'>
<br/>
label class='control-label col-md-2 ' for='id_date'>element</label>
<div class='col-md-2' class='form-group' class='col-md-11'>
<select class="form-control" id="sel12" ng-model="elemnt" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT element FROM elements where category = ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['element'] . "'>" . $row['element'] . "</option>";
} ?>
</select>
</div>
</div>
how i can make the content of the second drop list to be based on the first drop list ?! e.g how i can put the input of the first drop list in the second SQL statement ?!
regards.
You can't do that with only php.
You must use javascript/Jquery and ajax.
Make a php script who load data from a request.
After change your first select use ajax function who call your php script with the right value and update the second select.
<select class="form-control " id="sel1" ng-model="category" ng-init="" >
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('my');
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
} ?>
</select>
Jquery
$("#sel1").change(function(){
$.ajax({
method: "POST",
url: "yourscript.php",
data: {myval : $(this).val()};
})
.done(function( msg ) {
//Here append your result in your second select
});
});
PHP
<?php
if(isset($_POST['myval']))
{
//SQL query where id=myval
echo $result;//result of query
}
Using pure PHP with intermediate submit:
<?php
if(isset($_POST['submitForm']) && $_POST['submitForm'] == 1){
//form is submitted by button, proceed with DB stuffs
echo 'Great, you have submitted the form, will check VALUES and do INSERT.';
}
?>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="aForm">
<input type="hidden" name="submitForm" id="submitForm">
<select name="category" onchange="this.form.submit();">
<option value="">Choose...</option>
<option value="1" <?=($_POST['category']==1 && !$_POST['submitForm'])?'selected':'';?>>Cat 1</option>
<option value="2" <?=($_POST['category']==2 && !$_POST['submitForm'])?'selected':'';?>>Cat 2</option>
<option value="3" <?=($_POST['category']==3 && !$_POST['submitForm'])?'selected':'';?>>Cat 3</option>
</select>
<select name="element">
<?php
if($_POST['category'] && !$_POST['submitForm']){
// SELECT from DB based on passed category ID
echo '<option value="">Now choose element...</option>';
echo '<option value="1">Elem 1</option>';
echo '<option value="2">Elem 2</option>';
echo '<option value="3">Elem 3</option>';
}else{
echo '<option value="">Choose category first...</option>';
}
?>
</select>
<input type="button" name="btnSubmit" value="Submit" onclick="document.getElementById('submitForm').value = 1; this.form.submit();">
</form>
</body>
However this is not either the best or most efficient approach, the script only demostrates how can be done without JS as is requested.
I am building a select option dynamically. I am selecting from the select option, one value. (values for list come from php
How can I pass that selected value to PHP?
<select id ="s1" name="swimopt" class="so">
<?php echo $options; ?>
</select>'
THe $options are coming from a MySQL and populating the dropdown
When I select a value, and try
echo $_POST['swimopt'];
does not show selected value
Please help
<form id="swimdata" method="POST" action="save.php">
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="girls">GIRLS
<table id="meetTable" style="width:auto">
<tr>
<th>EVENT:</th>
<th>NAME:</th>
<th>LANE:</th>
<th>TIME:</th>
<th>PLACE:</th>
<th>SCORE 1:</th>
<th>SCORE 2:</th>
<th>PLACE:</th>
<th>TIME:</th>
<th>LANE:</th>
<th>NAME:</th>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
<input type="hidden" name="action1" value="addSwimmer" id="action1">
</form>
This is my PHP getting the $options from mySQL
if ($result->num_rows > 0) {
$options= '';
// output data of each row
while($row = $result->fetch_assoc()) {
$options .= "<tr><td>" . $row["swimmer_id"]. "</td><td>" . $row["first_name"]. " " . $row["last_name"]. " </td><td> " . $row["school_name"]. "</td></tr>";
}
} else {
echo "0 results";
}
echo $options;
$conn->close();
ScreenShots:
Options echoed into select box:
<select id ="s1" name="swimopt" class="so">
<?php echo $options; ?>
</select>'
PHP file code
$sql = "select first_name, last_name from swimming where gender='m'";
$results = mysqli_query($link,$sql);
if ($results->num_rows > 0) {
$options = '';
// output data of each row
while($row = $results->fetch_assoc()) {
$fname=$row["first_name"];
$lname=$row["last_name"];
$options .= "<option value= >" . $row["first_name"] . " " . $row["last_name"]."</option>";
}
} else {
echo "0 results";
}
echo $options;
$link->close();
?>
I think I see what your problem is. You're not setting a select option.
Try this:
<?php
$options = 'John';
?>
<form id="swimdata" method="POST" action="save.php">
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="girls">GIRLS
<table id="meetTable" style="width:auto">
<tr>
<th>EVENT:</th>
<th>NAME:</th>
<th>LANE:</th>
<th>TIME:</th>
<th>PLACE:</th>
<th>SCORE 1:</th>
<th>SCORE 2:</th>
<th>PLACE:</th>
<th>TIME:</th>
<th>LANE:</th>
<th>NAME:</th>
</tr>
</table>
<select id ="s1" name="swimopt" class="so" value="">
<?php
for($i=0;$i<10;$i++){
echo '<option value="user '.$i.'" >user '.$i.'</option>';
}
?>
</select>
<input type="submit" name="formSubmit" value="Submit" />
<input type="hidden" name="action1" value="addSwimmer" id="action1">
</form>
The select option must be wrapped with html tag <option> with a value attribute, which will be posted if it is selected.
According to your given code you are not generating options for a select. So try something like this:
if ($result->num_rows > 0) {
$options= '';
// output data of each row
while($row = $result->fetch_assoc()) {
$options .= "<option value=\"". $row["swimmer_id"] ."\">" . $row["first_name"]. " " . $row["last_name"]. " - " . $row["school_name"]. "</option>";
}
} else {
echo "0 results";
}
echo $options;
$conn->close();
Now if you $_POST it you will get the swimmer id
<form method="POST" name="mailform" action="sendmail.php">
<fieldset>
<?php
require_once("mysql_connect.php");
$sql = mysql_query( " SELECT NAME FROM TABLE WHERE ID = ( SELECT ID FROM TABLE2 WHERE COLUMN = '$USERNAME') ORDER BY NAME");
echo "<select>";
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row["NAME"]."'>".$row["NAME"]."</option>";
}mysql_free_result($sql);
echo "</select>";
?>
<input type="text" name = "name" placeholder = "name Required" ><br \><br \>
<input type="checkbox" name="instance[]" value="yes" checked="checked" \>instance1><br>
<input type="checkbox" name="instance[]" value="Yes" \>instance2<br><br \>
<input type="submit" name="email" value="Send Mail">
</fieldset> <br \>
</form> <br \><br \>
This the part of the code. I need to build sendemail.php which should have the value from dynamic select list, and value from the check boxes.
<?php
require_once("mysql_connect.php");
$sql = mysql_query( " SELECT NAME FROM TABLE WHERE ID = ( SELECT ID FROM TABLE2 WHERE COLUMN = '$USERNAME') ORDER BY NAME");
echo "<select name='selection'>";
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row["NAME"]."'>".$row["NAME"]."</option>";
}mysql_free_result($sql);
echo "</select>";
?>
change your php code with above.. you'll get $_POST['selection'] from dynamic select list.
Edit:
You'll get $_POST['instance'] array.
<?php
$selected = $_POST['selection'];
$instances = array();
foreach ($_POST['instance'] as $instance)
{
$instances[] = $instance;
}
?>
Now, you'll have $selected as dynamic selected value and $instances have array of checked checkboxes.
To get the value of a selected item in a list
<select id="items" name="animal">
<option value="0">--Select Animal--</option>
<option value="Cat">Cat</option>
</select>
$selected_val = $_POST['items'];
To get the selected checkbox values
Red <input type="checkbox" name="color[]" id="color" value="red">
Green <input type="checkbox" name="color[]" id="color" value="green">
$colorsChecked = $_GET['color'];
foreach ($color as $colorsChecked)
{
echo $color;
}
your select box must has a name.
echo "<select name='cbouser'>";
after post you need to use $_POST["cbouser"] or $_REQUEST["cbouser"]
I have the following code:
<select name="to" class="combo" value='
<?php
if(isset($_POST['reply']))
{
echo "<option value='$reply'>$reply</option>";
}
?>
' />
<?php
$q = $database->selectAllUsersNotMe();
while($row=mysql_fetch_assoc($q))
{
$u=$row['username'];
echo "<option value=\"$u\">$u</option>";
}
?>
</select>
What this does is produce a combo box with a dropdown for all users on my site excluding the user sending the message.
I am trying to add a reply element to the message.
When i click reply, i use the following code:
<? $reply = $_POST['rfrom']; ?>
<form name='reply' method='post' action='/newmessage.php'>
<input type='hidden' name='rfrom' value='<?php echo $pm->messages[0]['from']; ?>' />
<input type='hidden' name='rsubject' value='Re: <?php echo $pm->messages[0]['title']; ?>' />
<input type='hidden' name='rmessage' value='[quote]<?php echo $pm->messages[0]['message']; ?>[/quote]' />
<input type='submit' name='reply' value='Reply' />
</form>
The values are correct and definately pass the information using POST.
On the initial piece of code I provided, how can I alter this so the username that I am replying to is selected when I am replying, if not, the usernames are just listed.
Thanks
$fromname=(isset($_POST['rfrom'])) ? $_POST['rfrom'] : ''; //ought to validate $_POST
while($row=mysql_fetch_assoc($q)) {
$u=$row['username'];
$selected=($u==$fromname) ? 'selected="selected"' : '';
echo "<option value=\"$u\" $selected>$u</option>";
}
$replyUser = $_POST['rfrom'];
while($row = mysql_fetch_object($q))
{
if($row->username == $replyUser)
{
echo('<option value="'.$row->username.'" selected="selected">'.$row->username.'</option>');
}else{
echo('<option value="'.$row->username.'">'.$row->username.'</option>');
}
}