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
Related
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');
}
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.
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'm trying to make a game in HTML and PHP and I'm pretty new to coding. The point is I need to execute a PHP script by clicking an HTML element. Is this possible?
My code would look something like this
$money = 100;
and by clicking on:
<a>rob a shop</a>
it must excecute:
function rob_shop(){
$money += 75;
echo "You now have $money $";}
Try this, is PHP:
<?php
if (isset($_GET['money'])) {
$money = rob_shop($_GET['money']);
echo 'You now have: '.$money.'<br>';
} else {
$money = 100;
echo 'You now have: '.$money.'<br>';
}
echo 'rob a shop';
function rob_shop($money){
$money = $money + 75;
return $money;
}
?>
But the best way to do it is with ajax to avoid refreshing the page.
Add extra param to link and check with php and call function.
rob money
Then check for link
if (isset($_GET['fun']))
{
runFun();
}
you will need to use javascript and an ajax call or you could just do it in javascript.
<a id="shop">rob a shop</a>
<div id="response"></div>
// include jquery
<script>
$( "#shop" ).click(function() {
var money = money + 75;
$("#response").html( "YOU NOW HAVE $"+money );
// or make an ajax call to a php script and return the value
});
</script>
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()
});
});
});
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 9 years ago.
Improve this question
how can I know the page is really opened in brower or get from script or src="xxx"?
<?php
session_start();
if(!isset($_SESSION['count'])){
$_SESSION['count']=0;
}
// I don't want to
// if(open in brower){
$_SESSION['count']++;
// }
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
url:'index.php',
type:'GET',
data:{},
success:function(){}
});
</script>
<?php
echo $_SESSION['count'] ; // the result now is 1,3,5,... for every reload
// I want to get the result 1,2,3,4... for every reload , how to check the page is really open in brower? not run in ajax or get ?
?>
A simple solution to this specific example would be to pass some data to the ajax request, and check that the data ISN'T present in order to increase your session count.
<?php
session_start();
if(!isset($_SESSION['count'])){
$_SESSION['count']=0;
}
if(!isset($_GET['dontcount'])){
$_SESSION['count']++;
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
url:'index.php',
type:'GET',
data:{'dontcount',1},
success:function(){}
});
</script>
<?php
echo $_SESSION['count'];
?>