how to access jquery variable in php [closed] - php

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()
});
});
});

Related

is possible to insert data automatic process a action.php? [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 4 years ago.
Improve this question
EXAMPLE
I want to insert the data automatic everyday without press the button action or whatever.
In my case,
my data is ready to input every day on system but i must press the button action every day too.
thakyou
Handle an auto-click parameter in URL:
in URL: ?automode=1
in Javascript:
var is_automode = false,
$btn = YOUR_BUTTON,
$form = YOUR_FORM,
$mode = HIDDEN_INPUT_TEXT;
location.search.replace(/^\?/,'').split('&').forEach(function(val) {
var pair = val.split('=');
if (pair.length && pair[0]==='automode' && pair[1]!=0) {
is_automode = true;
}
});
if (is_automode) {
setInterval(function () {
$form.target = '_blank';
$mode.value = 'automode'; // detect this value in the target page, call window.close() if is 'automode'
$btn.click(); // or $form.submit()
}, 86400*1000);
}
Or add a cron job to open the URL (YOUR_URL/action.php?automode=1) without setInterval()

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

Use data from a JSON result into a table [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 7 years ago.
Improve this question
Sorry for the bad titling, I had difficulty naming it.
I am trying to take this data:
<?php
require '../lib/config.php';
// Get the server statuses
$pdb = connectPDO();
$stmt = $pdb->prepare('SELECT * FROM serverlist');
$stmt->execute();
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
Which gives this result:
http://ichris.xyz/ajax/server-status.ajax.php
and then take that data and insert it into a table
http://ichris.xyz/server_list.php (the table I have made to insert this data).
Use JSON DECODE: http://php.net/manual/en/function.json-decode.php
Once you serialize the json feed, then using a while or for loop run your inserts using php.
You can use
$.get('ajax/server-status.ajax.php').done(function(results) {
var statusList = $.parseJSON(results);
var tableBody = $("table tbody");
if ($.isArray(statusList)){
for(var index in statusList){
var status = statusList[index];
$("<tr>").append("<td>"+status["name"]+"</td>")
.append("<td>"+status["faction2_name"]+"</td>")
.append("<td>"+status["map"]+"</td>")
.append("<td>"+status["players_online"]+"/"+status["players_max"]+"</td>")
.append("<td>"+(status["has_password"] ? 'Yes' : 'No')+"</td>")
.appendTo( tableBody );
}
}
});
See it in action in a JSFiddle demo

php variable changes when clicked on html link [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'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>

Return value for php class not returning correctly [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 attempting to return a value from a php class from within Javascript:
<html>
<?php
function tester()
{
return "testing";
}
?>
<script type="text/javascript">
var val= "<?php tester(); ?>";
document.write(val);
document.write('Finished!');
</script>
</html>
Nothing is being returned, rather I simply get a blank screen (apart from the "Finished" message) =(
That is happening because you are just returning the value, to output you have to echo it:
<?php echo tester(); ?>
You're not doing anything with the output of tester(). Try this instead:
var val= <?php echo json_encode(tester()); ?>;
You need to echo it.
Either use:
var val= "<?php echo tester(); ?>"
Or if you have short tags enabled:
var val= "<?=tester()?>"

Categories