0

I'm trying to capture n value in order to use it inside a loop, like this:

test.php

<!DOCTYPE html>
<html>
    <head>
        <script>
            function store(val) {
                var n = val;
            }
        </script>
    </head>
    <body>
        <form>  
            <select onchange="store(this.value)">
                <option value='10'>abc</option>
                <option value='20'>def</option>
            </select>
        </form>
        <div>
            <?php
                for ($i = 0; $i < **?? n ??** ; $i++) {
                    echo '<input type="radio">';
                }
            ?>
        </div>
    </body>
</html>

If abc is selected, create 10 radio buttons; in case of def, 20. Would it be possible in this case? I've tried many examples but they didn't work so far. Any help? Thanks!

  • which is selected ? – Ahmed Ginani May 12 '17 at 11:40
  • Why do you want to use php ? You can easily do this in javascript. – abhishekkannojia May 12 '17 at 11:40
  • That's not possible. php is evaluated server-side before the html is rendered. javascript is evaluated after html has loaded. – Alex Tartan May 12 '17 at 11:40
  • Php is executed before any javascript, maybe you could do what you want using javascript and html DOM – HReynaud May 12 '17 at 11:40
  • here you can not access the var n in php but there another way to create the radio button depending on dopdown change... – Ashu May 12 '17 at 11:41
  • more info : http://stackoverflow.com/questions/23430455/in-html-with-javascript-create-new-radio-button-and-its-text create radio button in javascript onchange of dropdown value html :
    – Ashu May 12 '17 at 11:51
  • This approach will not work Another approach is using Ajax onchange select make an ajax call to server and you can send '' as a result from the server and just append the result with the select you can refer this tutorial(http://wiki.workassis.com/jquery-ajax/) for making ajax call – Bikesh M May 12 '17 at 11:53
  • @abhishekkannojia how would you do that? I've tried with document.getElementById('id').innerHTML=...; but it wasn't successful. – user3230199 May 12 '17 at 11:59

1 Answers1

0

There is no possibility to call php directly from javascript. What you have to do is to perform a local ajax request to the location of your php script passing along the data as parameters.

What you could do is to make the form action target your php script and handle everything there.

Timothy Karvonen
  • 347
  • 1
  • 2
  • 9