I have this code
<select id="menu" name="department">
<?php
//Get the departments and create the select menu dynamically
include 'connect.php';
$query = "SELECT* FROM department;";
$result = mysqli_query($link,$query);
$html = "";
if($result){
while ($obj = mysqli_fetch_object($result)) {
$html.='<option value="'.$obj->dept_name.'">'.$obj->dept_name.'</option>';
}
} else {$html.='<p style="color:red;text-align:center">Θεμελιώδες λάθος κατά την ανάκτηση των τμημάτων</p>';}
print $html;
mysqli_close($link);
?>
</select>
<?php
if (isset($_POST['menu']))
print '<script type="text/javascript">document.getElementById("menu").value = "'.$_POST['menu'].'";</script>';
?>
which is nested in a form tag that creates dynamically a select menu and I want to keep the selected value after the submission. It doesn't work. Any ideas why?
Where you are creating options dynamically add code to retain post value for select,something like,
$html .= '<option value="'.$obj->dept_name.'"';
if(isset($_POST['department']) && $_POST['department'] == $obj->dept_name){
$html .= ' selected ';
}
$html .= '>'.$obj->dept_name.'</option>';
Related
In my dropdown list, i put all the "pack_name(s)" the user has posted and I display it all in the list for the user to select and update. So when the user selects one and hits submit, i want to get that "value" submitted and use it for later purposes but ive been researching and only found "pre-set" values with html and the value was given using Jquery. So i wondering if its possible to basically take the "pack_name" selected and when the user hits submit, echo out the selected value.
PHP
<?php
session_start();
if(empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){ //catch file overload error...
$postMax = ini_get('post_max_size'); //grab the size limits...
echo "<p style=\"color: #F00;\">\nPlease note files larger than {$postMax} will result in this error!</p>"; // echo out error and solutions...
return $postMax;
}
if(isset($_COOKIE['id'])){
if($_SESSION['came_from_upload'] != true){
setcookie("id", "", time() - 60*60);
$_COOKIE['id'] = "";
header("Location: developerLogin.php");
exit;
}
try{
// new php data object
$handler = new PDO('mysql:host=127.0.0.1;dbname=magicserver', 'root', '');
//ATTR_ERRMODE set to exception
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die("There was an error connecting to the database");
}
$userid = $_SESSION['id'];
$stmt = $handler->prepare("SELECT * FROM pack_profile WHERE pack_developer_id = :userid");
$stmt->bindParam(':userid', $userid, PDO::PARAM_INT);
$stmt->execute();
echo "<select>";
while($result = $stmt->fetch()){
echo "<option>" . $result['pack_name'] ."</option>";
}
echo "</select>";
if($_SERVER['REQUEST_METHOD'] =="POST"){
$token = $_SESSION['token'];
}
}
?>
You need to give the select element a name attribute, and give each option element a value attribute.
For example:
echo "<select name=\"pack\">";
while($result = $stmt->fetch()){
echo "<option value=\"" . $result['pack_name'] . "\">" . $result['pack_name'] ."</option>";
}
echo "</select>";
Of course you should be escaping anything which could contain &, < or " with something like htmlspecialchars().
There's a search field in my index which is search in a mysql db.
The result have link with a product ID, so it's clickable.
I want to make a list from the search result elements which i clicked and take it in a form.
So when i have the list i want to submit all of those to another page.
I think the best idea for that is javascript's click event.
Found some js code on the internet, but i can't use it well, because unfortunately i don't have enough knowledge for javascript yet.
What is the simpliest solution?
javascript what i found
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$("ul").append("<li>product name! <a href='javascript:void(0);' class='remove'>×</a></li>");
});
$(document).on("click", "a.remove" , function() {
$(this).parent().remove();
});
});
</script>
search.php
<?php
include './config/functions.php';
dbconn();
$html = '';
$html .= '<div class="result">';
$html .= '<a href="urlString" name="urlString">';
$html .= '<h3>category: <b>nameString</b></h3>';
$html .= '<h4>functionString</h4>';
$html .= '</a>';
$html .= '</div>';
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
if (strlen($search_string) >= 2 && $search_string !== '') {
$query = 'SELECT * FROM products WHERE pro_name LIKE "%'.$search_string.'%"';
$result = mysql_query($query);
while($results = mysql_fetch_array($result)) {
$result_array[] = $results;
}
if (isset($result_array)) {
foreach ($result_array as $result) {
$display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['pro_name']);
$display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['pro_category']);
$display_url = "index.php?".urlencode($_GET['mode'])."&send=".urlencode($result['pro_id']);
$output = str_replace('nameString', $display_name, $html);
$output = str_replace('functionString', $display_function, $output);
$output = str_replace('urlString', $display_url, $output);
echo($output);
}
}else{
$output = str_replace('urlString', 'javascript:void(0);', $html);
$output = str_replace('nameString', '<b>No result to show!</b>', $output);
$output = str_replace('functionString', 'Sorry :(', $output);
echo($output);
}
}
?>
search function in index.php
function product_search(){
$html = '<div id="main">
<div class="icon"></div>
<input type="text" id="search" autocomplete="off">
<h4 id="results-text">search key: <b id="search-string"></b></h4>
<ul id="results"></ul>
</div>';
echo $html;
}
I'm looking to replace a select dropdown with images.
My current code for the drop down is
<div class="app_services_dropdown_select">
<select name="app_select_services" class="app_select_services">
<option value="1" selected="selected">Class IV MOT (Up to 3,000KG)</option>
<option value="2">Class VII MOT (3,000KG - 3,500KG)</option></select>
<input type="button" class="app_services_button" value="Show available times">
</div>
Which is generated by this code:
$s .= '<div class="app_services">';
$s .= '<div class="app_services_dropdown">';
$s .= '<div class="app_services_dropdown_title">';
$s .= $select;
$s .= '</div>';
$s .= '<div class="app_services_dropdown_select">';
$s .= '<select name="app_select_services" class="app_select_services">';
if ( $services ) {
foreach ( $services as $service ) {
$service_description = '';
// Check if this is the first service, so it would be displayed by default
if ( $service->ID == $appointments->service ) {
$d = '';
$sel = ' selected="selected"';
}
else {
$d = ' style="display:none"';
$sel = '';
}
// Add options
$s .= '<option value="'.$service->ID.'"'.$sel.'>'. stripslashes( $service->name ) . '</option>';
}
}
$s .= '</select>';
$s .= '<input type="button" class="app_services_button" value="'.$show.'">';
$s .= '</div>';
$s .= '</div>';
And I really want an image of a car as value 1 and a van as value 2, plus I really want it to submit on click rather than having the button.
Is it possible to replace the dropdown with images instead or would I need to use a radio button, then style it using an image?
You can see my current dropdown in use here
There are various plugins are available which help you to achieve your target. I have googled and found few you can try them
ddslick
Ms Dropdown
Have a link that trigger the apparition of a div with Javascript.
This div will contains the two links images, with the links you want the user to go.
This div also need to be placed relatively with the initial link that trigger it.
If you don't want to program the Dropdown by yourself, you can use libraries like bootstrap or jquery-ui
I have 2 simple drop down lists one dependent on the other in a php include.
When one is selected it refreshes the page and then populates the second with the correct data, (works great).
The problem is that it refreshes the whole page instead of just the include file.
Is there a way to do just refresh the include? I have looked at many other examples (Ajax, Javascript etc) on here and other sites and cannot seem to get it to work.
A huge thank you in advance to anyone who can point me in the right direction.
<form enctype="multipart/form-data" action="advanced_search2.php" method="POST">
<?php require 'config.php'; ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<?
echo '<div id="publication">';
echo "<select name='cat'><option value=''>Select one</option>";
$quer2="SELECT DISTINCT id,publication FROM publication WHERE appear = 'Yes' and Search = 'Yes'";
foreach ($dbo->query($quer2) as $noticia2) {
echo "<option selected value='$noticia2[publication]'>$noticia2[publication]</option>"." <BR>";
}
echo "</select>";
echo '</div>';
?>
<?
echo '<div id="section">';
echo "<select name='subcat'><option value=''>Select one</option>";
$quer="SELECT DISTINCT section FROM sandbox WHERE publication = '$cat' AND appear = 'Yes'";
foreach ($dbo->query($quer) as $noticia) {
echo "<option value='$noticia[section]'>$noticia[section]</option>";
}
echo "</select>";
echo '</div>';
?>
<form>
<script>
$('#publication').change(function(){
var page = $( this ).attr('data-page');
$.ajax({
type:'POST',
url:page,
success: function(response){
$('#section').html(response);
}
});
})
</script>
wrap your second select box with a div
in you js function 'reload', call a ajax (another page) where you pass the catid and echo your second select box in that page
update your div html with the ajax response.
Your main page can look like this
<?php require 'config.php'; ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<?
echo '<div id="publication">';
echo "<select name='cat'><option value=''>Select one</option>";
$quer2="SELECT DISTINCT id,publication FROM publication WHERE appear = 'Yes' and Search = 'Yes'";
foreach ($dbo->query($quer2) as $noticia2) {
echo "<option selected value='$noticia2[publication]'>$noticia2[publication]</option>"." <BR>";
}
echo "</select>";
echo '</div>';
?>
<div id="section"></div>
<script>
$('#publication').change(function(){
$('#section').load('subcat.php?cat='+$(this).val());
});
})
</script>
And another page for getting subcat (subcat.php)
<?php
require 'config.php';
$cat = $_GET['cat'];
echo "<select name='subcat'><option value=''>Select one</option>";
$quer="SELECT DISTINCT section FROM sandbox WHERE publication = '$cat' AND appear = 'Yes'";
foreach ($dbo->query($quer) as $noticia) {
echo "<option value='$noticia[section]'>$noticia[section]</option>";
}
echo "</select>";
?>
I am creating a form for inserting data which technologies are being used by which customers.
I get data of customer's ID and name and select a customer in an drop-down list, and i get data from technologies id and description and display description in a table + it creates a textbox (ID='box-". $row1['ID_T']."') for every technology entry in a DB.
So now i would like to check this dynamically created textboxes with jquery for value (if empty or filled with data) (getelementbyid) but i can not find a way to check theese DYN textboxes.
The ID_T and ID_C will be loaded into another table containing these two PK's and add string from textbox to value.
i would appreciate your help so much!
<HTML>
<HEAD>
<SCRIPT>
function update_tech(id,description)
{
var x = confirm('Do you want to edit technology '+description);
if (x == true)
{
document.getElementById('update_id').value = id;
//document.getElementById('description').value = description;
//document.form_update.submit();
}
}
</SCRIPT>
</HEAD>
<?php
include "connection.php";
$sql = "SELECT ID_C, Name FROM empresa";
$rs = mysql_query($sql, $conn);
$sql2 = "SELECT ID_T, description FROM technologies";
$rs2 = mysql_query($sql2, $conn);
while($row = mysql_fetch_array($rs2))
{
if (isset($_POST["box-".$row["ID_T"]]))
if ($_POST["box-".$row["ID_T"]] != "")
echo "INSERT ....".$_POST["box-".$row["ID_T"]]."<br>";
}
$rs2 = mysql_query($sql2, $conn);
mysql_close($conn);
?>
<BODY>
<SELECT NAME="customers" ONCHANGE="showCustomer(this.value)">
<?php
if (mysql_num_rows($rs))
{
while($row = mysql_fetch_array($rs))
{
?>
<option value='<?php echo $row['ID_C'] ?>'><?php echo $row['Name']?></option>
<?php
}
}
else {
echo "<option value=\"0\">No customers</option>";
}
?>
</SELECT>
<FORM METHOD="POST">
<?php
echo "<table border='0'>";
while($row1 = mysql_fetch_array($rs2))
{
echo "<tr>";
echo "<td><INPUT TYPE='text' name='box-". $row1['ID_T']."' ID='box-". $row1['ID_T']."'></td>";
echo "<td>" . $row1['description'] . "</td>";
echo "</tr>";
}
echo "</TABLE>";
?>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
You can either maintain an array of input box ids or use the jquery partial selector to identify all the input boxes.
jQuery approach is something like:
$('input[id^="box-"]').each(function(e, i) { console.log(e); };