-1

I'm having a issue trying to get this worked out,not sure how best to execute it.

I Have a Page that opens a Modal on load, and has a Dynamic selection inside of it based on a MySQL data pull. What I want is that when hitting the Selection button that it will read the input selected in the Modal and set a variable I can use with the rest of the Page.

The Code for my modal is

<div class="modal fade" id="CompanySelect" tabindex="-1" role="dialog" aria-labelledby="ModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content regmodel">
      <div class="modal-header">
        <h2 class="modal-title" id="ModalTitle" style="margin:auto;">Select Company</h2>
      </div>
      <div class="modal-body">
        <form class="CompanySelection">
            <div class="selector">
                <?php
                    while($row = mysqli_fetch_array($CompaniesResults)) {
                        $x = $x + 1;
                        $Comp_array = array($row['Company_ID'], $row['Company_Name'], $row['WebID'], $row['Logo']);
                ?>
                <input id="<?php ECHO $Comp_array[2];?>" type="radio" name="Company" value="<?php ECHO $Comp_array[0];?>" required/>
                <label class="Compselect <?php ECHO $Comp_array[2];?>" for="<?php ECHO $Comp_array[2];?>"></label>
                    <?php } ?>
            </div>
        </form>
      </div>
      <button type="button" class="btn btn-primary regbtn" data-dismiss="modal">Select</button>
    </div>
  </div>
</div>

<script type="text/javascript">
    $(window).on('load',function(){
        $('#CompanySelect').modal('show');
    });

    $('#CompanySelect').modal({
        backdrop: 'static',
        keyboard: false
    })
</script>

Basically it will then Set Variable $CompSel = that I can use with the rest of the page without issues. This being a selection of various fields and some text fields that will eventually be saved to the database again using the $CompSel Made from the Modal as well.

Thanks for the Assist ahead of time.

Digi
  • 9
  • 7
  • Possible duplicate of https://stackoverflow.com/questions/18508964/difference-between-server-and-client – misorude Nov 05 '18 at 08:08

2 Answers2

0

You can't set a PHP variable from the client, since PHP is a server side language. PHP is not compiled, but interpreted. This means that every variable you create "exists" only for that request. New request? New variables.

If you want to somehow make a variable persist through multiple requests, you have to store into a session or a database.

Depending on what you want to do, there are different solutions, but for what you are asking, "setting a PHP variable from a modal", the answer is no, you can't.

Ermenegildo
  • 1,286
  • 1
  • 12
  • 19
  • Ok that makes sense Thanks :) ill have to see about making it a session variable as you suggest for the rest of the form. – Digi Nov 05 '18 at 09:56
  • You could create a session variable, but you'll have to reload the page. Once a page is loaded, you have to reload it to "run" PHP again. You should look at basic guides on how PHP works, because it seems it you are confusing server-side and client-side. – Ermenegildo Nov 05 '18 at 10:05
  • Yea I'm very much still learning it as I'm going along, and what limitations there are and aren't when it comes to its functionality. – Digi Nov 05 '18 at 10:09
0

OK I managed to figure out how I could do it. seeing as the Modal isn't gone, just hidden I can still utilize JavaScript to read my radio selection and use that then when I' Doing my post at a later Stage.

Having a read and setting the variable with

var x = $('#CompanySelection input[type=radio]:checked').attr('value');

does the trick for me.

So all I do is use that when I do want the modals Data when posting the main form then.

Digi
  • 9
  • 7