Incrementing $_SESSION index in jquery


Incrementing $_SESSION index in jquery


<?php
session_start();
$j=0;
?>
<form class="contact100-form validate-form" action="step-3.php" >




Add another Driver




</div>
</form>


$("#add_driver").click(function () {
$( "#add_driver_section").replaceWith( "
Firstname * >
");});



$_SESSION['name'] is an array with multiple values. I need to increment the value of $j so as to get a new index of $_SESSION['name'] every time the add_driver is clicked. This code works only the first time. The rest of the time it prints the same value as that in the first time. I am unable to show you the rest of the code or screenshots of the output because of company rules. Please tell me what is wrong with my code and how to acheive what i'm looking for. I only want some method to increment the index. Thank you in advance.


$_SESSION['name']


$j


$_SESSION['name']


add_driver





use a hidden field to store the value of index.
– Amit Joki
2 days ago





@AmitJoki but then how will i use the value inside php script. can you please show a sample code?
– NEETHI N Nambiar
2 days ago





rather than increment the in php, on click increment a java script variable and then assign it to a php variable. or else store the incremented value in a hidden field or use cookies with jquery. In this way it won't work.
– Vamsi Krishna
2 days ago





@PatrickEvans already tried it. not working.
– NEETHI N Nambiar
2 days ago





@VamsiKrishna but that too will only assign the value once to php.
– NEETHI N Nambiar
2 days ago




2 Answers
2



$_SESSION is a server variable, so you can't change it on the client side without interacting with the server. I would recomend you to use AJAX.
When the button is clicked, an AJAX request loads an empty page that increases the $_SESSION variable. (also you have to store $j also as a session variable, so you can have a track of it)



so for example:



in some file called myFile.php, you increment the index and retrieve the name in that index.


<?php
//this file just increases j everytime is loaded
session_start();
if (isset($_SESSION['j'])){
$_SESSION['j']++;
} else {
$_SESSION['j'] = 0;
}
$result['name'] = $_SESSION['name'][$_SESSION['j']];
die(json_encode($result));



then in you button:


$("#myButton").click(function (evento) {
jQuery.ajax({
url: "myFile.php",
dataType: "json",
method: "POST",
data: {},
success: function (data) {
alert("The name[j] is " + data.name)
// do here what you did in your button before
// DONT use PHP echos here, use the data.name variable to access the $_SESSION['name'][$j] value
}
});
});



Remember that PHP is a preprocesor, so when the page loads in the client side, everithing PHP printed is now a constant. so you cannot use the echoed values and make them change.





is there really no lesser complicated method??
– NEETHI N Nambiar
2 days ago





How often does $_SESSION['name'] changes?
– Dknacht
2 days ago





the value to $_SESSION['name'] is added dynamically so we do not know the count.
– NEETHI N Nambiar
2 days ago


$_SESSION['name']





You could send all the $_SESSION['name'] data drom the beginning, the problem is that if it changes after the user loaded the page, then he won't notice. If the user that loads the page is the same one that changes $_SESSION['name'], then (only then) it's a good option.
– Dknacht
2 days ago



There is another method, and that is to send all the $_SESSION['name'] variable and use it in the client side. the problem is that if $_SESSION['name'] changes after the user loaded the page, then he won't notice.
So it would be like this:
(not tested, please excuse syntax errors)



var names = JSON.parse("");
var count = 0;
$("#add_driver").click(function () {
count++;
alert("now the name you needed is here: " + names[count]) //you have the name in names[count], do what you will
});





can i somehow use this code: if(isset($_SESSION['name']))($j=sizeof($_SESSION['name']); echo "var j=".$j;) in the php script and access $_SESSION directly from javascript using var j as the index and incrementing or decrementing it as reqd.??
– NEETHI N Nambiar
2 days ago


if(isset($_SESSION['name']))($j=sizeof($_SESSION['name']); echo "var j=".$j;)


$_SESSION


var j





You can count it in the client side too. Instead of using sizeof($_SESSION['name']) you can use names.length. I would recommend you to make another question explaining what you want to do (instead of how) so maybe I can help you finding the best way to handle this problem, because I really don't think that this approach of mixing client and server side is the best.
– Dknacht
2 days ago



sizeof($_SESSION['name'])


names.length






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV