autosubmit drop down value into database - javascript

i'm trying to auto submit the value of drop down menu using onchange=this.form.submit() into database but i couldn't make it work. can anyone please help me. any suggestions will be highly appreciated.
here is the code:
<?
require_once ('database.php');
?>
<form action="" method=post>
<select name="assignee" onchange="this.form.submit()">
<option value="0">Unassigned</option>
<?
$find_selected = mysql_query("select assign_to from orders where id = $id");
$asignee =mysql_fetch_row($find_selected);
$list=mysql_query("SELECT id, full_name from user where username <> 'root' and nature = 3");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<? echo $row_list['id']; ?>"<? if($row_list['id']== $asignee['0']){ echo "selected"; } ?>><? echo $row_list['full_name'] ?></option>
<?
}
mysql_free_result($list);
mysql_free_result($find_selected);
?>
</select>
</form>
<?php
if(isset($_POST['submit'])){
$sql = "UPDATE `orders` SET `assign_to` = '{$_POST['assignee']}' WHERE `id` = '$id' ";
mysql_query($sql) or die(mysql_error());
}
?>

<?php
if(isset($_POST['assignee'])){
$sql = "UPDATE `orders` SET `assign_to` = '{$_POST['assignee']}' WHERE id=$id";
mysql_query($sql);
}
?>
You just defined your query but didn't executed it

Related

PHP set dropdown value after page refresh

I'm having trouble setting the value of the dropdown after the form input refreshes the page. I can get the value but no matter what I try I'm unable to set the dropdown after the page refreshes. I've tried a number of different ideas I've found online too. I've tried both JavaScript and PHP solutions and all I can do is get the value but not set it. This is the code I have so far, which returns the drop down ID, I just need to know how to use it. I appreciate any help, thanks!
<?php
$pdo = new PDO('mysql:host=localhost; dbname=db', 'root', 'password')'
$sql = "SELECT divid, division FROM divisions ORDER BY division ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$divs = $stmt->fetchAll();
?>
<form method="post">
<select id="divi" name="divisions">
<?php foreach($divs as $div): ?>
<option value="<?= $div['divid'];?>"><?= $div['division']; ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['divisions'])){
$selected = $_POST['divisions'];
echo 'Selected: " . $selected;
} else {
echo 'Select division.';
}
}
?>
It's not very clear to me what you really want, however it looks like you want to select default:
<select id="divi" name="divisions">
<?php foreach($divs as $div): ?>
<option <?php echo ("mycondition ex: 'id == 1'") ? "selected" : NULL ?> value="<?= $div['divid'];?>"><?= $div['division']; ?></option>
<?php endforeach; ?>
</select>
You are simply missing any code that sets 'selected="selected"' in the HTML for the select field.
Also, your code is very hard to read so I've cleaned up the loop a little bit.
<form method="post">
<?php
echo '<select id="divid" name="divid">';
foreach ($divs as $div) {
$selected = '';
if (isset ($_POST['divid']) && ($_POST['divid'] == $div['divid'])) {
$selected = 'selected="selected"';
}
echo '<option value="' . $div['divid'] . '" ' . $selected . '>' . $div['division'] . '</option>';
}
echo '</select>';

How to auto refresh div contents contain php variables?

I created a page named queries.php
<?php
require("connection.inc.php");
$query = "SELECT SUM(john), SUM(robert), COUNT(school) FROM election";
$result = mysql_db_query($db, $query, $connection) or
die("query error!");
$data = mysql_fetch_array($result);
$john = number_format($data[0], 0,',','.');
$robert = number_format($data[1], 0,',','.');
$school = $data[2];
$total = $john + $robert;
$remaining_school = 524 - $school;
$percentage_john = round(($john*100/$total),2);
$percentage_robert = round(($robert*100/$total),2);
?>
Then I display the data in display.php
The data displayed well and nothing error, I tried a couple of tricks to auto refresh the container using jquery but failed.
Here is my display.php page:
<?php
include("queries.php");
?>
<div id="totaljohn"> // <--- the div id taken from css layout
<h9>John: <?php echo $john; ?> <br> (<?php echo $percentage_john; ?> %)</h9>
</div>
<div id="totalrobert">
<h9>Robert: <?php echo $robert; ?><br> (<?php echo $percentage_robert; ?> %)</h9>
</div>
<div id="totalboth">
<h4>Total : <?php echo $total; ?> Suara</h4>
</div>
<div id="totalschool">
<h4>Total School attending : <?php echo $school; ?> Schools</h4>
</div>
<div id="remaining_schools">
<h4>Remaining Schools: <?php echo $remaining_school; ?> of 524 schools</h4>
</div>
Everything looks good. Is your database connected? Is your SQL correct? Are there records in the database returned for your query?

Mysql Populating dropdown based on another dropdown choice

