Reputation: 49
I have a problem. I have this code:
<INPUT TYPE="button" value="Evaluare" onClick="punctaj=1;
if (i1[1].checked) punctaj=punctaj+3;
if (i2[0].checked) punctaj=punctaj+3;
if (i3[3].checked) punctaj=punctaj+3;
alert('Ai obtinut nota '+punctaj+'!');">
How can I send punctaj's value to a php variable, without reloading the page?
Upvotes: 0
Views: 67
Reputation:
You need to use Ajax plus json to communicate with jquery and PHP.. syntax of Jquery and Ajax
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<title>Jquery Ajax</title>
<script type="text/javascript">
$(document).ready(function(e) {
$('#but').click(function()
{
$.ajax({
type:'POST',
url:"values.php",
data:"Test_Val="+$('#test').val()+"",
success: function(result)
{
alert(result);
}
});
})
});
</script>
</head>
<body>
<input type="text" name="test" value="Hello World!" id="test" />
<input type="submit" name="submit" id="but" />
</body>
</html>
values.php
<?php
if(isset($_POST['Test_Val']))
{
$value=$_POST['Test_Val'];
echo $value;
}
?>
Upvotes: 2
Reputation: 112
You can use
document.cookie="name=value";
Then when PHP is ready to read it you can use
$var = $_COOKIE['name'] ;
Be aware that PHP will not be able to read javascript cookies on the same page load. Everything PHP happens BEFORE anything javascript happens.
You could use AJAX to make javascript send values to a dedicated PHP page through POST or GET. Dont get trapped into wondering why your php cant read the javascript var even though the javascript var is above the php code. It will never work.
Upvotes: 0