Here's the code I use and it doesn't work when I try echo $_POST['count'];
on page.php
<input type="submit" value="go" id="more">
<script>
$(function(){
$('#more').click(function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "page.php",
data: {"count":3} //saving number 3
});
});
});
</script>
In page.php.
<?php echo $_POST['count'];?> //doesn't work.
$number
is undefined in your JavaScript. When this happens, most if not all browsers don't post the data. It is undefined because the variable doesn't exist in the JavaScript scope. I think you meant to echo the PHP value.
Try this
<?php session_start();
$_SESSION['n'] = 3;
$number = $_SESSION['n'];?>
<input type="submit" value="go" id="more">
<script>
$(function(){
$('#more').click(function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "page.php",
data: {"count":<?php echo($number);?>}
});
});
});
</script>
$.ajax sends out a request from the client to a remote server, and then gets a response. Your page.php script needs to echo out some type of a response text, that you can then use on the Client side UI. I think you might be trying to submit a form. Which would look like this:
<?php
session_start();
if(isset($_SESSION['n'])){ $number = $_SESSION['n']; }
else {$number = "<DEFAULT VALUE>";}
?>
<body>
<form name="formname" action="<?php echo "page.php?number=$number ?>" method="post">
<input type="submit" value="go" id="more">
</form>
</body>
Then on page.php, you can get the number variable like this: $number = $_POST['number'];
*Note: You can pass variables to a remote script using $.ajax(). That remote script would then take those variables, and issue back a response. Here is some documentation on using ajax with JQuery Click here
To echo a session variable on the same page, you can use a named hidden input and using the value from the $number
variable; i.e.: value="<?php echo $number;?>"
, then using a <div>
as the element to show up inside of.
page.php would contain:
<?php
echo $_POST['count'];
This is the code that you will load in your browser, and using jQuery:
<?php
session_start();
?>
<?php
$_SESSION['n'] = 3;
$number = $_SESSION['n'];
?>
<html>
<head>
<script src="//code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#submit-button').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'page.php',
data: $("#theform").serialize(),
success: function(d){
$(".result").html(d);
}
});
});
});//end document ready
</script>
</head>
<body>
<div id="some_id">
<form id="theform">
<div>
<input type="hidden" id="count" name="count" value="<?php echo $number;?>" />
</div>
<div>
<button id="submit-button">Submit</button>
</div>
</form>
</div>
<div class="result"></div>
</body>
</html>
I think it's easier to create formdata object and append input or some private data to it. After that send this object with xmlhttp object to server.
var formdata = new FormData();
formdata.append("count", 3);
var xmlhttp;
..................
xmlhttp.send(formdata);
$count = $_POST['count'];