select option selection in php - php

I have a form for input order, there are 2 fields, branch and customer model. Before we click button "search", we must select branch and customer fields. After I filled the field, then click button search, the data appears. The problem is, after the data appears, where the condition branch and customer models are filled, I want to create another search, then I changed branch field only (customer model field was same with the previous search), change branch field from 'A' to 'B' and click button search, the data not appears / empty (whereas the data exists in database). Branch field,the field when I changed into B, after I click button search, the branch filled changed into
'SELECT' (branch option are SELECT -> A , B).
I'm confused with this problem.. here is the code :
<?php
if ($_POST)
{
$_SESSION['data_id'] = $_POST['data_id'];
$_SESSION['branch'] = $_POST['branch'];
$_SESSION['customer_model'] = $_POST['customer_model'];
}
?>
<form id="search" class="formular" method="post" action="search_data.php" style="z-index:1;">
<table width="100%">
<tr>
<td>Data ID :</td>
<td><input type="text" name="data_id" value="<?php echo $_SESSION['data_id'];?>"></td>
</tr>
<tr>
<?php
$branch = "'".str_replace(",", "','", $_SESSION['moi_status_UserBranch'])."'";
$check_data = $db->GetAll("select cab_id, cabang_name from company_profile where cab_id in(".$branch.")");
?>
<td>Branch :</td>
<td>
<select id="branch" name="branch">
<option value="0">Select</option>
<?php foreach($check_data as $row): ?>
<option <?php echo ($_SESSION['branch'] == $row['CAB_ID']) ? "selected" : ""; ?> value="<?php echo $row['CAB_ID']; ?>"><?php echo $row['CABANG_NAME']; ?></option>
<?php endForeach; ?>
</select>
</td>
</tr>
<tr>
<td width="110">Customer Model</td>
<td>
<select id="customer_model" name="customer_model" class="readonly validate[required]">
<option <?php echo ($_SESSION['customer_model'] == '0') ? "selected" : ""; ?> value="0">Pilih</option>
<option <?php echo ($_SESSION['customer_model'] == 'I') ? "selected" : ""; ?> value="I">Personal</option>
<option <?php echo ($_SESSION['customer_model'] == 'C') ? "selected" : ""; ?> value="C">Corporate</option>
</select>
</td>
</tr>
</table>
<input type="submit" id="btnsubmits" name="btnsubmit" class="button" value="Search" />
<a onclick="mypopup();" style="text-decoration:underline; cursor:pointer; font-weight:bolder">Add New</a>
</form>
<script>
function mypopup()
{
<?php unset($_SESSION['regional_saved']); ?>
mywindow = window.open("choose_custtype.php" ,"Item","width=700px,height=350px,resizable=1,scrollbars=1,top=150,left=200");
mywindow.moveTo(200, 200);
}

Related

How to show the last selected dropdown value after insert PHP values in Mysql?

