How to fetch data from drop-down menu in PHP? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to select multiple elements from a drop down menu bar and show it in the next text box without submitting the form?
Suppose I have elements like "car,dog,rat,bike,tv,fridge,etc,etc " showing in the drop down menu. I want to select car,bike,tv,fridge and fetch the data in the next text area without going to a new page i.e I want to show the user selected items but only from drop down box only.

See if this is what you want
<select id="select" multiple>
<option value="1">car</option>
<option value="2">dog</option>
<option value="3">rat</option>
<option value="4">bike</option>
</select>
<textarea id="txt1"></textarea>
your js:
$(document).ready(function(){
$( "#select" )
.change(function () {
var str = "";
$( "select option:selected" ).each(function() {
str += $( this ).text() + " ";
});
$( "#txt1" ).text( str );
})
.change();
});
WORKING DEMO
Do not forget to include jquery.js file

Welcome To StackOverflow! As stated in the comments, for a better answer try providing what you have already as there are multiple ways to answer this type of question.
Having said that, this should help:
$('.dropdown').on('change', function() {
var value = $(".dropdown").val();
$('textarea').val(value); //insert string into textarea
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="dropdown" multiple>
<option>Rock</option>
<option>Paper</option>
<option>Scissors</option>
<option>Lizard</option>
<option>Spock</option>
</select>
<textarea></textarea>
This is using jQuery, so remember to include the jQuery file.

Related

Display that User Current Location as the value of the Textbox [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have here a codes that displays the current location of the user, this codes is already working. My problem is how to put the id "#location" as the value for my textbox in html form so that the current location will put in a textbox form.
$(document).ready(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showLocation);
} else {
$('#location').html('Geolocation is not supported by this browser.');
}
});
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.ajax({
type:'POST',
url:'../geo/getLocation.php',
data:'latitude='+latitude+'&longitude='+longitude,
success:function(msg){
if(msg){
$("#location").html(msg);
}else{
$("#location").html('Not Available');
}
}
});
}
HTML:
Location: <input id="location" type="text" value="" size="50">
<br>
If the ajax request works correctly (we don't know what kind of data it returns), when the problem is in wrong choice of method.
Use .val() instead of .html() because you can't insert html inside input.
if (msg) {
$("#location").val(msg);
} else {
$("#location").val('Not Available');
}

Accessing multiple PHP pages through array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need some help with this PHP pages.
I don't understand
session_start();
require("config/db.php");
if(isset($_GET['page'])){
$pages=array("menu","cart");
if(in_array($_GET['page'], $pages)) {
$_page=$_GET['page'];
}else{
$_page="menu";
}
}else{
$_page="menu";
}
?>
My code at the bottom calls $_page and it will display that PHP page,
in this case, menu.php. How do I make it call menu2.php if I were to select an option that changes the value to menu2?
EDIT:
Thanks for all your comments! I would like to add on that this is just a container in the website, and my output will be derived from
Please look at the sample code i created below, this should give you an idea, I hope.
<section href="#" id="showItem"></section>
<select id="options">
<option value="page1.php">Page 1</option>
<option value="page2.php">Page 2</option>
</select>
<script type="text/javascript" src="js/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
var options = $('select#options').val();
var section = $('section#showItem');
var getPage = function (url) {
$.get(url).success(function (data) {
section.html(data);
}).error(function () {
section.html("error loading page");
});
};
$('select#options').change(function () {
var options = $('select#options').val();
getPage(options);
});
$(document).ready(function () {
getPage(options);
});
</script>
Use
header("Location:" . $_page . ".php");
where location can be any valid url/page name on the server relative to the current document.
just try: header('Location:'.$_page.'.php'); it will redirect to selected page

Assistance with Extendable Forms in HTML [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am making an online entry form for a martial arts competition. I have got on with everything fine so far. However, there is a part where the user selects how many applicants they wish to input into the competition. The amount that they select will determine the amount of entry rows that come up. I am unsure of how to do this though, would if be possible for any of you geniuses to help me?
Thank you in advance.
From your description, if the end-user selects, say, 8 as the number of people they will be signing up in the competition, the next series of signup elements will correspond with the number of they selected.
Here's my HTML form in my PHP file:
KarateCompetition.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name = "my-form" id = "my-form">
<select name = "number-of-participants" id = "number-of-participants">
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
<option value = "6">6</option>
<option value = "7">7</option>
<option value = "8">8</option>
</select>
<div id = "entry-details-area">
<!-- This is where we'll dynamically insert our rows -->
</div>
</form>
</body>
<!--First, grab jQuery from Google we need this-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!--Next, include my logic-->
<script src = "karate.js"></script>
</html>
Next, we'll write our custom logic in "karate.js," and we'll be sure to include that file in our PHP page after we have grabbed jQuery from Google (simply inserting the line shown will dynamically include jQuery in your PHP page)
Here's my jQuery (a Javascript library) logic that I'll use to load the HTML elements dynamically when the user selects a specific number of participants:
karate.js:
//This is my HTML select element "number-of-participants"
var participants_selector = $("#number-of-participants");
//This is where I want to add my dynamic HTML input elements
var entry_details_area = $("#entry-details-area");
//Each time a new value is chosen, grab their number of participants
participants_selector.on("change", function()
{
//Get the number of participants that the end-user has selected
var number_of_participants = participants_selector.val();
//Clear out any previously-appended HTML
entry_details_area.empty();
//Next, create a corresponding number of input elements for the number of participants selected.
for (var i = 0; i < Number(number_of_participants); i++)
{
//Remember: Arrays start counting at 0, so to make it human-readable, add +1
var participant_number = i+1;
//Let's add a simple box: "first-name" for each participant
var entry_form_html = '<p>Participant ' + participant_number + ' first name is: '
+'<input name = "first-name-'+participant_number+'" id = "first-name-'+participant_number+'"></p>';
//Add the HTML for each participant
entry_details_area.append(entry_form_html);
}//end of loop
});//end of on-change listener logic
To test, here's a JSFiddle
You can use jquery do append html code dinamically. It would look like this:
<table id='tableid'>
<tr><td>Name</td><td>Age</td></tr>
</table>
<script>
var x = 5; // user input
for(y=0;y<x;Y++){
$('#tableid').append('<html code here to the other <tr> lines>');
}
</script>

Load second drop box with specific value from first drop box in HTML [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to create 2 drop box
when i select mobile form 1st drop box
2nd drop box should open mobile brand list
if i click Computer in first box
list of computer brand should open
use ajax, it could be something like this
YOUR HTML
<select id="combo1" name="combo1" onchange="getvaluesforcombo2($('#combo1').val());">
<option value=""></option>
<option value="1">Computer</option>
<option value="2">Mobile</option>
</select>
<select id="combo2" name="combo2">
<option value=""></option>
</select>
YOUR JS
function getvaluesforcombo2(valuec1) {
var postData = {combo1v:combo1v};
$.post('getvaluefor2.php', postData, function(data) {
var obj = $.parseJSON(data);
var sel = $("#combo2");
sel.empty();
sel.append('<option value=""></option>');
$.each(obj, function(k, v){
sel.append('<option value="'+v.Code+'">'+v.Name+'</option>');
});
});
}
AND YOUR PHP
if (isset($_POST['combo1'])) {
$combo1= $_POST['combo1'];
$query= $this->db_connection->prepare('SELECT type, name FROM mobileandpcbrands WHERE type = :combo1');
$query->bindValue(':combo1', $combo1, PDO::PARAM_INT);
$query_pilar->execute();
//if there is results
if ($query->rowCount() != 0) {
$result = $query->fetchAll();
foreach ($result as $results) {
$rows[] = $results;
}
echo json_encode($rows);
}
This is an example i hope this help you

how to access jquery variable in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My question is very very similar to this one
How to get jQuery variable value in PHP file
The difference is I have some code samples. I just cant seem to get a satisfactory answer for what I am looking for. I am doing all these in one php file. Is this even possible?
I want to store categoryChoiceVal into $category
Here is the html
<select class="categoryChoice ">
<option>Gym class</option>
<option>Math tutorial</option>
<option>Basketball court</option>
</select>
the php
<?php
$category = '';
$query_string = "https//this_is_a_dummy_string.htm?{$category}";
$data = file_get_contents($query_string);
?>
I am catching the selection from the select tag by doing this
$(function(){
$('select').change(function() {
var categoryChoiceVal = $('.categoryChoice option:selected').text();
});
});
Thanks in advance
<?php
$category = '';
if (isset($_POST['categoryChoiceVal'])){
$category = $_POST['categoryChoiceVal'];
}
$query_string = "https//this_is_a_dummy_string.htm?{$category}";
$data = file_get_contents($query_string);
?>
$(function(){
$('select').change(function() {
$.post('phpUrl',{
categoryChoiceVal:$('.categoryChoice option:selected').text()
});
});
});

Categories