I've been testing out a lot of codes for this but i can't get it to work.
I want to populate a dropdown based on the value of another dropdown.
EDIT:
my table name is zipcode and i have CITY, STATE, Zipcode, FULLSTATE, and zipID
STATE is abbreviation of FULLSTATE
I have:
zipcode:php
<?php
/* Template Name: Zipcode Help */
?>
<?php get_header(); ?>
<?php
global $wpdb;
$query = "SELECT * FROM zipcode group by FULLSTATE";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='".$row['STATE']."'>".$row['FULLSTATE']."</option>";
}
?>
<select name="dropdown1" id="dropdown1" onchange="populatelist">
<option value="empty" selected>--Select State--</option>
<?php echo $dd; ?>
</select>
<select id="city" name="city">
<option value="empty"> </option>
</select>
<!--content of the page-->
<div class="container-wrapper clearfix" id="main-content-container">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
Jquery:
function populatelist()
{
var qry=document.getelementbyid('dropdown1').value;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
});
}
}
xmlhttp.open("POST", "help.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("qry="+qry);
}
Reference Page:(help.php)
<?php
/* Template Name: Help */
?>
<?php get_header(); ?>
<?php
global $wpdb;
$value=$_POST['qry'];
$query = "SELECT * FROM zipcode where STATE like '$value'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$ee .= "<option value='".$row['CITY']."'>".$row['CITY']."</option>";
}
echo $ee;
mysql_close();
?>
<?php get_footer(); ?>
Questions: Where should i put my help.php if i'm using wordpress?
I've created a custom template named 'help' then created a page in dashboard and assigned that template. Is this correct? I uploaded it on the theme directory. Also put it inside the js folder. I dunno I never tried jquery before :S
How should i call the jquery?
I put it on header:
<?php wp_enqueue_script('jquery'); ?>
<?php wp_head() ?>
<script type="text/javascript" src="<?php bloginfo("template_directory"); ?>/js/zipcode.js"></script>
</head>
I've been doing this for 3 days already and i'm so exhausted. I really don't know where to put the reference page
Use the following code in your zipcode.php file.
<?php
/* Template Name: Zipcode Help */
?>
<?php get_header(); ?>
<?php
global $wpdb;
$query = "SELECT * FROM zipcode group by FULLSTATE";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='".$row['STATE']."'>".$row['FULLSTATE']."</option>";
}
?>
<select name="dropdown1" id="dropdown1">
<option value="empty" selected>--Select State--</option>
<?php echo $dd; ?>
</select>
<select id="city" name="city">
<option value=""> </option>
</select>
<!--content of the page-->
<div class="container-wrapper clearfix" id="main-content-container">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#dropdown1').change(function () {
var value = $(this).val();
if (value == "") {
$("#city").html("<option value=''>Select</option>");
} else {
$.ajax({
type: "POST",
url: "<?php bloginfo('template_directory'); ?>helper/help.php",
data: "value=" + value,
success: function (server_response) {
$("#city").html(server_response);
}
}); //$.ajax ends here
} //if else
return false
});
}); // function ends here
</script>
<?php get_footer(); ?>
Inside your wordpress theme folder create a folder called helper and put your helper.php file in there.
Use the following code in your help.php file;
<?php
$value = $_POST['value'];
global $wpdb;
$value= a_href;
$query = "SELECT * FROM zipcode where STATE like '$value'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$ee .= "<option value='".$row['CITY']."'>".$row['CITY']."</option>";
}
echo $ee ;
?>
I didn't test the code but I think it should work. Let me know if you have any more question.
**Make sure Jquery is loaded properly
Very Important: mysql_query is deprecated as of PHP 5.5.0 . You should use PDO or mysqli extension. Check This: http://cz1.php.net/mysql_query

How can I get a value from a select box on the same page

I'm trying to use PHP and Javascript to make a category selector box.
I've set it up so that the Javascript will show the steps in order of being selected, and hide after being deselected.
However, I can't figure out how to take the selected options "id" or "value" and pass it to the next line. (once the chosen id or value is passed on, the next list can load)
Here is my code, Thanks in advance for looking. And please, if I'm doing something wrong or not the right way. Let me know and/or show me the right way to do it.
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>
<div class="content">
<form>
<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
while($row = mysqli_fetch_array($result_c))
{
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
<?php
$var_c = ????
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
while($row = mysqli_fetch_array($result_s))
{
echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
}
?>
</select>
<select name="subsections" class="newcatediv" id="step3" size="3">
<?php
$var_s = ????
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';
echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
}
?>
</select>
</form>
</div>
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php");
?>
By default, the first option in a <select> is selected, so this would work:
<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
$var_c = null;
while($row = mysqli_fetch_array($result_c))
{
if($var_c == null) $var_c = $row['category_nameid'];
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
<?php
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
$var_s = null;
while($row = mysqli_fetch_array($result_s))
{
if($var_s == null) $var_s = $row['section_nameid'];
echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
}
?>
</select>
<select name="subsections" class="newcatediv" id="step3" size="3">
<?php
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';
echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
}
?>
</select>
Cheers
Hi :) You Can't Process that at the same page using Php. But you can do that with this jquery including 3 pages.
First Page:
$(document).ready(function(){
$("#step1").change(function(){
var id=$("#step1").val();
alert(id); //shouts the value of the selected step1
$.post("select_step2.php", {id:id}, function(data){
$("#step2").empty();
$("#step2").append(data);
$("#step2").change(function(){
var id2=$("#step2").val();
alert(id2); //shouts the value of the selected step2
$.post("select_step3.php", {id:id2}, function(data){
$("#step3").empty();
$("#step3").append(data);
});
});
});
});
});
The above code is for jquery where you can call each data's that depends on each step.
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>
<form>
First Step: <select name="categorys" class="newcatediv" id="step1" size="3">
<?php
$result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");
while($row = mysqli_fetch_array($result_c))
{
echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
}
?>
</select>
Second Step: <select name="sections" class="newcatediv" id="step2" size="3"></select>
Third Step: <select name="subsections" class="newcatediv" id="step3" size="3"></select>
Code for you select_step2.php:
<?php
//Please include the connection to your database here :)
$var_c = trim($_POST['id']);
$section = "";
$result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");
while($row = mysqli_fetch_array($result_s))
{
$section.="<option value='$row[section_nameid]'>$row[section_name]</option>";
}
echo $section;
?>
Code for your select_step3.php:
<?php
//database connection here
$var_s = trim($_POST['id']);
$subsection= "";
$result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");
while($row = mysqli_fetch_array($result_ss))
{
$subsection.="<option value='$row[subsection_nameid]'>$row[subsection_name]</option>";
}
echo $subsection;
?>