I have a plenty of list of writers and publishers which are to be inserted in mysql through PHP, but every time I have to select between "Add Writer" and "Add Publisher" manually after inserting every writer/publisher details.
I want the selected values doesn't get changed automatically on inserting values in Mysql, (until it changed by user) i.e., selected option remains same (chosen by user) even after inserting the values, until changed by user.
index.php
<?php
include "dbConfig.php";
/*Adding Writer*/
if(isset($_POST['addwrt']))
{
$writerid=$_POST['writerid'];
$wname=$_POST['wname'];
$wcity=$_POST['wcity'];
$resultw=mysqli_query($db,"insert into wdetails(writerid,wname,wdesg,salutation,wcity)values('$writerid','$wname','$wdesg','$salut','$wcity')");
}
/*Adding Publisher*/
else if(isset($_POST['addpubl']))
{
$publisherid=$_POST['publisherid'];
$pname=$_POST['pname'];
$pcity=$_POST['pcity'];
$resultp=mysqli_query($db,"insert into pdetails(publisherid,pname,pdesg,pcity)values('$publisherid','$pname','$pdesg','$pcity')");
}
?>
<body>
<select id="stf">
<option value="cf" selected="selected" />Select the Field
<option value="addwriter" />Add Writer
<option value="addpublisher" />Add Publisher
</select>
<form method="post" name="libraryAdd">
<div id="addwriter" style="display:none">
<table>
<tr>
<td>Writer ID<span style="color:red"><b>*</b></span></td>
<td>W <input type='text' name="writerid"></td>
</tr>
<tr>
<td>Writer Name<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="wname" /></td>
</tr>
<tr>
<td>Writer City<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="wcity" /></td>
<td><input name="addwrt" type="submit" value="ADD Data"></td>
</tr>
</table>
</div>
<div id="addpublisher" style="display:none">
<table>
<tr>
<td>Publisher ID<span style="color:red"><b>*</b></span></td>
<td>P <input type='text' name="publisherid"></td>
</tr>
<tr>
<td>Publisher Name<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="pname" /></td>
</tr>
<tr>
<td>Publisher City<span style="color:red"><b>*</b></span></td>
<td><input type="text" name="pcity" /></td>
<td><input name="addpubl" type="submit" value="ADD Data"></td>
</tr>
</table>
</div>
</form>
</body>
.js file linked to the index.php
$(document).ready(function() {
$('#stf').change(function(){
var value=$(this).val();
if(value === "addwriter"){
$("#addwriter").show('slow');
$("#addpublisher").hide('slow');
}
else if(value === "addpublisher"){
$("#addpublisher").show('slow');
$("#addwriter").hide('slow');
}
else {
$("#addwriter").hide('slow');
$("#addpublisher").hide('slow');
}
});
});
dbConfig.php
<?php
//Database credentials
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'library';
//Connect and select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
The reason this happens is that in your select you have:
<option value="cf" selected="selected" />Select the Field
which means that the select will always start with "Select the Field" selected.
If you want the last selected option to remain selected after form submission, you should change the select code to:
<option value="cf" <?php if (!isset($_POST['addwrt']) && !isset($_POST['addpubl'])) echo 'selected="selected"'; ?> >Select the Field
<option value="addwriter" <?php if (isset($_POST['addwrt'])) echo 'selected="selected"'; ?> >Add Writer
<option value="addpublisher" <?php if (isset($_POST['addpubl'])) echo 'selected="selected"'; ?> >Add Publisher
You will also need to make the following changes to ensure the appropriate input boxes appear. Change
<div id="addwriter" style="display:none">
<div id="addpublisher" style="display:none">
to:
<div id="addwriter" <?php if (!isset($_POST['addwrt'])) echo 'style="display:none"'; ?> >
<div id="addpublisher" <?php if (!isset($_POST['addpubl'])) echo 'style="display:none"'; ?> >

Use of select tag to display database in textbox PHP

Im trying to display my database value into the textbox using drop down menu. which is i did and it is displaying. the problem here is that when i choose an item in the drop down list, it goes back to the first choice or last choice, the explanation i got was, my loop is selecting all of the items in the field causing the drop down menu to go back to the first choice when i click on other items. can you help me with the code on how to stop going back to the first choice when i select other options. Here is my whole code. i also use functions.
home.php
<?php
session_start();
include('dbconnect.php');
include('functions.php');
if(isset($_POST['brandname'])){
$id = $_POST['brandname'];
$result = mysql_query("SELECT * FROM tblstore WHERE brandname = '$id'");
while($row = mysql_fetch_array($result)){
$price = $row['price'];
$stocks = $row['stocks'];
}
}
?>
<html>
<body>
<form method="POST" name="">
<table align="center">
<tr>
<td>Choose here:</td>
<td>
<select name = "brandname" onchange = "this.form.submit()">
<?php dropdown() ?>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="qty" id="qty" value="" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" id="price" value="<?php echo $price ?>" disabled/></td>
</tr>
<tr>
<td>Stocks:</td>
<td><input type="text" name="stocks" id="stocks" value="<?php echo $stocks ?>" disabled/></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="total" id="total" disabled/></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<div align = "center">
hi' <?php echo $userRow['username']; ?> Sign Out
</div>
</body>
</html>
functions.php
<?php
function dropdown(){
$all = mysql_query("SELECT * FROM tblstore");
while($row = mysql_fetch_array($all)){
echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";
}
}
feel free to edit the whole code.. im a beginner in php and learning my way to it. thanks
Can add the multiple option if you need to select multiple
<select name="brandname" multiple>
<option value="Select">Select</option>
<?php
do {
?>
<option value="<?php echo $row['brandname']?>"> <?php echo $row['brandname'] ?></option>
<?php
} while ($row = mysql_fetch_assoc($all));
?>
</select>

php- check radio button then pass the value to mysql query

