views:

132

answers:

6

Hello,

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

+1  A: 

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
...
}
Thariama
My php function is done before calling the "js" function. I'll give a try to your approach.Thank you
l3gion
Tried your suggestion without success. After adding the var, my add new row button doesn't work anymore.
l3gion
The HTML page is created by PHP so it is no problem to inject the return value from the PHP function. This has nothing to do with `$(document).ready()`. For PHP, this is just text.
Felix Kling
Ohh, I understood Felix. Thanks for the explanation.
l3gion
@l3gion: This was more a comment to Thariama but you are welcome ;)
Felix Kling
@Felix Kling: you are right
Thariama
@I3legion: did you check if your function actually returns a non-empty string?
Thariama
@Thariama, php function return a select with info from DB. Js function, create a new row.Can you be more specific? Thank you.
l3gion
@I3legion: i know, what about using $("#dataTable").innerHTML="<?php echo intervalo_manha(); ?>";
Thariama
@Thariama, I can't do that. If I add that code into my function the Button to add a new row won't work. :(
l3gion
+1  A: 

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>\");";
Reigel
Thank you Reigel for your answer.I already tried that approach but it didn't worked. Now I can't even add a new row in my table.
l3gion
+1  A: 

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.

Martin
A little more help, or more details, would be appreciated. Thank you
l3gion
+1  A: 

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>
jondm
Tried your code jondm. Thanks.But, can you explain to me what should be the output?
l3gion
I get what you wanted to do. Unfortunately, I couldn't solve my problem.Any other suggestion?
l3gion
+1  A: 

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

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

?

GOsha
Thank you for trying to help me. I'll try it tomorrow, too tired today. :)
l3gion
A: 

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

l3gion
Probably we could do it in a cleaner way. It works and that's the important thing.
l3gion