I have created a page for updation of record. I want to pass the id of student from one page to another. I am trying to send it through window.location but it is not working. In
you can try using cookies
Set the cookie
<?php
setcookie("name","value",time()+$int);
/*name is your cookie's name
value is cookie's value
$int is time of cookie expires*/
?>
Get the coockie
<?php
echo $_COOKIE["your cookie name"];
?>
Populate a <form method="post" [...]>
in the first page with the information needed; you can change their aspect with CSS as desired.
When the <form>
is send you only need a PHP script/page that uses $_POST
to fill the new page.
Easier than AJAX if you try to navigate from the first page to the second.
If you already have a form and wanna post that to an update script, you could just add the student id as an hidden form element example:
<input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
Else if you want to redirect from another page to the update page, with a student id, the best way will probably be a $_GET variable.
So the URL would look something like this: http://domain.com/update.php?student_id=1
And then your update.php will include a simple check like this.
if(!empty($_GET['student_id'])) {
$student_id = $_GET['student_id'];
// Ready to update
} else {
// Throw 404 error, or redirect to an create page
}
When you use window.location then your page go to another page. ajax work on active page. You can not use.
If both pages are at same domain you can use localStorage
, storage
event to pass data between html
documents
At second page
window.addEventListener("storage", function(e) {
// do stuff at `storage` event
var id = localStorage.getItem("id");
});
at first page
// do stuff, set `localStorage.id`
localStorage.setItem("id", "abc");
Generally you can use sessions for this $_SESSION
variable to store it into the session, or you can pass that value via get parameter. And afterwards get that parameter with $_GET
Or $_POST
parameter if you want to submit form.