Dynamic drop down list NON-DATABASE php - 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>

Related

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>

Temperature Conversion PHP

I have been working on the following simple temperature conversion using HTML and PHP. It is my first attempt at using PHP and I can't seem to get it right.
I am sure there are lots of errors in my code so please be patient!
Here is what I have:
<!DOCTYPE HTML>
<html>
<head>
<title>Temp Conversion</title>
<meta charset="utf-8">
<body>
<form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table>
<tr>
<td>Enter value to convert</td>
<td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
</tr>
<tr>
<td>Convert to:</td>
<td><select name="convertType" id="convertType" size="1">
<option disabled> Select a measurement type</option>
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
<td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
</tr>
</form>
<?php
$valueConvert = $_POST['valueConvert'];
$convertType = $_POST['convertType'];
function tempConvert($valueConvert, $convertType){
if($convertType == "fahrenheit"){
$conversion = ((9/5)*$valueConvert) +(32);
}
else if ($convertType == "celsius"){
$conversion = ($valueConvert - 32) * (9/5);
}
return $conversion;
echo "The initial temperature was $valueConvert. The new temperature is $conversion.";
}
?>
</body>
</html>
I can't figure out how to pass the users textbox input and dropdown list selection to the php function.
You pretty much have things set up alright. You are not calling the function though.
You echo statement comes after the return statement and will never be executed.
It would be better to do something like this:
<?php
function tempConvert($valueConvert, $convertType)
{
if($convertType == "fahrenheit"){
$conversion = ((9/5) * $valueConvert) + (32);
}
else if ($convertType == "celsius"){
$conversion = ($valueConvert - 32) * (5/9);
}
return $conversion;
}
$valueConvert = $_POST['valueConvert'];
$convertType = $_POST['convertType'];
$conversion = tempConvert($valueConvert, $convertType);
echo "The initial temperature was $valueConvert. The new temperature is $conversion.";
?>
First of all as i wrote in the comment you forgot to cancel the head tag, and second of all you need to check if the convert button is closed and i fixed your convert function aswell, doesn't know how good it's working, im not pro at php either but hopefully it works :)
<html>
<head>
<title>Temp Conversion</title>
<meta charset="utf-8">
</head>
<body>
<form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table>
<tr>
<td>Enter value to convert</td>
<td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
</tr>
<tr>
<td>Convert to:</td>
<td><select name="convertType" id="convertType" size="1">
<option disabled> Select a measurement type</option>
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
<td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
</tr>
</form>
<?php
function tempConvert($value, $type){
if($type== "fahrenheit"){
return (((9/5)*$value) +(32));
}
elseif ($type== "celsius"){
return (($valueConvert - 32) * (9/5));
}
}
if (isset($_POST['btnConvert'])) {
$valueConvert = $_POST['valueConvert'];
$convertType = $_POST['convertType'];
echo "The initial temperature was $valueConvert. The new temperature is tempConvert($valueConvert, $convertType).";
}
?>
</body>
</html>

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>

how do i apply Criteria::LIMIT for upper and lower or is there another way?

i have this code in indexSucces.php
//some html for the main page
<td width='48%' align='right'>
<?php include_partial('login', array('form' => $form)) ?>
</td>
in the actions.class.php for index i have:
public function executeIndex()
{
$this->form = new RcLoginForm();
$this->setTemplate('index');
$this->search_form = new RcSearchForm();
$this->age_form = new RcAgeTableForm();
}
public function executeListmatches(sfWebRequest $request)
{
}
then the partial _login.php code:
<form action="<?php echo url_for('password/listmatches' ) ?>" method="post" >
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>I am a:</span>
<select id="gender1" name="gender1">
<option value="male1" >Male</option>
<option value="female1" selected="selected">Female</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>Seeking a:</span>
<select id="gender2" name="gender2">
<option value="male2" >Male</option>
<option value="female2" selected="selected">Female</option>
</select>
</td>
</tr>
<tr>
<td>
<span class='spn_med_lightblue_rbc'>Age:</span>
<select id="age1" name="age1">
<?php $ages = RcAgeTablePeer::getById(18);
foreach ($ages as $age)
{
//echo $age->getId();
echo "<option>";
echo $age->getId();
echo "</option>";
} ?>
</select>
</td>
<td>
<span class='spn_med_lightblue_rbc'>To:</span>
<select id="age2" name="age2">
<?php foreach ($ages as $age)
{
//echo $age->getId();
echo "<option>";
echo $age->getId();
echo "</option>";
} ?>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>Location:</span>
<select id="province" name="province">
<option value="all" selected="selected">All</option>
<option value="ec">Eastern Cape</option>
<option value="nc">Northern Cape</option>
<option value="wc">Western Cape</option>
<option value="fs">Free State</option>
<option value="gp">Gauteng</option>
<option value="kzn">Kzn</option>
<option value="lim">Limpopo</option>
<option value="mpu">Mpumulanga</option>
<option value="nw">North West</option>
</select>
</td>
<td><input class='submit_img' type="image" src="/images/rainbow/gobuttonbluesmall.png" value="Submit" alt="Submit"></td>
</tr>
<tr></tr>
<tr></tr>
</form>
the above will give a home page with a section(the partial _login) that contains the login to the website and also a quick search when you hit the go button the form action will go to
action="
this is my dilemma..i am not in any class, i just pass on the selected values through POST and use those values to get my rows
in listmatchesSuccess.php
$matching_rows = RcProfileTablePeer::getAllBySelection($gender_id2,$age1,$age2,$province_id,$from,$limit);
where above in RcProfileTablePeer.php:
static public function getAllBySelection($gender2,$age1,$age2,$province_id,$from,$limit)
{
$criteria = new Criteria();
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
$criteria->add(RcProfileTablePeer::GENDER_ID,$gender2, Criteria::EQUAL);
if ($province_id <> 10)
$criteria->addAnd(RcProfileTablePeer::PROVINCE_ID,$province_id, Criteria::EQUAL);
$criteria->setLimit($limit);
return self::doSelect($criteria);
}
hope all this makes better sense now :)
thank you
I hope I understand what you are asking,
I you want to add a limit to your query
$criteria->setLimit(10);
and also if you want to create a pager in symfony, I would suggest the propel pager class
e.g.
$this->page = $request->getParameter('page', 1);
$c = new Criteria();
$c->add(UserPeer::Status_ID,1);
$this->pager = new sfPropelPager('User', $limit);
$this->pager->setCriteria($c));
$this->pager->setPeerMethod('doSelect');
$this->pager->setPage($this->page);
$this->pager->init();
You can reference this link http://www.symfony-project.org/cookbook/1_0/en/pager for more information .
I hope that answers your question;
There is an error in your query:
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
Resulting query: rc_profile.age >= :age1.
If you are looking for the objects with age between $age1 and $age2, this should do the trick:
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->addAnd(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
Resulting query: rc_profile.age >= :age1 AND rc_profile.age <= :age2
Note that you should use $criteria->addAnd or $criteria->addOr to combine the conditions FOR THE SAME COLUMN.

Categories