Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a javascript function like this:

function addfamily(divName){
    var newdiv = document.createElement('div');
    newdiv.innerHTML = '<input type="text" name="family[]" size="16">';
    document.getElementById(divName).appendChild(newdiv);
}

which dynamically adds textbox to the form and a php script like this:

<?php
$result_family = mysql_query("SELECT * FROM family_member where login_id='$_SESSION[id]'");

$num_rows_family = mysql_num_rows($result_family);

if ($num_rows_family>0) {

   while($row_family = mysql_fetch_assoc($result_family)){

   echo "<script language=javascript>addfamily('family');</script>";
   }
}

having this code the textboxes are added fine. i just need to know how can i set a dynamic value as the textbox value by passing the php variable $row_family[name] to the function and the value of the textbox??? please help

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Since you want to pass the name of the Div along with $row_family['name'] your javascript function should look like

function addfamily(divName,familyName){
  var newdiv = document.createElement('div');
  newdiv.innerHTML = "<input type='text' name='family[]' size='16' value=" + familyName + ">";
  document.getElementById(divName).appendChild(newdiv);
}

and then the call from PHP should be like

echo "addfamily('family',$row_family['name']);";

HTH

share|improve this answer
    
wow,thank you very muchhhhhhh,god bless you!you really helped me alot –  adi Oct 12 '09 at 13:03
    
@adi - If this answer solved your problem, click the Accept Answer checkbox to the left of the answer. –  Jeremy Stein Oct 12 '09 at 13:51
    
Mixing PHP & JavaScript like that is bad practice ... but well done –  Baba Oct 20 '12 at 11:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.