0

I Edited

I have a html

<select name="action-grid">
    <option  selected > Select</option>
    <option  value="1" >Edit</option>
</select>   
<select  name="action-grid">
   <option  selected > Select</option>
   <option  value="2" >Edit</option>
 </select>
<?php $_product = $this->getProductBy($values);?>
<div><?php echo $_product->getName();?></div>

in code php. i have function getProductBy($value){....}. in view i have grid product, in column action i want when chose option edit value will set variable $value.

5
  • 1
    You can't set javascript variable value to PHP variable on the same page you need to do ajax call if you want to set PHP variable value. Commented Nov 20, 2015 at 6:56
  • Could you please brief what exactly you want to do with that php variable? Commented Nov 20, 2015 at 7:02
  • You cannot pass variable values from the current page javascript to the current page PHP code... PHP code runs at the server side and it doesn't know anything about what is going on on the client side. You need to pass variables to PHP code from html-form using another mechanism, such as submitting form on GET or POST methods or if you do not want to reload the page then using AJAX Commented Nov 20, 2015 at 7:07
  • in code php. i have function getProductEdit($value){....}. in view. i have grid product. in column action. I want when click edit value product_id of product will set variable $value. Commented Nov 20, 2015 at 7:09
  • Could you please mention this in the post instead in comments? Commented Nov 20, 2015 at 7:11

3 Answers 3

2

Html code, (Assuming Jquery included)

<script type="text/javascript">
 function getNewFunction(val) {
  $.ajax({
    url: "./myPhp.php",
    method: 'POST',
    data: {
        'value': val
    },
    success: function(res) {
        console.log(res);
    }
  });
 }
</script>    
<select onchange="getNewFunction(this.value)" name="action-grid">
 <option  selected > Select</option>
 <option  value="1" >First</option>
</select>

myPhp.php

<?php
    echo $_POST['value'];
?>
Sign up to request clarification or add additional context in comments.

Comments

1

As per you mentioned in comments

Your script

<script type="text/javascript"> 
    function getNewFunction(gridValue){
        $.ajax({
            type: 'POST',
            url : 'some_file.php',
            data: {value: gridValue},
            success: function(response) {}
        })
    }
</script>

in some_file.php file

<?php
$value = $_POST['value'];

//your funtion here
function getProductEdit($value)
{
    .....
}
?>

Comments

0
<select onchange="getNewFunction($('option:selected', this))" name="action-grid">
    <option selected>Select</option>
    <option value="2">Second</option>
</select>
<script type="text/javascript">
    function getNewFunction(value) {
        alert(value.text());
    }
</script>

try this way

demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.