I've been exploring the possibilities of using PHP for the first time in a project lately, but now I've stumbled upon a problem I can't seem to quite wrap my head around – even after looking through various questions here on Stack Overflow.
Basically I have an array which I'd like to randomize values from, and update these values by the click of a link without having to reload the entire page.
data.php
<?php
$var = array(
array("Hello", "0wLljngvrpw", "10", "15"),
array("Hey", "TINASKjjNfw", "20", "25"),
array("Hi", "rzU_fLcxIN0", "30", "35"),
);
// array_rand returns the INDEX to the randomly
// chosen value, use that to access the array.
$finalVar = $var[array_rand($var)];
?>
I tried to borrow an example from Ajax button click that triggers a php file / link, but I haven't got it to work.
index.php
<html>
<body>
<!-- Works, but reloads entire page -->
<?php include('data.php'); ?>
<form>
<input type="submit" action="data.php" method=post value="Randomize">
</form>
<hr>
<?php echo $finalVar[0];?>
<br>
<?php echo $finalVar[1];?>
<br>
<?php echo $finalVar[2];?>
<!-- Borrowed code, can't seem to get it working though -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("data.php");
return false;
}
</script>
<a href="#" onclick="doSomething();">Randomize without reload</a>
</body>
</html>
I've tried using the example from the previous mentioned question without luck, all I get is /?# at the end of the url (which also seem to break the first working submit action?) and no change in the random strings. I'm very new to this so I'm sorry if this isn't really a valid question, but it feels like there must be something pretty fundamental I've messed up or misunderstood here, no?
hrefbut this<input type=submit action="data.php" method=post value="Randomize">is missing quotes. So do<input type="submit" action="data.php" method="post" value="Randomize">and it will work.