How to we store a javascript function value inside a php variable

function check_val(val) {
alert(val);
return val;
}

this is my javascript function i take the value of val

         <select name='select'  id='select'   onchange='check_val(this.value)'> 
           <option value='0'><------select------></option>
          <option value='4'>apple</option>
        <option value='5'>mango</option>
         <option value='6'>berry</option>



    </select> <?php <input type=hidden name='select' value=???> //by using javascript i need the value

this is my select tag now i want to put the values inside a hidden field and post this value to the next page

This could be done like e.g.

<select name='select'  id='select'> 
  <option value='0'><------select------></option>
  <option value='4'>apple</option>
  <option value='5'>mango</option>
  <option value='6'>berry</option>
</select>
<input type="hidden" name='select'/>

<script type="text/javascript">
var select = document.getElementById('select');
var hiddenSelect = document.getElementsByName('select')[0];

select.addEventListener('change', function() {
  hiddenSelect.value = this.value;
});
</script>

But why would you want to do this? If it’s inside a form, the select value will get posted to your PHP script anyway…

1 Like

i also want to post is to the next page…
so please tell me the php syntax for this… it is the requirement

<?php input type=hidden name='abc' value=????? > all i need how to store the value into dis

i knw it will post still i need it… please tell…

I thought I did?

its nt wrking

how to store the value of javascript into that $abc ???

It doesn’t work like that. When you submit the form, the value of that hidden input will be accessible in PHP with $_POST['select'] or $_GET['select'], depending on the submit method.

1 Like

not at all. JavaScript and PHP run on different machines/times.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.