-1

Below is my code:

index.php file

javascript of index.php file

function Result()
{
  var marks = 55;
  document.getElementById("hdnmarks").innerHTML= marks; 
  window.location = "results.php";
}

HTML of index.php

<input type="hidden" name="hdnmarks" id="hdnmarks">

Description: I have a web page with url localhost/index.php. In index.php, I have a submit button on the click of which I call Result javascript method which sets the marks = 55 and put it into the hidden field and takes me to the results.php page.

In results.php, I have to insert value of marks in the database. But how should I access the marks as those were stored in the hidden field of index.php file?

I want to put marks in the session, but how should I maintain the PHP session in javascript function? I mean where and when should I put marks in the session before moving to results.php?

0

4 Answers 4

2

you can start session on your page like <?php session_start();?> and create hidden field for session like this

<input type="hidden" name="mysession" id="mysession">

and modify javascript function some thing like this

function Result(){
  var marks = 55;
  document.getElementById("mysession").innerHTML= <?php echo session_id();?>; 
  document.getElementById("hdnmarks").innerHTML= marks; 
  document.getElementById('Form').submit();
}

change the Form name with your form name

Sign up to request clarification or add additional context in comments.

8 Comments

How can I assign marks in the session in this javascript function in index.php so that I can use that session in results.php?
use $_SESSION['marks'] = 55; to assign any value but make sure your session is started session_start() on top of the page.
You are saying me to set marks in the session in the php code but as per my requirement when I come to javascript Result function, I have to do something and then redirect to another page. So I have to set the session in that javascript function not in the php code.
You can reassign the value again, after doing something But you don't need to start session again. session would started once on a page.
So will this code inside Result function work? <?php $_SESSION['marks'] = $marks; ?> NOTE: I have already started the session on the top of php page. I just want to know where should I assign the marks session?
|
2

your question have two parts

1)

But how should I access the marks as those were stored in the hidden field of index.php file?

the standard way is using a form

<form action="index.php" method=POST>
  <input type="hidden" name="hdnmarks" id="hdnmarks">
</form>

submit that form using a button or javascript to POST data to index.php

in index.php

<?php

$marks = $_POST['hdnmarks'];

?>

2)

I mean where and when should I put marks in the session before moving to results.php? you have to start session and make a session variable

index.php

<?php
session_start();
$marks = $_POST['hdnmarks'];
$_SESSION['marks'] = $marks;
?>

result.php

<?php session_start() ?>
...
//javascript code
var marks = <?php echo $_SESSION['marks'] ?>
...

NOTE: this isnt a very good way of passing data from one to another nor a good way of passing data from php to javascript and if you are using a database, session has no use in this as well

4 Comments

Can I write <?php session_start(); $_SESSION['marks'] = $marks; ?> in my javascript function "Result"?
yeah thats basically what i've written above :S
Standard position to start session is top of the page you can't start it like that
as long as it is at the top of the page there's no problem :S @nkp oh now only i got what you asked, no you cant put session_start with the rest of in inside the function
0

Just Javascript Use This:

function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}

getCookie('PHPSESSID');

Comments

0

We can access the php $_SESSION from javascript provided that the javascript code that access the session is included within a .php file. This kind of javascript code is called inline javascript.

// index.php

<html>

  <script>
     const userId = '<?php echo $_SESSION["user_id"]; ?>'
     console.log('The user id is', userId)
  </script>
  <script src="other.js"></script>

</html>

// other.js

console.log('Once again, the user id is', userId)

The html that will be generated will have a script with the userId already set. The variable can be accessed from other scripts that come after the inline script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.