1

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

wow,thank you very muchhhhhhh,god bless you!you really helped me alot
@adi - If this answer solved your problem, click the Accept Answer checkbox to the left of the answer.
Mixing PHP & JavaScript like that is bad practice ... but well done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.