PHP - Accessing a value selected in Combobox - php

I want to write a code that should let me select from a drop down list and onClick of a button load the data from a mysql database.However I cannot access the value selected in the drop down menu.I have tried to access them by $_POST['var_name'] but still can't do it.
I'm new to PHP.
Following is my code:
<?php
function load(){
$department = $_POST['dept'];
$employee = $_POST['emp'];
//echo "$department";
//echo "$employee";
$con = mysqli_connect("localhost", "root", "pwd", "payroll");
$rs = $con->query("select * from dept where deptno='$department'");
$row = $rs->fetch_row();
$n = $row[0];
$rs->free();
$con->close();
}
?>
<html>
<head>
<title>Payroll</title>
</head>
<body>
<h1 align="center">IIT Department</h1>
<form method="post">
<table align="center">
<tr>
<td>
Dept Number:
<select name="dept">
<option value="10" selected="selected">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</td>
<td>
<input type="button" value="ShowDeptEmp" name="btn1">
</td>
<td>
Job:
<select name="job">
<option value="President" selected="selected">President</option>
<option value="Manager">Manager</option>
<option value="Clerk">Clerk</option>
<option value="Salesman">Salesman</option>
<option value="Analyst">Analyst</option>
</select>
</td>
<td>
<input type="button" value="ShowJobEmp" name="btn1">
</td>
</tr>
</table>
</form>
<?php if(isset($_POST['dept']) && $_POST['dept'] != "") load(); ?>
</body>
</html>

change button to submit
<input type="submit" value="ShowDeptEmp" name="btn1">
and
<input type="submit" value="ShowJobEmp" name="btn2">

Use a prepared statement instead of echoing $department into your SQL. If someone posted '; DROP TABLE dept; they could run arbitrary SQL commands (See SQL Injection).
OR you can use mysql_real_escape_string() when escaping $department if you don't want to use a Prepared Statement.

Related

Redirect to current page PHP pagination

first of all, I'm sorry, I am new in this community also my English is not so good, I hope you guys will understand. The question I want to ask is
Currently I'm doing my directory website project, that display all the data in database in pagination because of the massive data in database. User need to select the the state and cluster, then the website will show the list of result. But all the list must be in pagination. I expected when click the page link, it will take the user to 2nd page and so on.
current website display
But the actual result is, when I click the 2nd page, it shows nothing. Like this: the website page after click 2nd page
I am still new in PHP code. I've tried everything I could. I would like to ask, is there something wrong in my coding?
here is some of my coding for "index.php"
enter code here
<form method="post">
<div class="input">
<select name="state" class="btn" required>
<option value="" class="placeholder" selected disabled>Carian Mengikut Negeri</option>
<option value="Johor">Johor</option>
<option value="Kedah">Kedah</option>
<option value="Kelantan">Kelantan</option>
<option value="Melaka">Melaka</option>
<option value="Negeri Sembilan">Negeri Sembilan</option>
<option value="Pahang">Pahang</option>
<option value="Penang">Penang</option>
<option value="Perak">Perak</option>
<option value="Perlis">Perlis</option>
<option value="Sabah">Sabah</option>
<option value="Sarawak">Sarawak</option>
<option value="Selangor">Selangor</option>
<option value="Terengganu">Terengganu</option>
<option value="Wilayah Persekutuan">Wilayah Persekutuan</option>
</select>
<select name="cluster" class="btn" required>
<option value="">Kluster Perniagaan</option>
<option value="Makanan dan Minuman">Makanan & Minuman</option>
<option value="Gaya Hidup">Gaya Hidup</option>
<option value="Automotif">Automotif</option>
<option value="Pembinaan">Pembinaan</option>
<option value="Perkhidmatan">Perkhidmatan</option>
</select>
<input type="submit" name="submit" value="Cari" >
</div>
</form>
<div class="output">
<?php
include "connect.php";
?>
</div>
And here is my "connect.php" that has a query (sorry, i cant show the full code at code sample, so i use snippet)
<?php
$conn = new mysqli('localhost','root','','bpuvirtu_Directory') or die("Connection Failed");
if(isset($_POST["submit"]))
{
$result_per_page = 10;
$str= $_POST["state"];
$cluster=$_POST["cluster"];
$sth="SELECT * FROM TABLE3 WHERE state='$str' and service='$cluster'";
$result=mysqli_query($conn,$sth);
$number_of_results=mysqli_num_rows($result);
$number_of_results;
$number_of_pages= ceil($number_of_results/$result_per_page);
if(!isset($_GET['page'])){
$page=1;
}
else{
$page=$_GET['page'];
}
$this_page_first_result = ($page-1)*$result_per_page;
$sth="SELECT * FROM TABLE3 WHERE state='$str' and service='$cluster' LIMIT " . $this_page_first_result . "," . $result_per_page;
$result=mysqli_query($conn,$sth);
?>
<table id="user">
<tr>
<th>Nama Syarikat</th>
<th><center>Kluster</center></th>
<th><center>Info</center></th>
</tr> <?php
while($row=mysqli_fetch_array($result)){
?>
<tr>
<form method="GET" action=detail.php>
<td><?php echo $row["name"]; ?></center> </td>
<td><center><?php echo $row["service"]; ?></center> </td>
<input type='hidden' name='id' value='<?php echo $row["id"]; ?>'/>
<td><center><input type="submit" name="detail" value="Info" ></center></td>
</form>
</tr>
<?php
}?></table><?php
for($page=1;$page<=$number_of_pages;$page++){
echo '' . $page . '';
}
}
?>
So, when I click page 2, I want it show the list from database at page 2.
You can try this:
//$page = grab page number from the query param here.
return redirect('currencies?page='.$page);
or:
$results->url($page)

