l3gion
l3gion

Reputation: 73

Call PHP Function in jQuery (var)

I'm facing a small problem that I can't solve by 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 return a select with information from 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 one will add a new row to my table, and for each new input will "activate" autocomplete. What I want to do is, instead of 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></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 php function directly will return nothing, and I can't do anything. Is there any way to accomplish this?

Thank you

Upvotes: 3

Views: 2728

Answers (6)

l3gion
l3gion

Reputation: 73

I solved my problem with a friend's help.

Basically what we've done.

We create a div with the "php echo":

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

Then we've hiden it with css:

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

After this we just called the div at jQuery function:

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

I never thought that it could be so easier. :)

Thank you for everyone who gave me tips and suggestions.

regards

Upvotes: 0

GOsha
GOsha

Reputation: 689

Maybe it will be better to use ajax functions. Give to it name. Call php-script, and get data from output.

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

?

Upvotes: 1

jondm
jondm

Reputation: 11

Try this code: (Please add the html tag to complete the page sice I am not able to add it here)

<?php 

function intervalo_manha(){ return 'here is my data';

$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(); exit; }

?>

<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>

Upvotes: 1

Martin
Martin

Reputation: 16292

You are using $().autocomplete(); to get_coll_name.php. $().autocomplete(); is most likely a jQuery UI or jQuery plugin that uses AJAX to call PHP. You will need to use AJAX to call PHP. You can not specify the method intervalo_manha(), you must specify a page. If you are using a framework, like Zend, this is easier as the framework allows you to specify methods in the call.

Upvotes: 1

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

if all the codes are on the same page, try

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>\");";

Upvotes: 1

Thariama
Thariama

Reputation: 50832

This won't work, because you execute your code inside $(document).ready. This means php is already done and can't be executed after $(document).ready (thats because php is server-side only). Either you will have to use ajax or you will have to do the php function call before calling $(document).ready and put it into a variable:

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

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

Upvotes: 1

Related Questions