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