WHAT I STARTED WITH
My php
<?php
mysql_select_db("$db") or die(mysql_error());
$sql = "SELECT kpi FROM pin_kpi_types";
$query = mysql_query($sql);
echo '<select name="KPI" style="width: 400px">';
while ($row = mysql_fetch_assoc($query)) {
echo '<option>'.$row['KPI'].'</option>';
}
echo '</select>';
?>
My current label/input method that requires me to manually type.
What I am trying to do is pull a query from a table to select the only available KPI types.
This is working now.
<p>
<label for="KPIType" id="preinput">Choose KPI Type: </label>
<input type="text" name="kpi_type" required placeholder="Lead" id="inputid" />
</p>
WHERE I WAS AT PREVIOUS
Edit; Unfortunately the answer posted by Lucky didn't quiet help me, but it steered me in the right way.
I am now have duplicate fields? It isn't posting the value into the DB. either.
Here is a screenshot...
http://s30.postimg.org/o1c2l9yr5/Untitled.png
Can someone steer me in right direction of where I went wrong? :)
Thank you in advance for any help.
<?php
include('db.php');
$select=mysql_query("SELECT * FROM pin_kpi_types");
$i=1;
while( $userrow=mysql_fetch_array($select) )
{
$kpi_id =$userrow['KPI_ID'];
$kpi =$userrow['KPI'];
?>
<style>
#CreateStatusTypeFORM label {display: inline-block;width: 10em; text-align: right;padding-right: 0.5em;}
</style>
<div id="CreateStatusTypeFORM">
<form action="insert.php" method="post" name="insertform">
<p>
<div align='center'><label for="StatusColor" id="preinput">Choose A Color: </label>
<ui-colorpicker ng-model="targetColor"></ui-colorpicker></div>
<input type="hidden" name="pinstatus_color" value="{{targetColor}}" id="inputid" />
</p>
<p>
<label for="StatusName" id="preinput">Set Status Name: </label>
<input type="text" name="pinstatus_type" required placeholder="Come Back" id="inputid"/>
</p>
<p>
<label for="StatusName" id="preinput">Available KPI Types: </label>
<select name="<?php echo $kpi; ?>" style="width: 237px">
<option name="KPI" required id="inputid" value="<?php echo $kpi; ?>"><?php echo $kpi; ?></option>
</select>
</p>
<?php } ?>
<input type="submit" name="send" value="Submit" id="inputid1" />
</p>
</form>
WHERE I AM AT NOW
This is where I am at. I now can get the dropdown correct without duping it over and over. However, now I cannot get the value to post the actual selected KPI to the DB. everything else submits data to the db, but KPI_type option.
<style>
.StatusForm {padding-left:75px;}
.CreateStatusTypeFORM {padding:25px;border-style:solid; border-color:solid black; width: 500px;background-color:#C4C4C4;}
#CreateStatusTypeFORM label {display: inline-block;width: 100em;}
#CreateStatusTypeFORM input {border-color:solid black;}
#CreateStatusTypeFORM input[type=text] {padding:5px; border:1px solid #666; -webkit-border-radius: 5px; border-radius: 5px;}
</style>
<div class='StatusForm'>
<div class="CreateStatusTypeFORM">
<H3> ADD New Status </H3>
<form action="insert.php" method="post" name="insertform">
<p>
<label for="StatusColor" id="preinput">Choose A Color: </label>
<ui-colorpicker ng-model="targetColor"></ui-colorpicker>
<input type="hidden" name="pinstatus_color" value="{{targetColor}}" id="inputid" required placeholder=""/>
</p>
<p>
<label for="StatusName" id="preinput">Set Status Name: </label>
<input type="text" name="pinstatus_type" required placeholder="" id="inputid"/>
</p>
<p>
<label for="KPI" id="preinput">Available KPI Types: </label>
<select>
<?php
include('db.php');
$select2=mysql_query("SELECT * FROM pin_kpi_types ORDER BY KPI_ID DESC");
while( $userrow=mysql_fetch_array($select2) )
{
$kpi =$userrow['KPI'];
$kpi_id =$userrow['KPI_ID'];
?>
<option name="kpi_type" required id="inputid" value="<?php echo $kpi; ?>"><?php echo $kpi; ?></option><?php } ?>
</select>
</p>
<input type="submit" name="send" value="Submit" id="inputid1" />
</p>
</form>
</div>
</div>
This is the insert function.
<?php
ob_start();
include("db.php");
if(isset($_POST['send'])!="")
{
$pinstatus_type =mysql_real_escape_string($_POST['pinstatus_type']);
$kpi_type =mysql_real_escape_string($_POST['kpi_type']);
$pinstatus_color =mysql_real_escape_string($_POST['pinstatus_color']);
$update =mysql_query("
INSERT INTO pin_status_types(
pinstatus_type,
kpi_type,
pinstatus_color,
created
)
VALUES(
'$pinstatus_type',
'$kpi_type',
'$pinstatus_color',
now()
)
");
if($update)
{
$msg="Successfully Updated!!";
echo "<script type='text/javascript'>alert('$msg');</script>";
header('Location:index.php');
}
else
{
$errormsg="Something went wrong, Try again";
echo "<script type='text/javascript'>alert('$errormsg');</script>";
}
}
ob_end_flush();
?>
mysql_select_db("$db") or die(mysql_error());
$sql = "SELECT kpi FROM pin_kpi_types";
$query = mysql_query($sql);
echo '<label for="KPIType" id="preinput">Choose KPI Type: </label>';
echo '<select name="KPI" style="width: 400px" required id="inputid">';
echo '<option value=''>Lead</option>';
while ($row = mysql_fetch_assoc($query)) {
echo '<option value='.$row['KPI'].'>'.$row['KPI'].'</option>';
}
echo '</select>';
<?php
include 'config.php';
$sql_states=mysqli_query($conn,"SELECT * FROM master_states");
?>
<section Name="state_name">
<option value="">section1</option>
<option value="">section2</option>
</section>
Related
Good Day! StackOverFlow Community
I have the below-mentioned code where Multi-Dependent Dropdown shows up with data. When I try to insert in MYSQL DB, only ID is inserted not the name whereas I am inserting the name
I surely am making mistake somewhere, need your precious guidance
Thanks
<?php
require_once ("DBController.php");
$db_handle = new DBController();
$query = "SELECT * FROM categories";
$countryResult = $db_handle->runQuery($query);
?>
<html>
<head>
<TITLE>Dynamically Load Dependent Dropdown on Multi-Select using PHP and
jQuery</TITLE>
<head>
<style>
body {
width: 610px;
font-family: calibri;
}
.frmDronpDown {
border: 1px solid #7ddaff;
background-color: #C8EEFD;
margin: 2px 0px;
padding: 40px;
border-radius: 4px;
}
.demoInputBox {
padding: 10px;
border: #bdbdbd 1px solid;
border-radius: 4px;
background-color: #FFF;
width: 50%;
}
.row {
padding-bottom: 15px;
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"
type="text/javascript"></script>
<script>
function getState() {
var str='';
var val=document.getElementById('country-list');
for (i=0;i< val.length;i++) {
if(val[i].selected){
str += val[i].value + ',';
}
}
var str=str.slice(0,str.length -1);
$.ajax({
type: "GET",
url: "get_state.php",
data:'country_id='+str,
success: function(data){
$("#state-list").html(data);
}
});
}
</script>
</head>
<body>
<div class="form-group" style="font-family:arial;">
<label for="landnumber">Land Number:</label><b><label style="float:right;">رمز الأرض</b></label>
<select dir="rtl" lang="ar" id='landnumber' name='landnumber' class="form-control" required>
<option disabled selected>-- رمز الأرض --</option>
<?php
include "connect.php"; // Using database connection file here
$records = mysqli_query($db, "SELECT landnumber From landnumbers"); // Use select query here
while($data = mysqli_fetch_array($records))
{
echo "<option value='". $data['landnumber'] ."'>" .$data['landnumber'] ."</option>"; // displaying data in option menu
}
?>
</select>
</div>
<br />
<form method="post" action="">
<div class="frmDronpDown">
<div class="row">
<label>Categories - التصنيفات </label><br /> <select name="country[]"
id="country-list" class="demoInputBox"
onChange="getState();" multiple size=4>
<option value="">Select Categories</option>
<?php
foreach ($countryResult as $country) {
?>
<option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>Activities - الأنشطة</label><br /> <select name="state[]"
id="state-list" class="demoInputBox" multiple size=5>
<option value="">الأنشطة</option>
</select>
</div>
<input type="submit" name="submit" id="action" class="btn btn-info" value="Insert" />
</form>
</div>
</body>
</html>
<?php
if(isset($_POST['submit'])){
require('connect.php');
$state_string = implode(', ', $_POST['country']);
$sql = '
INSERT INTO
`chosen_activities` (
`name`
)
VALUES (
"'. $state_string .'"
)';
mysqli_query($db,$sql);
}
?>
This below line of code inserts only ID not the name, I want to insert name
<option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>
Thanks
What I have Tried
<option value="<?php echo $country["name"]; ?>"><?php echo $country["name"]; ?></option>
I have changed id to name but then other dropdown doesn't populate as it is populating by ID only. You can see AJAX above bringing get_state
From your information, I get that you are trying to insert a name instead of an ID,
I hope you are using ID as int in mysql corresponding table, Here you have to know Int only allow numbers to insert, So you have to create a separate column for name field or you have to change the data type of the Id column to allow insert.
For good Practices your multi-dependent dropdown keeps id as foreign key
change the value in $country['name']
<option value="<?php echo $country["name"]; ?>"><?php echo $country["name"]; ?></option>
In a select input, the value of the selected option is submitted to the backend on form submit.
So if you want to submit the name instead of id you just have to change the values of the options.
<option value="<?php echo $country["name"]; ?>"><?php echo $country["name"]; ?></option>
This will submit the name.
Or else you can submit the ID and run a separate SQL query on PHP side to get the name.
<option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>
Then you can access the variable in $_POST['country-list'] and run a select query to get the name from the database.
So, this is how I did that keeping ID same above in drop down output and not modifying anything else
if (isset($_REQUEST['country'])) {
$i=implode(',', $_REQUEST['country']);
$query = "SELECT name FROM categories where id IN ($i)";
$result = $db->query($query);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["name"]."<br>";
}
} else {
echo "0 results";
}
$db->close();
}
Thanks to all those who helped ...
I would like to specify the duration of the display of a form. Suppose there is a variable with the value x=(10), whenever I call this page, the timer should be based on the current time and the form should be displayed for 10 minutes and after this time the form should be sent automatically.
<?php
include 'db_conn.php';
if (isset($_GET['id'])) {
$var=$_GET['id'];
$query="SELECT * FROM addquiz WHERE quz_id='$var' ";
$db=mysqli_query($conn,$query);
$res=mysqli_fetch_array($db);
$noquestions=$res['noquestion'];
$quiz=$res['title'];
}?>
<div class="container">
<form method="POST" class="form-horizontal" >
<div style="margin-left: 30%;"><b>Your Quiz: <?php echo $quiz;?></b>
<input type="text" name="user_name" placeholder="Enter Your Name" style="height: 30px; width: 30%;"></div>
<div class="row">
<div class="col-md-12">
<div class="panel" style="margin:5%">
<?php
$count=1;
$que="SELECT * FROM addques WHERE quz_id='$var'";
$dbd=mysqli_query($conn,$que);
while ($cmd=mysqli_fetch_array($dbd)) {
$quest=$cmd['qusname'];
$ans_id=$cmd['ans_id'];
$opt1=$cmd['qpta'];
$opt2=$cmd['optb'];
$opt3=$cmd['optc'];
$opt4=$cmd['optd'];
$answ=$cmd['answer'];?>
<b>Question <?php echo $count++;?> :<br><?php echo $quest;?></b><br><br>
<fieldset>
<input type="hidden" name="ansid[]" value="<?php echo $ans_id; ?>">
<input type="checkbox" name="ans[]" value="1"><?php echo $opt1;?><br><br>
<input type="checkbox" name="ans[]" value="2"><?php echo $opt2;?><br><br>
<input type="checkbox" name="ans[]" value="3"><?php echo $opt3;?><br><br>
<input type="checkbox" name="ans[]" value="4"><?php echo $opt4?><br><br><br>
</fieldset>
<?php } ?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
<div class="button" style="margin-left: 60%;"> View Result </div>
</div>
</div>
</div>
<?php
$i=0;
if (isset($_POST['submit'])) {
$name=$_POST['user_name'];
while ( $i<$noquestions ) {
$query="INSERT INTO `result`( `quz_id`, `ans_id`, `answer`,`user_name`) VALUES('";
$query.=$var . "', '";
$query.=$_POST['ansid'][$i] . "', '";
$query.=$_POST['ans'][$i] . "', '";
$query.=$name . "' )";
$db=mysqli_query($conn,$query);
$i++;
}
}
?>
It depends on what you are trying to achieve.
As you say "at each visit of the page" it could be done like this with javascript:
window.onload = function () {
window.setTimeout(function () {
document.form.submit();
}, 5000);
};
Further you could set the timeout from your php variable and like the comment says above, you could validate the taken time on the server side with php to make sure no one is cheating.
I am trying to display the saved data in the database on select before a user can update other choice. I am not really sure how to code it. Can anyone come out with possible solution? Had tried many ways but unable to do so.
Update: I have found out the problem why the options does not show.
This is my code:
`
$consentId = $_GET['consent_id'];
$retrieveConsent = "SELECT * FROM consent, leavetype WHERE consent.type_of_leave = leavetype.type_of_leave";
$retrieveResult = mysqli_query($link, $retrieveConsent) or die("Retrieve Error" . mysqli_error($link));
$queryleavetype = "SELECT * FROM leavetype";
$queryleaveresult = mysqli_query($link, $queryleavetype) or die("Leave Retrieve Error " . mysqli_error($link));
$row = mysqli_fetch_array($retrieveResult);
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit Consent </title>
</head>
<body>
<div class="ui-content">
<h3><b>Edit Leave</b></h3>
<form action="doEditConsent.php" method="post" data-ajax="false">
<input type="hidden" name="cId" value="<?php echo $row['consent_id']; ?>"/>
<label for="dateFrom" ><b>Date From:</b></label>
<input type="date" id="dateFrom" name="newDateFrom" value="<?php echo $row['consent_date_from']; ?>" required>
<br>
<label for="dateTo" ><b>Date To:</b></label>
<input type="date" id="dateTo" name="newDateTo" value="<?php echo $row['consent_date_to']; ?>" required>
<br>
<label for="reason" ><b>Leave Type:</b></label>
<select name="leaveType" id="leaveType" data-mini="true">
<?php
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
?>
<option value="<?php echo $rowleave['type_of_leave']; ?>">
<?php echo $rowleave['leave_type']; ?>
</option>
<?php
};
?>
</select>
<br>
<button class="ui-btn ui-corner-all" type="submit" >Submit</button>
</form>
</div>
<?php
}
}
?>
</body>
</html>`
Try this, hopefully it will help you:
<select name="leaveType" id="leaveType" data-mini="true">
<option value=""></option>
<?php
$previous_selected_value = 'previous_selected_value';//get the previous value and assign it to this variable
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
$selected = ($rowleave['type_of_leave'] == $previous_selected_value) ? " selected='selected'" : "";
echo '<option value="'.$rowleave['type_of_leave'].'"'.$selected.'>'.$rowleave['leave_type'].'</option>';
}
?>
</select>
I currently have a form as you will see below. For simplicity sake the form is condensed to the checkboxes that I'm working with. What I want to have happen is for a user to optionally click one or the other. Depending on which one is clicked, the other one should be disabled. I noticed that user "dinah" posted a similar question before and that this js fiddle was provided http://jsfiddle.net/A5TGf/19/ . However, for some reason when I applied my form id and switched the blacklist names around it wasn't working with my code. Any suggestions? Thanks
HTML/PHP
<form name="insertTicket" id="insertTicket" action="index.php" method="POST" >
<div id="ticket_hidden" style="text-align: center; clear:both;">
<div id="visible_div" style="float: left;">
<input type="checkbox" name="escalated" id ="escalated" value="Yes" onclick="doInput(this);" tabindex="18">Escalate
</div>
<div id="hidden_div" style="float: left; display:none;">
<?php
$dept = $_SESSION['dept_id'];
$get_email = mysql_query("Select email_name, dept_id, copy_user from emails where escalated = 'yes'");
while(($email = mysql_fetch_assoc($get_email)))
{
$department = explode(",", $email['dept_id']);
if(in_array($dept, $department, TRUE))
{
echo '
<input type="checkbox" name="escalated_to[]" id="escalated_to[]" value="'.$email['email_name'].'" />'.$email['email_name'].'
';
if($email['copy_user'] == 'YES')
{
echo '
<input type="text" name="emails[]" style="width: 175px; height: 20px;" placeholder="To:">
';
}
}
}
?>
</div>
<div id="visible_divX" style="float: left;">
<input type="checkbox" name="email" id="email" value="Yes" onclick="doInputs(this);" tabindex="20">Send Email </div></td></tr>
<div id="hidden_divX" style="float: left; display:none; ">
<?php
$dept = $_SESSION['dept_id'];
$get_email = mysql_query("Select email_name, dept_id, copy_user from emails where escalated != 'yes'");
while(($email = mysql_fetch_assoc($get_email)))
{
$department = explode(",", $email['dept_id']);
if(in_array($dept, $department, TRUE))
{
echo '
<input type="checkbox" name="emailed_to[]" id="emailed_to[]" value="'.$email['email_name'].'" />'.$email['email_name'].'
';
if($email['copy_user'] == 'YES')
{
echo '
<input type="text" name="emails[]" style="width: 175px; height: 20px;" placeholder="To:">
';
}
}
}
?>
</div>
</form>
If you need to disable a checkbox by Javascript, you just need one line:
document.getElementById("foo").setAttribute("disabled","disabled")
and to enable it again,
document.getElementById("foo").removeAttribute("disabled")
I don't think you need to use jQuery for such task...
Good luck!
This Html coding with css and php.I am getting an error that you result variable is not declareed.but i had declare it in my form.please check and tell me about this error.
calculator
this style .css file
<style>
form {
display:block;
background-color: #333399;
width:300px;
height:500px;
border:thick;
border: #330000;
color: #FFCC00;
}
h1 {
text-align:center;
z-index: 2px;
}
</style>
This is Php coding
if(isset($_POST['add'])){
$first_value = $_POST['f_value'];
$sec_value = $_POST['s_value'];
//--calculation variables---//
$result = $first_value + $sec_value;
}
?>
</head>
Html form starts from here
<body>
<form method="post" action="new.php" name="calculator">
<h1> calculator</h1>
<p>
<strong>Frsit value</strong>
<input type="text" name="f_value" >
<p><strong>Second value</strong> <input type="text" name="s_value" maxlength="50">
<p>
<input name="add" type="submit" value="add" >
<!-- <input name="sub" type="submit" value="sub">
<input name="sub" type="submit" value="multiply">
<input name="sub" type="submit" value="divide">-->
`enter code here`<p>
<h2 style="border:thick">Result
<input type="text" maxlength="50" value="<?php echo $result ; ?>" Name='result' >
</h2>
</form>
</body>
</html>
Use isset here
<input type="text" maxlength="50" value="<?php if(isset($result)) { echo $result; } ?>" Name='result' >
if(isset($_POST['add'])){
$first_value = $_POST['f_value'];
$sec_value = $_POST['s_value'];
$result = $first_value + $sec_value;
}
else{
$result= '';
}
Your $result is falling out of scope:
<?php
if(isset($_POST['add'])) { //scope begins here
//php omitted for brevity
//$result is declared within this scope
$result = $first_value + $sec_value;
} // scope ends here - after this point, $result no longer exists!
?>
<!-- html omitted for brevity -->
<!-- This is OUTSIDE the scope where $result was declared - we can't get it any more! -->
<input type="text" maxlength="50" value="<?php echo $result ; ?>" Name='result' >
To solve this problem, first declare $result within the same scope as you intend to echo it:
<?php
$result = 0;
if(isset($_POST['add'])) {
//php omitted for brevity
//change $result's value
$result = $first_value + $sec_value;
}
?>
<!-- html omitted for brevity -->
<input type="text" maxlength="50" value="<?php echo $result ; ?>" Name='result' >
Futher information about variable scope