-1

In a drop down select field I pull several options based on a mysqli query

Once I select them, I use javascript to pick up the variables and infill form fields

Once I select the desired option, a form field infills with a reference number

I want to use that reference number for another select field to query the DB for a separate list

Here is the select to get the ref #

<select id="cdetail" name="cdetail"><option value="">Select</option> 
<?
$mlo="SELECT * from client WHERE loan_originator ='$uname'";
$mlo_res = mysqli_query($dbc, $mlo);
while ($row = $mlo_res->fetch_assoc()){

echo "<option value=" . $row['filename'] . ">Filename: " . $row['filename'] . "</option>";
?>
</select>

Once I make the choice from the client table, I will see the filename in the 'cdetail' form field

I want to add another select field to pull from another table based on the selection in the above select option

<select id="ndetail" name="ndetail"><option value="">Select</option> 
<?
    $filename = $row['filename'];

$quote1="SELECT * from nclient WHERE filename ='$filename'";
$quote_res = mysqli_query($dbc, $quote1);
while ($row1 = $quote_res->fetch_assoc()){

echo "<option value=". $row1['ln_amt'] . ">Loan Amt: " . $row1['filename'] . "</option>";
?>
</select>

Trying to pull the filename from the 1st query and creating a global variable did not work the way I coded it.

I was able to use javascript to push the value into a separate field:

<script>

$('#cdetail').on('change', function () {

var x = $('#cdetail option:selected').val()
$('#flnm').val(x);

});

</script>

This will populate (on change) the flnm field with the filename, but I have not been able to grab that input and convert it into a php variable

i.e.

<? $filename = $('#flnm').val(x); ?>

or

<? $filename = ('#flnm').val(x); ?>

or

<? $filename = val(x); ?>

or

<? $filename = flnm; ?>
cdr1234
  • 1
  • 1
  • 3
    PHP is run on the server, Javascript on the client. Submit a form or use AJAX to post a JS value to a PHP script – brombeer Aug 12 '19 at 22:00
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – misorude Aug 13 '19 at 08:10

1 Answers1

0

Short answer: No, you can't pass variables from javascript to php like that.

But you can do it the other way around (passing php variable to javascript in a php file):

<script>
var variable = '<?= $phpVariable ?>';
</script>

What you can do, is to use AJAX requests to pass the javascript (client side) variables to php (server side).

Mahmoud Abdelsattar
  • 1,299
  • 1
  • 15
  • 31