Jump to content

dynamically creating names and ids in forms and then re-calling the information


bruckerrlb

Recommended Posts

I'm trying to create a line style form. I have a company, and they should be able to add how many product licenses they have, but I am getting stuck and not even sure if this is possible. Basically I have a form to edit company information and within this form, I'd like for the user to be able to write in how many licenses a company has for each specific product. To achive this I have the following

 <form action="<?php echo $PHP_SELF;?>" method="post" name="modcompany">
      <input type=hidden name="id" value="<?php echo $company_id ?>">
   <table class="floattable">
   <tr>
   <td class="row2">Company ID</td><td class="row2"><?php echo $company_id; ?></td>
   </tr>
   <tr>
  <td class="row1">Company Name</td><td class="row1"><input type="text" name="company_name" value="<?php echo $company_name; ?>" /></td>
  </tr>
  <tr>
   <td class="row1">Address</td><td class="row1"><input type="text" name="address" value="<?php echo $address; ?>" /></td>
    </tr>
  <tr>
    <td class="row1">City</td><td class="row1"><input type="text" name="city" value="<?php echo $city; ?>" /></td>
     </tr>
  <tr>
     <td class="row1">State</td><td class="row1"><input type="text" name="state" value="<?php echo $state; ?>" /></td>
      </tr>
  <tr>
      <td class="row1">Zip</td><td class="row1"><input type="text" name="zip" value="<?php echo $zip; ?>" /></td>
       </tr>
  <tr>
       <td class="row1">Country</td><td class="row1"><input type="text" name="country" value="<?php echo $country; ?>" /></td>
        </tr>
  <tr>
        <td class="row1">Phone</td><td class="row1"><input type="text" name="phone" value="<?php echo $phone; ?>" /></td>
         </tr>
  <tr>
         <td class="row1">Contact</td><td class="row1"><input type="text" name="contact" value="<?php echo $contact; ?>" /></td>
          </tr>

  <tr>
         <td class="row1">Status</td><td class="row1"><input type="text" name="status" value="<?php echo $status; ?>" /></td>
          </tr>
  <tr>
         <td class="row1">Kayako Link</td><td class="row1"><input type="text" name="kayako_link" value="<?php echo $kayako_link; ?>" /></td>
          </tr>
  <tr>
         <td class="row1">Vtiger Link</td><td class="row1"><input type="text" name="vtiger_link" value="<?php echo $vtiger_link; ?>" /></td>
          </tr>
  <tr>
         <td class="row1">Internal Link</td><td class="row1"><input type="text" name="internal_link" value="<?php echo $internal_link; ?>" /></td>
          </tr>
  <tr>
         <td class="row1">Notes</td><td class="row1"><textarea name="notes" rows="5" cols="5"><?php echo $notes; ?></textarea></td>
         
  </tr>
  
  <?php 
   		$sql_license = "SELECT * FROM products";
      		$result_license = mysql_query($sql_license);        
      			while($row_license = mysql_fetch_array($result_license))
			{ 
  					$product_name = $row_license['product_name'];
				$product_id = $row_license['product_id'];
  ?>
   <tr>
         <td class="row1"><?php echo $product_name; ?></td><td class="row1"><input type="text" name="product<?php echo $product_id; ?>" /></td>
         
  </tr><?php
  
			}
  ?>
  </table>

 

The part that is messing with me is the end, how would I be able to recall the name parameter to insert it into the database. It's the last part that's confusing me

 <td class="row1"><?php echo $product_name; ?></td><td class="row1"><input type="text" name="product<?php echo $product_id; ?>" /></td>

 

There are going to be various products, but how would I be able to identify each one?

 

I'm identifying the other posts like this

if (isset($_POST['modcompany']))
   {
      $company_name = $_POST["company_name"];
  $address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$contact = $_POST['contact'];

$status = $_POST['status'];
$kayako_link = $_POST['kayako_link'];
$vtiger_link = $_POST['vtiger_link'];
$internal_link = $_POST['internal_link'];
$notes = $_POST['notes'];

 

But I just don't see how I can call the product$productid part..any ideas?

 

Thanks

 

Link to comment
Share on other sites

The dynamic form field names means you need to look in the POST vars and use strpos() to check each one, then when found, use substr() to parse the product ID's. Have a look at this:

 

foreach($_POST as $key => $value)
{
// looping through each post variable.  $key is the textbox name

// check if the the variable name starts with 'product' which indicates it's a product$id variable
if(strpos($key, 'product') === 0)
{
	// found one, need to parse the ID off the end
	$id = substr($key, 7); // 7 because 'product' is 7 chars long

	/*
	    at this point you now have the product id in $id and the value of the textbox in $value
	    so you can now add to database or do anything else you want
	*/
}
}

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.