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

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

How do you make color-changing text withJS? [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
So, let's say I want to have a piece of text on my website, that changes color every second. I do know how to use JS to change the color of the text, but not how to continue to auto-change it based on certain parameters. For example, cycling through thousands of colors, not just one or two.
ok here is a simple way to blink text with colors
Js :
// List of colors
var spectrum = ['#f00', '#f66', '#969', '#00F', '#0FB53F'];
var cycle = spectrum.length-1;
// Cycle speed
var speed = 300;
var i = 0;
window.setInterval(function(){
document.getElementById('index').style.color = spectrum[i];
if (i < cycle) i++;
else i = 0;
}, speed);
HTML
<p id="index">Flashing text</p>
Demo
You can simple use html inside PHP:
echo "<font color='red'>Hello World</font>";
for a loop you can simple create an array with all the html collors:
$colors=array();
$colors[1]="red";
$colors[2]="blue";
// soo on....
foreach($colors as $color){
echo "<font color='$color'>Hello World</font>";
}
setInterval is what you want.
Fiddle
http://www.w3schools.com/jsref/met_win_setinterval.asp
var myColours = [...];
var index = 0;
var myElement = /*get your element*/
setInterval(function() {
/* Set your element. Are you using Javascript to get your element? */
myElement.style.color = myColours[index];
/* Are you using jQuery? */
myElement.css('color', myColours[index]);
index++;
if(index > myColours.length - 1){
index = 0;
}
}, 1000);

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

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()?>"

How to reference an ID to delete [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
I´m trying to remove a div with jquery but I don´t know how can I refer to the ID because it´s a PHP var: id="delete$count"
My Javascript code:
<script type="text/javascript">
function deleteFile(fname,directory)
{
$.ajax({ url: "delete_img.php",
data: {"file":fname,"directory":directory},
type: 'post',
success: function(output) {
alert(output);
$('div').remove();
}
});
}
</script>
My PHP code:
$count=1;
echo '<div class="gallery-item" id="delete$count"></div>';
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\")'>
</a></div>";
$count++;
I think the below will send delete$count verbatim to the response stream without evaluating $count
echo '<div class="gallery-item" id="delete$count"></div>';
Change it to
echo "<div class='gallery-item' id='delete$count'></div>";
so as for PHP to interpolate $count.
Try then passing this as a third parameter to your function as
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\",\"delete$count\")'>
and collect its value in a third argument to
function deleteFile(fname,directory, deletecount) { }
and replace
$('div').remove();
by $('#' + deletecount).remove();
If i understund correctly, you want to achieve the following:
use AJAX to tell server to delete file
remove same file from current view without reloading page
If you read file information from database, i would suggest you to use row-id or something similar as 'gallery-item' ids, and as argument for deleteFile() function.
In case you dont, you can use paired value of $directory and $file instead.
echo '<div class="gallery-item" id="'. $directory . $file . '" ></div>';
echo "<div><a class='ico ico-delete' rel='9' rev='2' href='#'onclick='deleteFile(\"$file\",\"$directory\")'>
</a></div>";
With this, you can replace your $('div').remove() with$('#'+directory+fname).remove()

Categories