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.
Related
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)
I have been trying for weeks to solve this problem and as a last resort I seek help here.
I am using option tag to display 4 options. When I select an option it loads a .php file. The code I have is not too elegant but it does work.
<select name="history" class="button">
<option value="1" selected>Today</option>
<option value="2">Yesterday</option>
<option value="3">This Month</option>
<option value="4">This Year</option>
</select>
<div id="txtHint"><b></b></div>
<script>
$('[name="history"]').on('change', function() {
var ajaxMethod = "today.php";
switch($(this).val())
{
case "1":
ajaxMethod = "today.php";
break;
case "2":
ajaxMethod = "yesterday.php";
break;
case "3":
ajaxMethod = "monthly.php";
break;
case "4":
ajaxMethod = "yearly.php";
break;
}
$("#txtHint b").load(ajaxMethod);
});
</script>
All the .php files have the same table but make different mysql queries.
When I refresh the page, it clears the block and I have to select an option again. Is it possible to load an option as a default when the page loads? Otherwise I have an empty block before making a selection.
I tried having the table in the block and having each file only query the mysql but that does not work.
Any suggestions would be appreciated
For completeness I have included the today.php
<?php
$result = mysqli_query($con, "
SELECT max(Tmax), min(Tmin), max(R)
FROM alldata
WHERE DATE(DateTime) = CURDATE()
"
);
while ($row = mysqli_fetch_array($result)) {
$maxTemp = $row['max(Tmax)'];
$minTemp = $row['min(Tmin)'];
$totalRain = $row['max(R)'];
}
?>
<table id="historical">
<tr>
<td>
</td>
<td>
<?php echo 'Maximum'?>
</td>
<td>
<?php echo 'Minimum'?>
</td>
</tr>
<tr>
<td>
<img src="images/temp.png">
</td>
<td>
<?php echo $maxTemp." °C"?>
</td>
<td>
<?php echo $minTemp." °C"?>
</td>
</tr>
<tr>
<td>
<img src="images/rain.png">
</td>
<td>
<?php echo $totalRain." °C"?>
</td>
</tr>
</table>
To restore the selected option when you refresh the page.. TRY
1.Fetch data from DB to select the default option while loading the page. (If you store the option value in DB).
OR Use Sessions or cookie to store the selected data temporarly. And select the default option w.r to session or cookie data.
After a lot of testing, this is the code that fits the purpose.
It may help other users.
<select id="recordsSelector" class="button">
<option value="today" selected>
<?php echo "Today"?>
</option>
<option value="yesterday">
<?php echo "Yesterday"?>
</option>
<option value="thisMonth">
<?php echo "This Month"?>
</option>
<option value="thisYear">
<?php echo "This Year"?>
</option>
</select>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
stationData('<?php echo $defaultStats?>');
function stationData(period){
$(".records").html("");
if(period=="today"){
$("#almanacheading").html("Today");
}
if(period=="yesterday"){
$("#almanacheading").html("Yesterday");
}
if(period=="thisMonth"){
$("#almanacheading").html("This Month");
}
if(period=="thisYear"){
$("#almanacheading").html("This Year");
}
$.ajax({
url : "almanac.php?period="+period,
dataType : 'json',
success : function (json) {
$("#historyTMax").html(json['maxT']);
$("#historyTMin").html(json['minT']);
$("#historyHMax").html(json['maxH']);
$("#historyHMin").html(json['minH']);
$("#historyDMax").html(json['maxD']);
$("#historyDMin").html(json['minD']);
$("#historyPMax").html(json['maxP']);
$("#historyPMin").html(json['minP']);
},
});
}
$("#recordsSelector").change(function(){
period = $(this).val();
stationData(period);
});
$("#recordsSelector").val('<?php echo $defaultStats?>');
</script>
I want to make that if user is select certain option(provided from database) only one value then the other option tag appear to select else for all rest of other options user have text box to enter the value. Something like the below-
<tr>
<td><span class="alert">* </span>Country:</td>
<td><select name="country_id" class="medforminput" id="country_id" onchange="getState('select_state.php?country_id='+this.value)">
<option value="Select Country">Select Country</option>
<?php
while($row4=mysql_fetch_array($coun1))
{ ?>
<option value="<?php echo $row4['country_id']; ?>">
<?php echo $row4['country_name']; ?></option>
<?php }?>
<option value="others">Others</option>
</select></td>
</tr>
<tr>
<td><span class="alert">* </span>State</td>
<?php
if($row4['country_name']=="INDIA")
{?>
<td id="statediv"><select name="state_id" class="medforminput" id="state_id" onchange="getCity('select_city.php?state_id='+this.value)">
<option value="">Choose State</option>
</select></td>
<?php else {
<td id="statediv"><input type="text" name="state_id" class="medforminput" id="state_id"/>
</td>
<?php }?>
</tr>
I am getting nothing what wrong in the concept?
I want to display select tag where user choose the "INDIA" else text box to enter the state.
You need to write an onChange event handler in javascript that tests the option selected, then either displays the dropdown or textbox in a dynamic div.
I'm going to use jQuery because it's a LOT less code.
Example:
<select name="country_id" class="medforminput" id="country_id" >
<!-- options go here -->
</select>
<script language="javascript">
$("#country_id").change(function() {
if ($(this).val() == 'India') { // put the country id here instead of string comparison
$("#stateInput").html(getStates());
} else {
$("#stateInput").html("<input type='text' id='stateId' name='stateId' />");
}
});
</script>
Please excuse the pseudocode - I didn't want to make it excessively long.
I need to populate the relevant text box with the results of a Select drop down.
My Javascript so far is
<script language="JavaScript">
var mytextbox = document.getElementById('error');
var mydropdown = document.getElementById('errormsg_id');
mydropdown.onchange = function(){
mytextbox.value = this.value;
}
</script>
My select box and text boxes are built as arrays from a query so there is nultiple of them.
I need to be able to populate the corresponding text box with the results from the Select next to it.
Any ideas?
Thanks
<td width="1%">
<select style="width:100%" name="errormsg_id[]" id="errormsg_id[]">
<?php do { ?>
<option value="<?php echo $row_rserrormsg['errormsg_id']?>"
<?php if ($row_rsdates['errormsg_id'] == $row_rserrormsg['errormsg_id']) { echo("selected='selected'"); } ; ?> >
<?php echo $row_rserrormsg['error_message']?></option>
<?php } while ($row_rserrormsg = mysql_fetch_assoc($rserrormsg)); ?>
<?php mysql_data_seek($rserrormsg, 0);
$row_rserrormsg = mysql_fetch_assoc($rserrormsg);?>
</select>
</td>
<TD align="center" class="adminuser">
<input style="width:96%;text-align:left;" autocomplete="on" title="Enter Error Message" type="text" name="error[]" id="error[]" value="<?php echo $row_rsdates['error_message']; ?>" maxlength="100"/>
</TD>
Assuming You will be using jQuery and Your HTML is like this:
<table>
<tr>
<td><select class="my_select" name="errormsg_id1[]" id="errormsg_id1">...</select></td>
<td><input name="errormsg_id1_txt" id="errormsg_id1_txt" title="..." />
</tr>
...
<tr>
<td><select class="my_select" name="errormsg_idX[]" id="errormsg_idX">...</select></td>
<td><input name="errormsg_idX_txt" id="errormsg_idX_txt" title="..." />
</tr>
</table>
this could be done using jQuery:
$(document).ready(function(){
$('.my_select').change(function(){
$('#'+$(this).attr('id')+'_txt').val($(this).val());
});
});
Hope it is correct, have no time to test it...
Here is a JSFiddle/DEMO on how to get it to work with sample selects:
I think this approach is a little better than #shadyyx since it removed the need for you to manually increment the input and select field names.
<table>
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
</table>
And the JQuery:
$(document).ready(function(){
$('.select').change(function(){
$(this).parent().parent().find('.error_text').val($(this).val());
});
});
NOTICE: If you are using plain Javascript, see this DEMO
<table>
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id1" onChange="update('errormsg_id1')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text"name="errormsg_id_txt[]" id="errormsg_id_txt1" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id2" onChange="update('errormsg_id2')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" id="errormsg_id_txt2" title="..." />
</tr>
</table>
And Javascript:
function update(element){
var e = document.getElementById(element);
var id = element.substring(element.length-1);
document.getElementById("errormsg_id_txt"+id).value = e.value;
}
You need to think of way to uniquely identify your corresponding set of elements. The most convenient way I would suggest would be to use the primary key value of the data row.
And example would be like
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id']; //A primary key field
echo '<input type="text" name="text'.$id.'" id="text'.$id.'" />';
echo '<select name="select'.$id.'" id="select'.$id.'" />...</select>";
}
Next, call a function from the select menu, so that only the relevant text box is updated. Update the above select portion to read something similar to this.
echo '<select name="select'.$id.'" id="select'.$id.'" onchange="updateBox(\'text'.$id.'\', this.value)" />...</select>";
Now, create a very simple simple function to update the text box.
function updateBox(element, value) {
el = document.getElementById(element);
el.value = value;
}
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.