PHP function call in jQuery (var)

I ran into a small problem that I cannot solve myself.

I have this php function:

function intervalo_manha(){
    $que="select id_intervalo,data_15
          from intervalo_manha
          order by id_intervalo";
        $re=mysql_query($que);
        $object.="<select>";
        $object.="<option></option>";
    while(list($id_intervalo, $data_15)=mysql_fetch_row($re))
    {
       $object.= "<option value=\"".$id_intervalo."\">".$data_15."</option>"; 
    }
        $object.="</select>";
return $object;
}

This function returns a selection with information from the database.

I also have this js function:

$(document).ready(function() {
              var destTable = $("#dataTable");
              $("#btnAdd").click(function() {
               var newRow = $("<tr style='margin-left:-60px'><td><INPUT type='checkbox' name='chk'/></td><td><INPUT type='text' name='txt[]' id='txt'/></td><td></td></tr>");
               $("#dataTable").append(newRow);
                newRow.find('input').autocomplete("get_cols_name.php", {
                    width: 260,
                    matchContains: true,
                    selectFirst: false
                    });
                });
            });

This will add a new row to my table, and for each new entry it will "activate" autocomplete. I want to do this instead:

var newRow = $("<tr style='margin-left:-60px'><td><INPUT type='checkbox' name='chk'/></td><td><INPUT type='text' name='txt[]' id='txt'/></td><td></td></tr>");

I would like to have something like this:

var newRow = $("<tr style='margin-left:-60px'><td><INPUT type='checkbox' name='chk'/></td><td><INPUT type='text' name='txt[]' id='txt'/></td><td><?php echo intervalo_manha(); ?></td></tr>");

Calling the php function directly will not return anything, and I cannot do anything. Is there any way to do this?

thank

+3
source share
6 answers

I solved the problem with the help of a friend.

Basically what we did.

We create a div with "php echo":

<div id="intervalo-manha"><?php echo intervalo_manha(); ?></div>

Then we hid it with css:

<style type="text/css">
    #intervalo-manha {
        display: none;
    }
</style>

div jQuery:

var newRow = $("<tr style='margin-left:-60px'>...<td>" + $("#intervalo-manha").html() + "</td></tr>");

, .:)

, .

0

, $(document).ready. , php $(document).ready( , php ). ajax, php $(document).ready :

var php_function_result = "<?php echo intervalo_manha(); ?>";

$(document).ready(function() {
...
// use php_function_result here
...
}
+1

,

echo "var newRow = $(\"<tr style='margin-left:-60px'><td><INPUT type='checkbox' name='chk'/></td><td><INPUT type='text' name='txt[]' id='txt'/></td><td>";
echo intervalo_manha();
echo "</td></tr>\");";
+1

$(). autocomplete(); get_coll_name.php.. $() (); , JQuery UI jQuery , AJAX PHP. AJAX PHP. intervalo_manha(), . , Zend, , .

+1

: (, html, , )

<?php 

intervalo_manha() {   return ' ';

$que="select id_intervalo,data_15
      from intervalo_manha
      order by id_intervalo";
    $re=mysql_query($que);
    $object.="<select>";
    $object.="<option></option>";
while(list($id_intervalo, $data_15)=mysql_fetch_row($re))
{
   $object.= "<option value=\"".$id_intervalo."\">".$data_15."</option>"; 
}
    $object.="</select>";

return $object; }

if ($ _ GET ['get_data']) {   echo intervalo_manha();   ; }

? >

<head>
    <title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>

    <script type="text/javascript">
    $(document).ready(function() {
        var destTable = $("#dataTable");
        $("#btnAdd").click(function() {

         var newRow = $("<tr style='margin-left:-60px'><td><INPUT type='checkbox' name='chk'/></td><td><INPUT type='text' name='txt[]' id='txt'/></td><td class='d_value'>...retrieving data...</td></tr>");
          $("#dataTable").append(newRow);

          //newRow.find('input').autocomplete("get_cols_name.php", {
           //   width: 260,
            //  matchContains: true,
             // selectFirst: false
              //});
             $.get('index.php?get_data=1', {}, function(data) {
              newRow.find('.d_value').html(data);
             });
         });
      });

    </script>
</head>
<body>
<input type="button" value="click me" id="btnAdd" />
<table id="dataTable">

</table>
</body>
+1

It might be better to use ajax functions. Give him a name. Call php-script and get the data from the output.

$.ajax(/*...*/);

?

+1
source

Source: https://habr.com/ru/post/1747955/


All Articles