Get value from <select> in html - php

So I'm trying to get my value from a SELECT tag in HTML, I'm working with the POST method. Somehow when I get my answer back I only get my label, instead of getting my value which is what I need. I manage my form in 3 diferent documents,
here is my php doc, the visual part
<form action="actions_admin/demografica_asig.php" method="POST">
<div class="large-4 columns">
<label>Seleccione Periodo:
<select name="selec_periodo">
<option value="1">I</option>
<option value="2">II</option>
<option value="3">III</option>
<option vslue="4">IV</option>
</select>
</label>
</div>
</form>
my accion docmuent:
<?php
require_once("../../clases/demoasig.class.php");
$selec_periodo = $_POST['selec_periodo'];
$demoasig = new Demografica();
$demoasig->selec_periodo = $selec_periodo;
$resultdatos = $demoasig->demo_asig();
echo $resultdatos;
?>
my Class Document:
<?php
require_once('conexion.class.php');
class Demografica{
public $selec_periodo;
public function __construct(){
$this->asig = array();
}
public function demo_asig(){
$sql= "SELECT COUNT(*) FROM est_asig WHERE est_asig_trimestre={$this->selec_periodo}";
$resultado = mysql_query($sql, Conectar::conexion()) or die($sql);
$this->asig[]= $resultado;
return $this->asig;
}
}
?>

Related

Getting onchage value and through ajax and call it on a php function

<div class="card-body"><h2 class="text-success text-center">MONTHLY STATISTICS OF FORMS</h2><hr>
<select class="form-control">
<?php
$query = $class->chartData("Agriculture Department");// Change department via selected button without refreshing through ajax
foreach($query as $data)
{
$month[] = $data['MONTH'];
$approved[] = $data['APPROVED'];
$denied[] = $data['DENIED'];
$completed[] = $data['COMPLETED'];
$pending[] = $data['PENDING'];
}
$dept_name = $class->departments();
while ($row = $dept_name->fetch()) {
?>
<option selected value="<?php echo $row['req_dept'];?>"><?php echo $row['req_dept'];?></option>
<?php
}
?>
</select>
Im newbie to js so im not really familiar to the ajax syntax. I want to change the department everytime it is changed

Inserting values into database from form with drop downs