Javascript to php the same file

I have a problem. I need to get the value from a select tag then use it in php for my sql. Here is my code
<div class="form-group">
<label> ROOMS </label>
<?php
echo "<select value= 'TRoom1' id ='TRoom1' class='form control'>";
echo "<option>Select Room Type</option>";
while ($row1 = mysql_fetch_array($result2))
{
echo "<option>" . $row1['Room_type'] . "</option>";
}
echo "</select>";
?>
this is for the sql command
<div class="modal-body">
<div class="container">
<?php
$selectedValue = $_POST['TRoom1'];
$sql = "SELECT RoomNumber FROM rooms Where Room_type = '$selectedValue' ";
$result = mysql_query($sql);
echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
echo "<option>Select Room Number</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>" . $row['RoomNumber'] . "</option>";
}
echo "</select>";
?>
TIA! :))
THis is the code ofor room type with its corresponding room number
<div class="form-group">
<label for="exampleInputEmail1"> ROOMS </label>
<?php
echo "<select value= 'TRoom1' name ='TRoom1' id ='TRoom1' class='form-control'>";
echo "<option>Select Room Type</option>";
while ($row1 = mysql_fetch_array($result2))
{
echo "<option>" . $row1['Room_type'] . "</option>";
}
echo "</select>";
?>
</div>
<div class="form-group">
<?php
$select_value=$_POST['selectedValue'];
$sql = "SELECT RoomNumber FROM rooms Where Room_type = '$select_value' ";
$result = mysql_query($sql);
echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
echo "<option>Select Room Number</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>" . $row['RoomNumber'] . "</option>";
}
echo "</select>";
?>
</div>
you need to use name attribute for your select tag if u want to fetch the value in the php part and in the option u have to pass the value attibute.that value u will get in the php part.
<html>
<head></head>
<body>
<form action="a.php" method="post">
<select name="selectname" id="someid" >
<?php
while ($row1 = mysql_fetch_array($result2))
{
?>
<option value="<?php echo $varible ?>"> <?php echo $row1['Room_type']; ?></option>
<?php } ?>
</select>
<input type="submit" value="submit">
</form>
</body>
</html>
for php part:u can fetch value like this:
filename=a.php
<?php
$select_value=$_REQUEST['selectname'];
$sql1 = "SELECT RoomNumber FROM rooms Where Room_type = '$select_Value' ";
$sql=mysql_result(mysql_query($sql1),0);
?>
Please google your doubts before posting here. There are plenty of example available. mysql_query is deprecated use mysqli_ function
<div class="form-group">
<label> ROOMS </label>
<?php
echo "<select id ='TRoom1' name ='TRoom1' class='form control'>";
echo "<option>Select Room Type</option>";
while ($row1 = mysql_fetch_array($result2))
{
echo "<option value=".$row1['Room_type'].">" . $row1['Room_type'] . "</option>";
}
echo "</select>";
?>
If you are submitting your form as post you would get values as
$sql = "SELECT Room_type, Rate, RoomNumber FROM rooms Where Room_type ='".$_POST['TRoom1']."' ";
Try like
var Sel_val = document.getElementById('TRoom1').value;
Sel_val will be the selected value of that Dropdown.Better you use ajax in your case.If it is on the same page then you use Form submit method.
For the ajax first you need the target url and the value which you want to send..So try like
$.ajax({
url : Url of the page at which the sql command will be there,
type : 'POST',
data : { Sel_val : Sel_val }
});
Then at your target file get the Sel_val via POST method.
I think you used the self action and try this below code
if($_POST){
$selectedValue = $_POST['TRoom1'];
$sql = "SELECT RoomNumber FROM rooms Where Room_type = '$selectedValue' ";
$result = mysql_query($sql);
echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
echo "<option>Select Room Number</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>" . $row['RoomNumber'] . "</option>";
}
echo "</select>";
}

Categories