Jump to content

[SOLVED] Dynamic text boxes


viper6277

Recommended Posts

Hi everyone, I'm new member to the forum, this website has been so useful in the past on solving my PHP problems and helping me understand more about php coding in general.

 

I am currently working on upgrading my companies website with PHP and MySQL, and I hit a stumbling block maybe because I'm not to good at PHP theory, or the flow of data...?

 

Here's my problem...

 

I have a form where field techs can close out service calls, Time in, time out, work done, who the tech is....ect...

 

Currently I have 20 fields for parts..... Part1 / Part1QTY....and so on to Part10 / Part10QTY....

 

the form emails, and it also feeds data into MySQL... so everything is working fine...

 

What I would like to do, is stream line it...so there is only 2 fields...part_number and qty....and when techs needs to enter more parts then click on a button that creates 2 new sets of text boxes for input....

thus allowing me to have them enter more the just 10 parts and qty's.

 

I thought about, maybe having a secondary form on my HTML page, with different sets of submit buttons, and they can enter 1 part at a time... but I could not get the secondary form Submit button to not clear the data in the primary form...so that didn't work for me.!

 

I'm am basically stuck here... there maybe one or two other issues I have...but those are not so critical.

 

Any thoughts on this or recommendations on how I should approach the problem would be greatly appreciated.

 

I do have a great deal of MS Access experience but this PHP is a totally different monster. For those who know access, it's very easy to add a new record in a subform and just keep adding records...which is sort of what I'm trying to do here....

 

Thanks again.... Eddie.

Link to comment
Share on other sites

I had the same problem with my site.

 

<?php

if (isset($_POST['submitted'])) {
$part = array();
foreach ($_POST['part'] as $postpart) {
	if (!empty($postpart)) {
		$part[] = $postpart;
	}
}
foreach($part as $p) {
	echo $p.'<br>';
}
}

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"><input type="hidden" name="submitted">';
if ((is_numeric($_GET['numboxes'])) && ($_GET['numboxes'] > 0)) {
for ($nb = 0; $nb < $_GET['numboxes']; $nb++) {
	echo '<input type="text" name="part[]"><br>';
}
} else {
echo '<input type="text" name="part[]"><br>';
}
echo '<input type="submit" value="submit"></form>';

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="get">
Nuber of boxes: <input type="text" name="numboxes">
<input type="submit" value="submit">
</form>';
?>

 

That's the basic princaples. I hope you can implement them into your script with ease.

Link to comment
Share on other sites

  • 1 month later...

I had the same problem with my site.

 

That's the basic princaples. I hope you can implement them into your script with ease.

 

Hi micmania1, thanks for the reply, I'm sorry I didn't get back to you, I forgot to check the post. I want to try your code out, but someone from another forum helped me with this problem.... here is the resolved code that mcccy005 from  http://www.dhtmlgoodies.com/forum helped me with,

 


<?php include("include/db_connect.php"); ?>

<?php

if (isset($_POST['part']))
{
//Count parts in array
$total_parts=(count($_POST["part"]));

// loop to define insert data (remember that the array keys start from 0)
$data_insert='';
foreach($_POST["part"] as $key=>$val){
   $data_insert.='("'.$val.'","'.$_POST["qty"][$key].'"),';
}

//remove last comma
$data_insert=substr($data_insert,0,-1);

print "$data_insert";
$sql="INSERT INTO tbl_service_calls_parts (part_number,qty) VALUES ".$data_insert."";

if (!mysql_query($sql))
  {
    die('Error: ' . mysql_error());
  }

mysql_close($con);

?>

<?php

} else {    // if form hasn't been submitted

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <title>Dynamic fields</title>
   <script language="javascript">
   
   var counter=1;
   function addRow() {
     
      counter=counter+1;
    //Alert(counter);
      var tbody = document.getElementById("table1").getElementsByTagName("tbody")[0];
      var row = document.createElement("TR");

    //Number
    var cell1 = document.createElement("TD");
    var cell1 = document.createElement("TD");
    //cell1.setAttribute("class","list_side");
    //cell1.setAttribute("className","list_side");
    cell1.innerHTML = " "+counter+"";
     
    //Part
    var cell2 = document.createElement("TD");
    var inp2 =  document.createElement("INPUT");
    inp2.setAttribute("type","text");
    inp2.setAttribute("name","part["+counter+"]");
    cell2.appendChild(inp2);
   
     //Quantity
    var cell3 = document.createElement("TD");
    var inp3 =  document.createElement("INPUT");
    inp3.setAttribute("type","text");
    inp3.setAttribute("name","qty["+counter+"]");
    inp3.setAttribute("value","");
    inp3.setAttribute("size","6");
    cell3.appendChild(inp3);
     
    row.appendChild(cell1);
    row.appendChild(cell2);
    row.appendChild(cell3);
    tbody.appendChild(row);
    //alert(row.innerHTML);
   }
   </script>
</head>

<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <p> </p>
  <table width='200' align='center' id='table1'>
    <tr>
      <td> </td>
      <td class='list_data' colspan='2'><input name="button" type='button' onClick='addRow();' value='New Part'></td>
    </tr>
    <tr>
      <td> </td>
      <td class='list_title'>Part Number</td>
      <td class='list_title'>Qty</td>
    </tr>

    <tr>
      <td class='list_side'>1</td>
      <td><input type='text' name='part[]' size='20'></td>
      <td><input type='text' name='qty[]' size='6'></td>
    </tr>
   
  </table>
  <p>
    <label>
    <div align="center">
      <input type="submit" name="Submit" value="Submit">
    </div>
    </label>
  </p>
</form>

<?php

}

?>

</body>
</html> 

 

the code is working great all for one flaw...but I'm going to start a new topic for that because it's a different problem, I'll link it back to this after it's resolved....but this code, works, it's fast, and really cool....I just wanted to share it with everyone.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.