I'm trying to insert values input from the user in a form into my database.
I am trying to create 2 drop down lists, with the first deriving the options for the second. For example the first drop down list for Faculty, with the second drop-down list containing the schools within the selected faculty.
I am also then wanting to insert the gathered information into my database however I can focus on that after getting the drop-down's correct first.
My register page is on one page with the getSchool.php on a different file, I have a feeling the connection between the two could be my issue.
The register.php is below. This is the page the form is on
<?php
session_start();
include('dbConnect.php');
$queryStr=("SELECT * FROM faculty");
$dbParams=array();
// now send the query
$results = $db->prepare($queryStr);
$results->execute($dbParams);
?>
<html>
<head>
<TITLE>Faculty & School</TITLE>
<head>
<!-- Help for code to create dynamic drop downs -->
<script src="https://code.jquery.com/jquery-2.1.1.min.js"
type="text/javascript"></script>
<script>
function getFaculty(val) {
$.ajax({
type: "POST",
url: "getFaculty.php",
data:'facultyID='+val,
success: function(data){
$("#schoolList").html(data);
}
});
}
function selectFaculty(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
</head>
<body>
<div class="frmDronpDown">
<div class="row">
<label>Faculty:</label><br/>
<select name="faculty" id="facultyList" class="demoInputBox"
onChange="getFaculty(this.value);">
<option value="">Select Faculty</option>
<?php
foreach($results as $faculty) {
?>
<option value="<?php echo $faculty["facultyID"]; ?>"><?php echo
$faculty["facultyName"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<form action="addBlood.php" method="post">
<label>Test:</label><br/>
<select name="test" id="test-list" class="demoInputBox">
<option value="">Select Test</option>
</select>
</div>
</div>
<label>Result:</label><input class="input" name="result" type="text"><br>
<label>Date:</label><input class="input" name="date" type="date"><br>
<input class="submit" name="submit" type="submit" value="Submit">
</form>
Below is the getSchool.php which gets all the schools
<?php
include('dbConnect.php');
if(!empty($_POST["facultyID"])) {
$queryStr=("SELECT * FROM school WHERE facultyID = '" . $_POST["facultyID"]
. "'");
$dbParams=array();
// now send the query
$results = $db->prepare($queryStr);
$results->execute($dbParams);
?>
<option value="">Select School</option>
<?php
foreach($results as $school) {
?>
<option value="<?php echo $school["schoolID"]; ?>"><?php echo
$school["schoolName"]; ?></option>
<?php
}
}
?>
Thanks in advance for any feedback and help.
Simon
url: "getFaculty.php",
data:'facultyID='+val,
success: function(data){
$("#schoolList").html(data);
Where is the #schoolList element ? Why getFaculty.php? Should it not be getSchool.php ?
First, just to re-iterate what was already mentioned, update your getFaculty() to get getSchool() and make sure it points to getSchool.php.
Now, you need to create a div following your first drop-down with an id schoolList.
<div class="row" id="schoolList"></div>
Now, update your getSchool.php so that it generates the full form/selection. Something along the lines of:
<?php
include('dbConnect.php');
if(!empty($_POST["facultyID"])) {
$queryStr=("SELECT * FROM school WHERE facultyID = '" . $_POST["facultyID"]
. "'");
$dbParams=array();
// now send the query
$results = $db->prepare($queryStr);
$results->execute($dbParams);
?>
<label>Schools:</label><br/>
<select name="schoolSelect" id="schoolSelect" class="demoInputBox">
<option value="">Select School</option>
<?php
foreach($results as $school) {
?>
<option value="<?php echo $school["schoolID"]; ?>"><?php echo
$school["schoolName"]; ?></option>
Once you've got those ideas down, you'll have to make sure you have the full flow of your page the way you want it. Then follow similar standards for posting any inputs to the php page you use for database manipulation.
As noted in earlier posts, this solution still leaves you vulnerable to injection. That's for another post, another day.

How to fetch data from database and display in html dropdown?

I am creating a simple form with fields like First_name, Last_name, city etc.So for city field, I want to display dynamic data.Below is the code I am using it's in PHP CodeIgniter.
Controller Page:
public function city()
{
$this->load->model('dropdownM');
$getcity=$this->dropdownM->get_city();
$this->load->view('form1city',$getcity);
}
Model Page:
<?php
class DropdownM extends CI_Model
{
public function get_city()
{
$this->db->select('fname');
$this->db->from('city');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result();
}
}
}
View page:
<form action="<?php echo base_url(); ?>index.php/Rec/user" method="post">
<select class="form-control" id="city" name="city">
<option value="">Select </option>
<?php if(count($getcity)):?>
<?php foreach($getcity as $city):?>
<option value=<?php echo $city->c_id;?>><?php echo $village1->C_name;?></option>
<?php endforeach;?>
<?php else:?>
<?php endif;?>
</select>
<center>
<input type="submit" value="Submit" class="btn btn-bricky" id="subbtn"
name="submit1">
</center>
<form>
It's not displaying anything in the drop-down.I am not able to find out what the issue.
pass data like this
$data['getcity']=$this->dropdownM->get_city();
$this->load->view('form1city',$data);
and in view
<?php if(count($getcity) > 0):?>
<select class="form-control" id="city" name="city">
<option value="">Select </option>
<?php foreach($getcity as $city):?>
<option value=<?php echo $city['c_id'];?>><?php echo $village1['C_name'];?></option>
<?php endforeach;?>
</select>
<?php else:?>
<p>No Category Found</p>
<?php endif;?>
In model
$this->db->select('fname');
$this->db->from('city');
$query = $this->db->get();
return $query->result_array();

send the selected value from a dropdown menu to another page

I have a dropdownlist populated by a MySql database that shows the titles of books stored in my database.
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
I want the option I choose to send it to another php page search.php
This search.php I want it to get the title and search for this specific book details.(title, price, author.... etc) .I tried to do it with but it ruins the page.
Add below code in form that should work for you.
<form action='search.php' method='post'>
<select id="titles" name='titles'>
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
<input type='submit' value='submit'>
</form>
in search.php:
$title = $_POST['titles'];
You just need to add the form above the select tag and need to give the NAME attribute in the select tag to post the data on another page. You can try with the following code:
<form method="post" action="search.php">
<select id="titles" name="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
and on the search.php page, you can get the value of the dropdown by this:
$title = $_POST['titles'];
Try as below :
<form method="post" action="YOURPAGEPATH">
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
Without submit button :
<form method="post">
<select id="titles" onchange="this.form.submit();">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
</form>
Surround that with a form with the appropriate action and add a submit button. Otherwise use something like jQuery to listen for the value of that to change and submit the form.
For example:
<form action="search.php" method="GET">
<select id="titles" name="title">
<?php /* put your stuff here */ ?>
</select>
</form>
And then in jQuery:
$(function(){
$('#titles').on('change', function(){
$(this).closest('form').submit();
});
});
Or you could go real old-school and attach the event listener to the select like this:
<form action="search.php" method="GET">
<select id="titles" name="title" onchange="this.parentNode.submit()">
<?php /* put your stuff here */ ?>
</select>
</form>
Just in case if you don't want to use the Submit Button
<script language="Javascript">
function books(book)
{
var url="http://www.example.com/search.php/?q="+book;
window.open(url, "_self");
}
</script>
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option onClick="books('.$row['title'].')" value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
Here the list will call the Books function on Click and pass the arguments to the function which will redirect you to search.php
To retrieve the book name use
$_GET["q"]
Change the URL as required.
And if the problem is solved don't forget to Mark the answer.

Insert values into using mysql and bootstrap?

here is the bootstrap that i need to put into the database well im
new to bootstrap and mysql so i have no idea whether im doing it right
or wrong? i need some help badly. btw im making a simple online reservation system
<div class = "panel-group" id = "Accordion">
<div class = "panel panel-info">
<div class = "panel-heading">
<h4 class = "panel-title">
</span> Reserve Now!
</h4>
</div>
<!-- Panel Body -->
<div class ="panel-body">
<div class = "row">
<form class="form-inline" role="form" action ="" method = "post">
<div class="form-group">
<div class="input-group" name = "from">
<div class="input-group-addon">FROM</div>
<select class="form-control input-sm">
<option selected disabled>Select Destination</option>
<option value="BCD">Bacolod</option>
<option value="BSO">Basco(Batanes)</option>
<option value="USU">Busuanga(Coron)</option>
<option value="BXU">Butuan</option>
<option value="CGY">Cagayan de Oro</option>
<option value="CYP">Calbayog</option>
<option value="CRM">Catarman</option>
<option value="BOR">Catician(Boracay)</option>
<option value="CEB">Cebu</option>
<option value="COT">Cotabato</option>
<option value="DVO">Davao</option>
<option value="DPL">Dipolog</option>
<option value="DGT">Dumaguete</option>
<option value="GES">General Santos</option>
<option value="ILO">Iloilo</option>
<option value="JOL">Jolo</option>
<option value="KLO">Kalibo</option>
<option value="LAO">Laoag</option>
<option value="LGP">Legazpi</option>
<option value="MNL">Manila</option>
<option value="MBT">Masbate</option>
<option value="NAG">Naga</option>
<option value="OZC'">Ozamiz</option>
<option value="PPS">Puerto Princesa</option>
<option value="RXS">Roxas</option>
<option value="SUG'">Surigao</option>
<option value="TAC">Tacloban</option>
<option value="TAG">Tagbilaran(Bohol)</option>
<option value="TWT">Tawi Tawi</option>
<option value="TUG">Tuguegarao</option>
<option value="ZAM">Zamboanga</option>
</select>
</div>
</div>
and here is the mysql query that im tweaking right now . im using PDO. what should i do ? i edited this is now my full db.php its from my previous small blog post project.
<?php namespace ars\DB; // Global annoucement of location within the this page
$config = array(
'username' =>'root',
'password' => 'kel',
'database' => 'ars'
); // function can be reusable
//function will be put to class namespace (real world)
function connect($config)
{
try {
$conn = new \PDO('mysql:host=localhost;dbname=' .
$config['database'],
$config['username'],
$config['password']);
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $conn;
} catch(exception $e ) {
return false;
}
}
//helper function
function query($query, $bindings, $conn)
{
$stmt = $conn->prepare($query);
$stmt->execute($bindings);
return ( $stmt -> rowCount() > 0 ) ? $stmt : false;
}
function get($tableName, $conn)
{ try{
$result = $conn->query("SELECT * FROM $tableName ORDER BY id ");
return( $result -> rowCount() > 0 )
? $result
: false;
}catch(Execption $e){
return false;
}
}
// passing the id
function get_by_id($id, $conn)
{
// querying the id
$query = query(
'SELECT * FROM posts WHERE id = :id LIMIT 1',
array('id' => $id),
$conn
);
if ($query) return $query->fetchAll();
// else
}
there are many errors in this code. i cant help you with your problem but i can just point out some of these errors. first off,
there is no need for order by in insert
your function is get but you are inserting. (can i assume you are supposed to use select? if so, learn more about it. a little google search will save you).
you can help us help you by showing your connect statement. (like
mysqli_connect() function) note:dont use mysql_connect as it is
depreciated.
a note to point out is your
has no name attribute. add a name="some_Select" in your select tag

Categories