0

I have a PHP echo statement of a session value like this:

<?php echo $userinfo['RenewAgreementAmount'] ;?>

I have a form select element like this:

<select id="Lock" name="Lock">
                      <option value="1" selected="selected">1 Year</option>
                      <option value="2">2 Years</option>
                      <option value="3">3 Years</option>
                      <option value="4">4 Years</option>
                    </select>

I'm trying to update the <?php echo $userinfo['RenewAgreementAmount'] ;?> output by taking what the user selects in the select value id Lock with this session value while interacting with the form?

I've tried <?php echo ($userinfo['RenewAgreementAmount'] * #Lock) ;?> with no luck.

What is the easiest way to update this value as the user interacts with the select form input in real time?

3
  • @pr1nc3 okay thanks...any pointers where to do this? Commented Dec 8, 2017 at 15:49
  • Give me some time to provide you an answer that explains what you need. I will come back to you. Commented Dec 8, 2017 at 15:50
  • @pr1nc3 thanks so much! I could not find an answer that was very clear...I think this would be a good question/answer combo for the community. Commented Dec 8, 2017 at 15:51

4 Answers 4

2
<script>
renewAmount = <?= $userinfo['RenewAgreementAmount']; ?>
function calc(year){
    var amount = renewAmount*year;
    // here you can perform an ajax request to update the amount on server
}
</script>
<select id="Lock" name="Lock" onchange="calc(this.value)">
  <option value="1" selected="selected">1 Year</option>
  <option value="2">2 Years</option>
  <option value="3">3 Years</option>
  <option value="4">4 Years</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

2

it's because your PHP is interpretted before your HTML. That's the whole point of PHP. One way you can work around that is to store your $userinfo['RenewAgreementAmount'] into a Javascript variable and works with this variable. EX:

<script>var RenewAgreementAmount = <?php echo $userinfo['RenewAgreementAmount'] ?></script>

And then, you can put the value of the newly created RenewAgreementAmount javascript variable where you want to show it using :

<span id="renewAgreementAmountField"></span>
<script>
    document.getElementById("renewAgreementAmountField").innerHTML = RenewAgreementAmount
</script>

You can then work with your variable and change the value.

RenewAgreementAmount = RenewAgreementAmount * document.getElementById("Lock").value;

If you want to update the value on the server side, you can use an AJAX request ( XMLHttpRequest in plain Javascript) with your new variable.

Comments

2

Ok so you have this dropdown and you want to get the real-time options everytime something is selected:

   $('#Lock').on('change',function() {
       var option
       option = $('#Lock').val()

       $.ajax({
           type: 'post',
           url: "your_php_file.php",
           dataType: 'data',
           data: {
               option: option
           }
       })
   })

This code will get the value every time a value is selected from this dropdown and will make an ajax call to your php class that you want to handle. your php class name should be put in the url: field

After that in your php you just retrieve the data like this:

$option=$_POST['option'];

Well this means that you execute an ajax call everytime something is selected. I propose that you put a button and trigger the ajax when the user clicks the button, so setup and onClick event.

Don't forget in your html-head to include the jquery version :

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>

1 Comment

anychance you can do a fiddle? I'm having trouble making this work.
1

Approach using some jQuery, would be something like this--

<?php
session_start();

$_SESSION["RenewAgreementAmount"] = 50;

?>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
    </head>
    <body>
        <select id="Lock" name="Lock">
            <option value="1" selected="selected">1 Year</option>
            <option value="2">2 Years</option>
            <option value="3">3 Years</option>
            <option value="4">4 Years</option>
        </select>

        <script>
            $(document).ready(function() {
                $("#Lock").bind("change", function(index){
                    var value = $(this).val();
                    //console.log("Changed to: " + value);
                    $.post("updateSessionValue.php". {"newVal": value});
                });
            });
        </script>
    </body>
</html>

For updateSessionValue.php page:

<?php
session_start();
$_SESSION["RenewAgreementAmount"] = $_POST["newVal"];

Obviously, just an example code... make sure to take all the necessary security precautions!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.