1

I want to get the selected jquery value and print as a php variable. Here is the code for displaying the text value from jquery script

PHP Code

<script type="text/javascript">
    $(document).ready( function ()
    { 
        $('#selector').change(function()
        {       
            var option = $(this).find('option:selected').val();     
            $('#showoption').val(option).show();
        });
    });
</script>

<?php
   $config = array(
   'DB_HOST'     => 'localhost',
   'DB_NAME'     => 'roles',
   'DB_USER'     => 'root',
   'DB_PASS'     => ''   
   );   
   $db = new PDO('mysql:host=' . $config['DB_HOST'] . ';dbname=' . $config['DB_NAME'], $config['DB_USER'], $config['DB_PASS']);   
   $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

  echo 'Select the role : <select id="selector">';
                     $sel_role = $db->query('SELECT * FROM role');
                     $sel_role->execute();
                     echo '<option selected="selected">Select a role</option>';
                     while ($row = $sel_role->fetch(PDO::FETCH_ASSOC)) 
                     {
                       $role_id = $row['role_id'];
                       $role_name = $row['role_name'];  
                       echo '<option value='.$role_id.'>'.$role_name.'</option>';         
                     }
             echo '</select>';      
  echo '<input type="text" name="name" id="showoption" disabled="disabled" /> ';
?>
Saranya
  • 29
  • 8
  • Is jquery is working or not? – Sudharsan S Jul 03 '14 at 04:57
  • JS is a client-side language. PHP on the other hand is a server-side language. Therefore, PHP is executed first, on the server-side, sent to the client over a connection and JS is executed on the received file during parsing an rendering by the browser. You should consider using AJAX — Google or search it on SO, there are already plenty of examples. – Terry Jul 03 '14 at 04:57
  • Sudharsan@ Yes jquery is working fine and I am getting the correct selected value, but i need to print that value to a php variable – Saranya Jul 03 '14 at 04:58
  • do you want to do it with ajax(without refreshing page ) or by reloading ? – NullPoiиteя Jul 03 '14 at 04:59
  • @NullPointer, I want with refreshing the page – Saranya Jul 03 '14 at 05:00
  • @NullPointer, and I have seen alot of exmaples of jquery.ajax, they have long code for executing one file and for processing one file, but I want my code to be shorten everything on one file – Saranya Jul 03 '14 at 05:01
  • @NullPointer, you were marked as a duplicate question!!!. There was submit form used to get the value, but I need without form. Usually submitting a form will definitely reload a page – Saranya Jul 03 '14 at 05:06
  • Is there any other way of getting a query value to a php variable – Saranya Jul 03 '14 at 05:10
  • no its not compulsory you can stop reload page http://stackoverflow.com/questions/10092580/stop-form-from-submitting-using-jquery – NullPoiиteя Jul 03 '14 at 05:10
  • NullPointer@ I wasn't using any search/submit buttons, I was directly filtering the selected value and I need to get that as a php variable – Saranya Jul 03 '14 at 05:17
  • NullPointer@ I guess I amn't irritating you with my questions, but that was the out put I want to. – Saranya Jul 03 '14 at 05:30

0 Answers0