How to send query with select tag through url

I want send the data I received from the select tag through the url as queries so I can use it and query the database.
I have tried but the values are not showing. I am getting something like: type=&action=
This is my code
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
<a class="site-btn fs-submit" href="search.php?<?php echo $query; ?>"> Advanced search</a>
I expected :
type=2 bed room&action=rent
Try the following code. Here, When you submit the form the whole form data will be passed to search.php and the form method is POST so you can access those data by simply calling $_POST['attribute value of name'].
HTML form:
<form class="filter-form" method="post" action="search.php" enctype="multipart/form-data">
<input type="text "class="d-block d-md-inline" placeholder="Enter State, City or Area" name="city">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<input type="submit" value="Advanced search">
</form>
create a PHP file (search.php):
<?php
//Do what you want here
if (isset($_POST['city'])) {
$city = $_POST['city'];
echo $city;
}
if (isset($_POST['types'])) {
$type = $_POST['types'];
echo $type;
}
if (isset($_POST['action'])) {
$action = $_POST['action'];
echo $action;
}
?>
Replace the link with a button. Attach the query params in the link to the action of the form. Your code should look like below.
<form action="search.php?<?php echo $query; ?>">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<?php
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
?>
<button class="site-btn fs-submit" type="submit">Submit</button>
</form>

dynamic dropdown list-get values from mysql

i have this code.it is a dropdown list and a submit button.
I have a query "SELECT * DISTINCT column_name FROM table_name" that has result 15 values
Now,i want to take those values of the query and dynamicly enter them on the option=..... field.Also the "NAME" section must be the same as values.
example: IF value1="abcd"
<option value="abcd" >abcd</option>
<!DOCTYPE html>
<html>
<body>
<form method="post" target=".....php">
<select name="exa" >
<option value="value1" >NAME 1</option>
<option value="value2" >NAME 2</option>
<option value="value3" >NAME 3</option>
<option value="value4" >NAME 4</option>
<option value="value5" >NAME 5</option>
<option value="value6" >NAME 6</option>
<option value="value7" >NAME 7</option>
<option value="value8" >NAME 8</option>
<option value="value9" > NAME 9</option>
<option value="value10" >NAME 10</option>
<option value="value11" >NAME 11</option>
<option value="value12" >NAME 12</option>
<option value="value13" >NAME 13</option>
<option value="value14" >NAME 14</option>
<option value="value15" >NAME 15</option>
</select>
<form action=$value>
<input type="submit" value="GO!" />
</form>
</body>
</html>
i've made this but didnt work
<!DOCTYPE html>
<html>
<body>
<form method="post" >
<select name="exa" >
<?php
include_once "LOGIN TO DB SCRIPT";
$query_ak='SELECT DISTINCT (column_name) FROM table_name';
$result = mysql_query ($query_ak) or die (mysql_error);
while ($row = mysql_fetch_assoc($result)) {
}
?>
<option value = $row['ak_ex']> "$row['ak_ex']"</option>
<input type="submit" value="GO!" name="go"/>
</select>
</form>
</body>
</html>
You have made a lot of errors, first you've put the OPTION object outside the WHILE loop, then you put the submit button inside the SELECT object, in the end you write distinct as a function, the correct syntax is below.
I suggest you to use mysqli to avoid security problem and because mysql is going to be DEPRECATED. Use also include and not include_once because it requires extra work from PHP to render the file(Little difference but everything is welcome).
I've modified the code to use it, you find all the information to modify your script for working with mysqli at http://php.net/manual/it/book.mysqli.php
<html>
<body>
<form method="post" >
<?php
// LOGIN TO DATABASE SCRIPT WRITTEN FOR MYSQLI
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// END OF LOGIN TO DB SCRIPT
include DATABASE CONFIGURATION;
$query_ak='SELECT DISTINCT column_name FROM table_name';
$result = $mysqli->query($query_ak);
?>
<select name="exa" >
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
}
?>
</select>
<input type="submit" value="GO!" name="go"/>
</form>
</body>
</html>
You should placed <option>...</option> inside loop. Place submit button outside </select> tag. You also have syntax error. mysql_error should be mysql_error().
<!DOCTYPE html>
<html>
<body>
<form method="post" >
<?php
include_once "LOGIN TO DB SCRIPT";
$query_ak='SELECT DISTINCT (column_name) FROM table_name';
$result = mysql_query ($query_ak) or die (mysql_error());
?>
<select name="exa" >
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['ak_ex'].'">'.$row['ak_ex'].'</option>';
}
?>
</select>
<input type="submit" value="GO!" name="go"/>
</form>
</body>
</html>

