Suppose I have 2 pages, page1.php and page2.php. Now my question is that how I can pass data through javascript function from page1.php to page2.php?
A simple way would be to use JavaScript to submit a form. If, for example, you had this form on page1.php
:
<form id="name_form" action="page2.php" method="post">
<p><label>Name: <input type="text" id="name_field" name="name"></label></p>
<p><input type="submit"></p>
</form>
It would be fairly simple to use JavaScript to submit that form with given values:
function submitName(name) {
document.getElementById('name_field').value = name;
document.getElementById('name_form').submit();
}
I would suggest doing it with $_POST
(in case of a post form in page1.php), or $_GET
on page2.php
On my experience, it's more reliable to use them than javascript
Not to mention you can also use $_SESSION
variables to store particular data you're gonna need on another part of the site not directly connected to page1.php or page2.php; or even data you'll be using continuously on some script.