Click here to Skip to main content
16,017,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello all
i have been researching and trying different ways to add a textbox and label by a button click but i have been having nothing but issues. I am currently working on a website where i can add a new text field with a label with a click.
once i have this working i am going to have it so i can have multiple tables in a database so a user can have multiple phone numbers.
im using a table of phone numbers with a ID that will connect to a user in the main table. I am using phpmyadmin to store the data.
Here is what i have i know i have to be close but dont see where i went wrong.

JavaScript
<script type="text/javascript" language="JavaScript">
    function NewPhoneData()
    {
        /*
        var testphome "<input type='text' id='txtphone' />";
        var area = document.getElementById("addPhone"); //.value

        area.appendChild(testphome);

        var newtxt = "<input type='text' name='phoneNumba' />";
        var area = document.getElementById("addPhone");

        document.getElementById(area).innerHTML = newtxt;

        var txtPhone = document.createElement("input");
        var lblPhone = document.createElement("Label");
        lblPhone.innerHTML = "Phone Number #";

        txtPhone.setAttribute("type", "text");

        var addIT = document.getElementById("addPhone");

        addIT.appendChild(lblPhone);
        addIT.appendChild(txtPhone);

        //create label
        var txtLabel = document.createElement("Label");
        //txtLabel.setAttribute(for,txtbane);
        txtLabel.innerHTML = "Phone Number #";
        parentDiv.appendChild(txtLabel);
      //create dynamic txtbox
     // var txtbox = document.
     */
    }


After i can add a new field here is my php code so far maybe people can point me in the direction how to add data to multiple tables at once

PHP
  //phone number field
  if (empty($_POST['homePhone']))
  {
      echo 'ERROR: Please enter a valid employee name';
      $inputError = true;
  }
  else
  {
// make sure the string is properly escaped
      $homePhone = $mysqli->escape_string($_POST['areaHome'] . $_POST['homePhone']);
      //echo "It Worked!!";
  }

  if ($inputError != true)
  {
      // build our insert query -- we can put php variables directly in the string
      $sql = "INSERT INTO people (address, email, firstName, lastName, mainPhone)
       VALUES ('$address', '$email', '$fName', '$lName', '$homePhone')";
      if ($mysqli->query($sql) === true)
      {
        echo 'New employee record added with ID: '  . $mysqli->insert_id;
      }
      else
      {
        echo "ERROR: Could not execute query: $sql. " . $mysqli->error;
      }
  }


Thank you for your time
Posted

1 solution

To dynamically add controls to your form please have a look at
Can I add a row 0f 6 textboxes dynamically using javascript? Please give me a code snippet[^]

Hope it helps you achieve what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900