Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  151] [ 1]  / answers: 1 / hits: 36917  / 10 Years ago, thu, may 8, 2014, 12:00:00

I know this has been answered many times but what i am looking for is the passing of variables within the same page. I understand that PHP is a server side script while JS is the client side thus when the page loads, it will load PHP before JS thus it is impossible to do so.



What i am seeking is an alternative method to perform my JS task which is to take the value after the ? in the address bar (//localhost/Task/delete.php?ID=1). Else alternatively is there a way around passing the variable into PHP as the value will be used to execute a SQL query.



Thanks



<script language=javascript type=text/javascript >
var url = window.location.href;
var params = url.split('?ID=');
var fdf = (params[1])
alert(fdf);

</script>

<?php
$random = $_GET[fdf];

echo $random;
?>

More From » php

 Answers
5

HTML Code



<div id=content></div>


Javascript Code



$(document).ready(function(){
var url = window.location.href;
var params = url.split('?ID=');
var id = (params[1]);
$.ajax({
type:POST,
url:page.php,
data:{id:id},
success:function(result){
$(#content).html(result);
}
});
});


PHP Code: page.php



<?php
$random = $_POST[id];
echo $random;
?>


Complete One page code: demo.php



Note: URL for this page must be demo.php?ID=someValue



<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script src=js/jquery.js></script>
<script>
$(document).ready(function(){
var url = window.location.href;
var params = url.split('?ID=');
var id = (params[1]);
$(#submit).click(function(){ $.ajax({
type:POST,
url:demo.php,
data:{id:id},
success:function(result){
$(#content).html(result);
$(#submit).hide();
}
});
});
});
</script>
</head>
<body>
<button id=submit>Click Me</button>
<div id=content></div>

</body>
</html>
<?php
$random = $_POST[id];
echo $random;
?>


Note: Don't forget to include jquery library file


[#71128] Tuesday, May 6, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
morrismilom

Total Points: 230
Total Questions: 96
Total Answers: 114

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
;