Page 1 of 1

PHP and Javascript - Do I need ajax?

Posted: Sat Feb 24, 2007 1:58 am
by facets
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey Gang,

I need to use 'something' like the following code to add a row of select drop downs from a db. Is there a way to do this? I know JS is client side and PHP is server side so I know there is a limitation. Does anyone have an example or tutorial on how do to something like it.

[syntax="html"]
<SCRIPT LANGUAGE="JavaScript">

  function addRow(id){
    var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR")
    var td1 = document.createElement("TD")
        td1.appendChild(document.createTextNode("column 1"))
    var td2 = document.createElement("TD")
        td2.appendChild (document.createTextNode("column 2"))
    row.appendChild(td1);
    row.appendChild(td2);
    tbody.appendChild(row);
  }
</script>

<BODY bgcolor=#009999>

<a href="javascript:addRow('myTable')">Add row</a>
<table id="myTable" cellspacing="0" border="1">
  <tbody> 
    <tr>    
      <td>row1_column1</td><td>row1_column1</td>
    </tr>   
  </tbody>
</table>

</body>
I did try the following but it only wrote the functions output 'code' to the page.

Code: Select all

 td1.appendChild(document.createTextNode(\"".deliveryTypes()."\"))
Is this what 'ajax' is for?

tia, Will./


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Feb 24, 2007 4:16 am
by Kieran Huggins
If you want to modify your existing xhtml code to include / reflect information from a remote server without refreshing, then yes: that's exactly what ajax is for :-)

I'm always quick to advocate jQuery (check my sig) since it's fast, reliable, and easy to grok. You would want something like:

Code: Select all

 // "lazy way" warning!
$('<tr/>').appendTo($('#myTable tbody')).load('getRow.php',{arg1:something,arg2:other});
Then getRow.php would return (depending on the arguments) :

Code: Select all

<td>col1 stuff</td><td>col2 stuff</td>

Posted: Sat Feb 24, 2007 5:15 am
by facets
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok. looking through jquery now. So to go back to my original code (not posted earlier), I wish to use this select (for starters) as part of the 'Add another row' loop.

So could jquery call a function?

Code: Select all

echo "<td width=150px>\n";
$sql_query = mysql_query("SELECT id, prod_name from product ORDER BY prod_name ASC");
echo "<select name=\"productId[$section]\">\n";
echo "<option value=\"\">-- Please Select --</option>\n"; 
while(list($id, $prod_name)=mysql_fetch_array($sql_query)) {
                $prod_name = stripslashes($prod_name);
                echo "<option value=\"$id\">$prod_name</option>\n";     
}
echo "</select>";
mysql_free_result($sql_query);
echo "</td>\n";

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]