Good day every one, i am trying to check if a radio button were clicked, and iwant the value of that clicked radio button to pass on a variable, i will used that variable to compare records in database with the same values from it. and display all records on list box??
but when i try to run this code nothings happen
<td><input type="radio" name="1stChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course1">
<?php
include('dbconnection.php');
if(isset($_POST['1stChoice'])) {
if($_POST['1stChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['1stChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php $row['Program']; ?></option></td>
</select>
<?php } ?>
</tr>
<td width="20%">2nd choice:</td>
<td><input type="radio" name="2ndChoice" value="CHED" ></td>
<td><input type="radio" name="2ndChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course2">
<?php
include('dbconnection.php');
if(isset($_POST['2ndChoice'])) {
if($_POST['2ndChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['2ndChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php $row['Program']; ?></option></td>
</select>
<?php } ?>
I tested your code and as far as I can see, you're not echoing your <?php $row['Program']; ?> which should read as <?php echo $row['Program']; ?>
This is for both your <option> tags.
I also didn't notice any form tags <form></form>, so you will need to add those if you're not presently using them.
Using a submit button could be useful also. Although I'm not sure if you're using JS/jQuery with your code.
<input type="submit" name="submit" value="Submit">
Here is what I used to test it with, along with a few additions/modifications:
(I added form tags, a submit button and the echo for the <select> tags)
<form action="" method="post">
<td><input type="radio" name="1stChoice" value="TESDA" ></td><br>
<input type="submit" name="submit" value="Submit">
<td align = "center">
<select name="course1">
<?php
include('dbconnection.php');
if(isset($_POST['1stChoice'])) {
if($_POST['1stChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['1stChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php echo $row['Program']; ?></option></td>
</select>
<?php } ?>
</tr>
<td width="20%">2nd choice:</td>
<td><input type="radio" name="2ndChoice" value="CHED" ></td>
<td><input type="radio" name="2ndChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course2">
<?php
include('dbconnection.php');
if(isset($_POST['2ndChoice'])) {
if($_POST['2ndChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['2ndChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);
while($row=mysql_fetch_array($mysql_select))
{
?>
<option><?php echo $row['Program']; ?></option></td>
</select>
</form>
<?php } ?>
This is not a php work. IF your html is good, your browser will check the checked radio button. See! In your code, name must start with a letter or a _(underscore).
So, replace all name='2ndChoice' with
<td><input type="radio" name="ck_2ndChoice" value="CHED" ></td>
<td><input type="radio" name="ck_2ndChoice" value="TESDA" ></td><br>
and all name='1stChoice' with name='ck_1stChoice'

serach oracle database using multiple fields

I am using this code to serach database and I am using 4 fields in this code but this code does not serach database value.
where problem in this code tell me plz edit my code for full working serach with 4 fields
my code :
<?php
{
include ('connection.php');
if(isset($_REQUEST['submit'])){
$optid = $_POST['OPRID'];
$optdec = $_POST['OPRDEFNDESC'];
$empid = $_POST['EMPLID'];
$empmail = $_POST['EMAILID'];
$query ="SELECT * FROM OPERATOR WHERE OPRID LIKE '%".$optid."%'
or OPRDEFNDESC LIKE '%".$optdec."%' or EMPLID LIKE '%".$empid."%'
or EMAILID LIKE '%".$empmail."%' ";
}
else{
$query="SELECT * FROM OPERATOR";
$objParse = oci_parse ($ora_conn, $query);
}
?>
<form action="multi.php" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="500" border="1" align="center">
<tr>
<th>Operator ID
<input name="OPRID" type="text" id="OPRID" value="";>
<tr>
<th>Operator Name
<input name="OPRDEFNDESC" type="text" id="OPRDEFNDESC" value="";>
<tr>
<th>Person ID
<input name="EMPLID" type="text" id="EMPLID" value="";>
<tr>
<th>Email ID
<input name="EMAILID" type="text" id="EMAILID" value="";>
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<table>
<tr>
<td>Operator ID</td>
<td>Operator Name</td>
<td>Person ID</td>
<td>Email ID</td>
</tr>
<?
$success = oci_execute($objParse);
//while($objResult = oci_fetch_array($objParse,OCI_BOTH))
while($objResult = oci_fetch_array($objParse, OCI_RETURN_NULLS+OCI_ASSOC))
{
?>
<tr>
<td><div align="center"><?=$objResult["OPRID"];?></div></td>
<td><?=$objResult["OPRDEFNDESC"];?></td>
<td><?=$objResult["EMPLID"];?></td>
<td><div align="center"><?=$objResult["EMAILID"];?></div></td>
<td align="center"><a href="Optr_Edit.php?OprID=
<?=$objResult["OPRID"];?>">Edit</a></td>
</tr>
<?
}
?>
</table>
<?
oci_free_statement($objParse);
oci_close($ora_conn);
}
?>
Try like this
<tr>
<td><div align="right"><strong>Password Encrypted:</strong></div></td>
<td>
<select name="txtENCRYPTED">
<option value="">Select</option>
<option <?php if ($objResult["ENCRYPTED"] == "Y") {echo 'selected';} ?>value="Y">Y</option>
<option <?php if ($objResult["ENCRYPTED"] == "N") {echo 'selected';} ?> value="N">N</option>
</select>
</td>
</tr>
You are doing it wrong
Select element do not have value attribute
You have value attribute only in options element.
For eg:
<select name="txtENCRYPTED" id="txtENCRYPTED">
<option value="">Select</option>
<option value="Y">Y</option>
<option value="N">N</option>
</select>
In your code you have provided the db-retrived-data in tags setting .
The tag defines the menu. It can have the following settings
The name setting adds an internal name to the field so the program that handles the form can identify the fields.
The size option defines how many items should be visible at a time. Default is one item.
The multiple setting will allow for multiple selections if present.
The tag defines the single items in the menu.
The value setting defines what will be submitted if the item is selected.
one solution :-
<form method="post" action="" >
<select name="encrypt" value="encrypted" id='select'>
<option value="">Select</option>
<option value="<?php if($objResult["ENCRYPTED"]=='Y'){ echo 'Y'; } ?>">Y</option>
<option value="<?php if($objResult["ENCRYPTED"]=='N'){ echo 'N'; } ?>">N</option>
</select>
<input type="submit" value="submit" id='form'/>
</form>
</td>
</tr>
//script type jquery.js
//script type javascript
$(document).ready(function(){
$('form').submit(function(){
alert($('#select').val());
});
});
</script>

Dynamic drop down list NON-DATABASE php

I'm trying to create a changing drop down list in php, but without a database. Here's what I came up with so far
<body>
<form method = "post">
<table>
<tr>
<td>
<p>Name</p>
</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td><p>State</p>
</td>
<td>
<select name = "State">
<option value = "PA"> Pennsylvania</option>
<option value = "CA"> California</option>
<option value = "AZ"> Arizona</option>
<option value = "NY"> New York</option>
<option value = "FL"> Florida</option>
</select>
</td>
</tr>
<td><input type="submit" name = "formSubmit" value="Submit" /></td>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['formSubmit']))
{
if(empty($_POST['Name']))
{
echo("You forgot your name");
}
else
{
}
}
?>
</body>
I had planned to use the 'else' statement to generate my second drop down list, but so far nothing I've tried has worked. I've looked all over for ideas, but most of the information I've come across has dealt with databases. This isn't a database, it's a single PHP program. Maybe I have the wrong idea of how this works. Should I try calling a function that creates the form ahead of time or am I completely off base for what I'm attempting?
To do so you need javascript
There is already a question posted related to yours
javascript-dynamic-drop-down-box-update
jQuery would give you a true dynamic result without having to reload the page. However if you want to use PHP to add what is in the textbox to the list of values then you can use PHP session as an array to hold the values like this.
<?php
session_start();
if(isset($_POST['formSubmit']))
{
if(empty($_POST['Name']))
{
echo("You forgot your name");
}
else
{
$name = $_POST['Name'];
if(isset($_SESSION['List']))
{
$index = count($_SESSION['List']);
$_SESSION['List'][$index+1] = $name;
}
else
{
$_SESSION['List'][0] = $name;
}
}
}
?>
<body>
<form method = "post">
<table>
<tr>
<td>
<p>Name</p>
</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td><p>State</p>
</td>
<td>
<select name = "State">
<option value = "PA"> Pennsylvania</option>
<option value = "CA"> California</option>
<option value = "AZ"> Arizona</option>
<option value = "NY"> New York</option>
<option value = "FL"> Florida</option>
<?php
if(isset($_SESSION['List']))
{
foreach($_SESSION['List'] as $name)
{
echo '<option value="'.$name.'">'.$name.'</option>';
}
}
?>
</select>
</td>
</tr>
<td><input type="submit" name = "formSubmit" value="Submit" /></td>
</td>
</tr>
</table>
</form>
</body>

Categories