display the output in the same page using php

im new to php.i created a page in php containing chk boxes ,input fields and retrive the data from the database(mysql) and also use the javascript.
i h've a problem in this ,that is i want to display the output in the same page.
i created the code like this,i want the output in same page,but it shows error.
<?php
if(isset($_POST['submit']))
{
$con=mysql_connect("localhost","demosdef_review","review123");
if(!$con)
{
die('could nt connect 2 the server');
}
mysql_select_db("demosdef_review", $con);
$state = $_POST["state"];
$country = $_POST['country'];
$contry = "USA";
if($country==1)
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='1' AND id='$state'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
else
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='$country'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
}
?>
enter code here
<div class=buy>
Dealer Enquiry
</div><br/>
<div class=buyfont>Authorized Retailers</div>
<div><img src="images/archivers.png"><br/>
<a href="http://www.archiversannex.com/Books-And-SoftwareSoftware-And-Accessoriesdefault.aspx?PageID=20&CategoryID=48"/>
<img src="images/logo_archivers_annex.png" ></a><br/><br/>
<br/><br/>
</div><br/>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>
<p>
<label for="country" style="padding-right: 2px;">Country</label><select name="country" value="countryid"
id="countryid"
onchange="sub()" style="width:120px;">
<option value="1">USA</option>
<option value="2">CANADA</option>
<option value="3">UK</option>
<option value="4">AUSTRALIA</option>
<option value="5">ITALY</option>
<option value="6">GUATEMALA</option>
<option value="7">NEW ZEALAND</option></select></p>
<p>
<label for="state" style="padding-right: 14px;">State</label>
<select id="state" name="state" value="state" style="width:120px">
<option value="7">Alabama</option>
<option value="8">Alaska</option>
<option value="9">Arizona</option>
<option value="10">Arkansas</option>
<option value="11">California</option>
<option value="12">Colorado</option>
<option value="13">Connecticut</option>
<option value="43">Florida</option>
<option value="14">Georgia</option>
<option value="15">Idaho</option>
<option value="16">Illinois</option>
</select></p> <br/>
<input class="dealer-submit" type="submit" value="Submit" onClick="buy_func()"/>
</form>
<script type="text/javascript">
function sub()
{
var x=document.getElementById("countryid").selectedIndex;
var y=document.getElementById("countryid").options;
var z = document.forms[0].state;
if(y[x].index==0){
z.disabled = false;}
else if(y[x].index>0) {
z.disabled = true;}}
</script>
Your HTML markup has an error
<a href="http://www.hobbylobby.com/storelocator"/>
It should be
<a href="http://www.hobbylobby.com/storelocator">
Also try
<form method="POST" action="">
Keeping the action blank will post it to the same page itself
And replace your submit button with this
<input class="dealer-submit" type="submit" name="submit" value="Submit" onClick="buy_func()"/>
Check the value of the submit button, if its set isset($_GET('submit')) then display the results
does that solve your problem?
submit is the type of the element, 'Submit' is the value you are looking for:
<?php
if(isset($_POST['Submit']){
}
?>
Remove the backslash at the end of the line:
<a href="http://www.hobbylobby.com/storelocator"/>
^

php drop down how to control the hide and show

i want to control the drop down box to control show or hide statement. I do like this but it seems it doesn't work, i have it working if im using radio button.
can help me with the code? which part am i wrong?
thank you.
$dbcnx = mysql_connect('localhost', 'root', '');
mysql_select_db('dbase');
if($_POST['gred'])$gred=$_POST['gred'];else $gred="";
<script language="JavaScript">
function funcHide(elemHide1,elemHide2,elemHide3)
{
document.getElementById(elemHide1).style.display = 'none';
document.getElementById(elemHide2).style.display = 'none';
document.getElementById(elemHide3).style.display = 'none';
document.getElementById(elemShow).style.visibility = 'visible';
}
function funcShow(elemShow1,elemShow2,elemShow3)
{
document.getElementById(elemShow1).style.display = 'block';
document.getElementById(elemShow2).style.display = 'block';
document.getElementById(elemShow3).style.display = 'block';
document.getElementById(elemShow1).style.visibility = 'visible';
document.getElementById(elemShow2).style.visibility = 'visible';
document.getElementById(elemShow3).style.visibility = 'visible';
}
</script>
<table>
<tr>
<td>Gred </td>
<td>:</td>
<td><select name="gred" id="gred">
<option value=""> </option>
<option value="A17" <?php if($gred=='A17')echo "selected";?> onClick="funcShow('box1', 'box2', 'box3');">A17</option>
<option value="A22" <?php if($gred=='A22')echo "selected";?>>A22</option>
<option value="A27" <?php if($gred=='A27')echo "selected";?>>A27</option>
</select>
</td>
</tr>
<tr>
<td>TK</td>
<td>:</td>
<td>
<select name="tk" id="tk">
<option value=""> </option>
<option value="01" <?php if($tk=='01')echo "selected";?>>01</option>
<option value="02" <?php if($tk=='02')echo "selected";?>>02</option>
<option value="03" <?php if($tk=='03')echo "selected";?>>03</option>
<option value="04" <?php if($tk=='04')echo "selected";?>>04</option>
<option value="05" <?php if($tk=='05')echo "selected";?>>05</option>
<option value="06" <?php if($tk=='06')echo "selected";?>>06</option>
</select>
<?} ?>
</td>
</tr>
<tr>
<td colspan="2" valign="top">Status</td>
<td valign="top">:</td>
<td>
<?php
$qry = "SELECT * from dtable where userid='".$USER->id."'";
$sql = mysql_query($qry);
$row = mysql_num_rows($sql);
if($row==0)
{
?>
<input type=radio name="status" <?php if($status=='retake') {?>checked="checked"<?php } ?> value="retake" onClick="funcShow('box1', 'box2', 'box3');">Retake<br /></tr> <tr>
<td colspan='2'>
<div id="box1" style="display: none;">Last Date <br> Latest Date<br>
</div></td>
<td><div id="box2" style="display: none;">: <br> : <br></div></td>
<td>
<div id="box3" style="display: none;">
<?php $rsu[lastdate] ?> <br> <?php $rsu[latestdate] ?>
</div>
</td>
don't put the onClick attribute onto the option tag. You should use onChange on the select tag and then pass in this.value. Then based on that value you can decide which "box" to show/hide. Just a simple example:
<script type="text/javascript">
function showHide(selValue){
switch(selValue){
case "A17":
//show/hide whatever box you want.
alert(selValue);
break;
default:
//do nothing
alert('nothing');
}
}
</script>
<select name="gred" id="gred" onChange="showHide(this.value);">
<option value=""> </option>
<option value="A17"<?php if($gred=='A17')echo "selected";?>>A17</option>
<option value="A22" <?php if($gred=='A22')echo "selected";?>>A22</option>
<option value="A27" <?php if($gred=='A27')echo "selected";?>>A27</option>
</select>
for now, as you can tell, it just alerts some values. But it is easy to change.
Use onChange to trigger JS when you change something in the dropdown menu, instead of onClick. With selectedIndex you can determine what has been selected (the option 'show' or 'hide') and go from there.
Tip: get rid of all the junk code and limit it to just the elements that you need. Try to make it work in its most basic form, then build it out to what you're actually trying to do. That way it's a lot easier to track bugs and find out why it's not working.
Also, you might want to look into jQuery. You can do pretty much the same in plain old JavaScript, but jQuery makes things like this a lot easier.

Categories