简体   繁体   中英

Call function with param from JavaScript

I hope you can help me.

I'm trying to call a php function from javascript.

when I place a value in concatenation with a string does not work but if I put a fixed value without concatenate if I called the function correctly.

I leave the code I use.

Function from my controller:

function size_make_dropdownlist($size_make_id)
{
    $output = "<select id='size_make' name='size_make' style='width:100px;'>";

    //$output .= $this->new_option('Seleccionar', '0', $size_make_id);

   // $query = $this->car_model->get_car_makes();

    $options = array('product_id' => id_clean($size_make_id));
    $Q = $this->db->getwhere('omc_product_sizes',$options);
    if ($Q->num_rows() > 0){
    $query = $Q->row_array();   


    foreach ($query as $row)
    {
        $output .= "<option value=".$row['product_id'].">Talla ".$row['size']."</option>";
    }
    }
    $output .= "</select'>";

    return ($output);
}

FUNCTION JAVASCRIPT

function sizes(product){
var phpcodeis="<?php $size_make_dropdownlist=$this->MProducts->size_make_dropdownlist("+product+"); echo $size_make_dropdownlist; ?>";

//$size_make_dropdownlist=$this->MProducts->size_make_dropdownlist("+product+"); echo $size_make_dropdownlist; ?>"; //When use this line work perfectly but i need the another one

document.getElementById("php_code_size").innerHTML=phpcodeis;
alert(product);
}

CODE HTML

<a href="#"
   style="display:block; color:#333; font-family:Tahoma; font-size:12px;"
   onclick="sizes(1); return false;"> Sizes 1 </a>
<a href="#"
   style="display:block; color:#333; font-family:Tahoma; font-size:12px;"
   onclick="sizes(2); return false;"> Sizes 2 </a>
<span id="php_code_size"> </span>

or another way to do this?

I would understand a little more hope that this help me be a little easier.

Thanks in advance

You should take note that PHP is executed before JavaScript. In most scenarios, you cannot generate PHP with JavaScript and expect it to run (unless you're using JavaScript at the backend and have some magical bridge to PHP from it).

What you probably want is to make and XHR (AJAX) request from JavaScript and let a PHP service return pure data. JavaScript would then render the option elements from this data, ideally by using an HTML template engine.

you can embed php tags in inline javascript but you can't do it in javascript files. you must either use ajax call to receive value from server. or embed the value in html file before the function definition (not very good approach!)

somewhere in php file (in the head section of html for example)

EDIT:

<head>
<script type="text/javascript">
   var myGlobalFromPHP = <?php echo myFunctionValue ?>;  // enclose it in quotes if it's a string.
</script>
</head>

Then in your javascript file you can use this myGlobalFromPHP

function(){
   alert(myGlobalFromPHP);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM