2
votes

I am using a form having select dropdown. I want to pass the value obtained from the selected option as a $_GET request in form action field but any ways to access it outside the foreach loop. Here is the code sample that I have written

<form id="dynamicForm" action="client-detail-dynamic.php?id=<?php echo $_GET['id']; ?>&r_id=<?php **PASS THE DROPDOWN VALUE ID HERE**  ?>" method="post">
    <select class="form-control" id="dynamicfy" name="dynamicfy">
        <?php     
        $j = 0;
        foreach($payment_data as $pd):
        ?>
            <option value="<?php echo $payment_data[$j]->r_id; ?>"><?php echo $payment_data[$j]->fy; ?></option>
       <?php $j++; endforeach; ?>
   </select>
</td>
<td class="col-md-4">
    <input type="submit" name="submit" id="submit" class="btn btn-sm btn-success">
</td>
</form>

NOTE: $payment_data is an array containing the table data with field names r_id, fy etc

4
what about the post method ? you don't need ? - Sanooj T
No i dont need any post value....only get values needed to for further processing on next page - Abhijit Borkakoty
You want to pass the values on submit ? - Sanooj T
all sorts of possible ways are welcome....i just want the value of the selected dropdown on the action page - Abhijit Borkakoty
You may also get selected dropdown on the action page with using $_POST['dynamicfy'] on action page "client-detail-dynamic.php" . - Maulik Kanani

4 Answers

1
votes

I have two methods for this.

First method

Create a hidden field inside form element to store the value of id.Put form action null

<form id="dynamicForm" action="" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"> 

On submit you will get two values

if(isset($_POST['submit'])){
  $id=$_POST['id'];
  $r_id=$_POST['dynamicfy'];
  header("location: client-detail-dynamic.php?id=" . $id . "&r_id=" . $r_id . "");
  exit();  
}

Second method use javascript

<select class="form-control" id="dynamicfy" name="dynamicfy" onchange="rdrt(this.value)">

<script>
    function rdrt(str){
        id=<?php echo $_GET['id']; ?>;
         if(str!=""){ 
        location.href="client-detail-dynamic.php?id=" + id + "&r_id=" + str; 
        }
    }
</script>
0
votes

Rather than changing the page from FORM ACTION what you can do is pick the values and set them in url passed to header:location.

try this.

    ``<?php


       if(isset($_POST['submit'])
       {
           $option = $_POST['dynamicfy'];
           $id = $_POST['id']
           header('location: http://client-detail-dynamic.php?id=$id,r_id=$option');
       }

       ?>

   <form id="dynamicForm" action="" method="post">
   <select class="form-control" id="dynamicfy" name="dynamicfy">
   <?php 

        $j = 0;
        foreach($payment_data as $pd):

     ?>
     <option value="<?php echo $payment_data[$j]->r_id; ?>"><?php echo                  $payment_data[$j]->fy; ?></option>
    <?php $j++; endforeach; ?>
    </select>
    </td>
    <td class="col-md-4">
    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>
    <input type="submit" name="submit" id="submit" class="btn btn-sm btn-success">
    " />

     </td>
     </form>
0
votes

What you are trying to do is go somewhere based in the $_GET['id]. That's not possible server side as you have to FIRST make the request, then execute code. If your aren't trying to bring form data with you to this URL, then try this suggestion. However forget what I about not possible. you could do something like:

<?php

if(isset($_POST['submit-button'])) {
  header("location: file.php?something=" . $_GET['id']);
}

// set the form action to nothing and add this to the same page the form is on
// and you can redirect based on the $_GET['id']
?>
0
votes

To change value on selection of dropdown, You will need to use a jQuery on change of select box. Please refer following code for same.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
    jQuery(Document).ready(function() {
        jQuery('#dynamicfy').change();
    });

    jQuery('#dynamicfy').change(function() {
        jQuery('#dynamicForm').attr('action', 'client-detail-dynamic.php?id=' +<?php echo $_GET['id']; ?> + '&r_id=' + jQuery(this).val());
    });

</script>

if you just want selected dropdown on the action page then You may also get selected dropdown on the action page with using $_POST['dynamicfy'] on action page "client-detail-dynamic.php"