NEETHI N Nambiar
NEETHI N Nambiar

Reputation: 55

Incrementing $_SESSION index in jquery

<?php
   session_start();
    $j=0;
?>
<form class="contact100-form validate-form" action="step-3.php" >
 <div class="wrap-input100 validate-input bg1 rs1-wrap-input100">
    <div class="contact100-form-btn" id="add_driver">
        <span>
            Add another Driver
            <i class="fa fa-long-arrow-right m-l-7" aria-hidden="true"> 
            </i>
        </span>
    </div>
 </div>
</form>

<script>
  $("#add_driver").click(function () {  
 $( "#add_driver_section").replaceWith( "<div class='wrap-input100 validate- 
 input bg1 rs1-wrap-input100' data-validate = 'Enter Your First Name'> <span 
 class='label-input100'>Firstname *</span> <input class='input100' 
 type='text' name='name[]' placeholder='Enter Your First Name ' <?php 
 if(isset($_SESSION['name'][++$j])){echo "value='".$_SESSION['name'] 
 [$j]."'";}?> ></div>");});
</script>

$_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.

Upvotes: 0

Views: 111

Answers (2)

user9886608
user9886608

Reputation:

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)

<script>
    var names = JSON.parse("<?php echo json_encode($_SESSION['name']); ?>");
    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
    });
</script>

Upvotes: 1

user9886608
user9886608

Reputation:

$_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.

Upvotes: 1

Related Questions