0

Ok, So I have a radio button in a while statement...

If the user click the radio button, I want the session variable of order_nums to change to the data['order_num] that relates to that radio button id... How can I achieve this?

Here is my code.

<?php
     $num = '1';
    while($data=$stmts->fetch(PDO::FETCH_ASSOC))
    {   
        if($_SESSION['order_nums'] == $data['order_num'])
            {
            echo "<input type='radio' id='$num++' name='ac'>" . '<label>ACTIVE:</label>' . $data['order_num'];
                echo "<br>";
            } else
            {
            echo "<input type='radio' id='$num++' name='ac'>" . $data['order_num'];
            echo "<br>";

            }

        }
?>
Yagnik Detroja
  • 921
  • 1
  • 7
  • 22
Kmiles1990123
  • 189
  • 2
  • 12
  • Do you have code that processes the form when the information is submitted to it ? You can do what you are trying to do when the user submits the form. – Maximus2012 Jan 08 '16 at 15:28
  • it will not use a submit button no form... Just when the user clicks the radio button I want it to change automatically. – Kmiles1990123 Jan 08 '16 at 15:29
  • So hopefully, This can be achieved with jquery.. Just not sure how. @Maximus2012 – Kmiles1990123 Jan 08 '16 at 15:29
  • In that case you need jQuery/AJAX. You would still need another PHP that does the update for you. You are just not submitting an explicit form. You might want to look for some basic PHP/jQuery/AJAX tutorials: http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php – Maximus2012 Jan 08 '16 at 15:32
  • I understand already I will need jquery/AJAX.. thats why I posted this question because I am not sure how to do it. – Kmiles1990123 Jan 08 '16 at 15:33
  • Try this: http://stackoverflow.com/questions/13152927/how-to-use-radio-on-change-event you need to fire a radio button on change event which then sends an AJAX request to another PHP page that updates the session variables. – Maximus2012 Jan 08 '16 at 15:34
  • Another one: http://stackoverflow.com/questions/3765154/ajax-radio-buttons-with-jquery – Maximus2012 Jan 08 '16 at 15:36
  • Still not understanding. These examples are not changing PHP variables so I dont understand how they are relevant to me. Please enlighten me. – Kmiles1990123 Jan 08 '16 at 15:40
  • @Maximus2012 Plus I am pulling info dynamically from a DB. So my radio button dont have set name... there just numbers that poplulate hence the $num++ – Kmiles1990123 Jan 08 '16 at 15:42
  • The part that changes the PHP variables is the easier part. What you need first is to make sure that the AJAX request gets sent to another PHP page when there is a radio button change event. What you need is to combine the solutions from all the links that I posted. http://stackoverflow.com/questions/6999049/change-session-variable-value – Maximus2012 Jan 08 '16 at 15:43
  • The radio buttons can have dynamic names. At this point though, I would recommend that you start with static radio buttons, make sure your code is working and then move on to the dynamic part. Once you have tried all the things that I recommended, then you can update your question with that information and point out any specific error that you might be getting. – Maximus2012 Jan 08 '16 at 15:44

2 Answers2

0

Got it...

Changed main code to this...

 <?php
                         $num = '1';
                         while($data=$stmts->fetch(PDO::FETCH_ASSOC))
                         {  
                            if($_SESSION['order_nums'] == $data['order_num'])
                            {

                                echo "<input type='radio' name='ac' value=". $data['order_num'] ." onchange='this.form.submit();''>" . '<label>ACTIVE:</label>' . $data['order_num'];
                                echo "<br>";
                            } else
                            {
                                echo "<input type='radio'  name='ac' value=". $data['order_num'] ." onchange='this.form.submit();'>" . $data['order_num'];
                                echo "<br>";                                        
                            }                                                               
                         }
                         ?>

Added this to the very top of my code..

                <?php
            if (isset($_POST['ac']))
            {
            unset($_SESSION['order_nums']);
            $_SESSION['order_nums'] = $_POST['ac'];
            }
            ?>
Kmiles1990123
  • 189
  • 2
  • 12
0

Then try Like Something follow:

Try thorough java script when user click on radio button script called and session value changed.

May this code help you.

echo "<input type='radio' id='$num++' class='cmt' name='ac'>" . '<label>ACTIVE:</label>' . $data['order_num'];

  $(document).ready(function () {
             $('.cmt').live('click', function () {
              var dd= (this).attr(id);
              <?php $_SESSION['order_nums']== dd?>
             });
          });

Give the class to the radio button.

Thanks

Yagnik Detroja
  • 921
  • 1
  • 7
  • 22